Professional Documents
Culture Documents
Memcached 23
Memcached 23
Memcached is
a high-performance and free distributed memory object caching system.
It stores objects as a key value pair similar to hash tables and operates completely as an in-memory data store.
It is probably the second largest used software on the Internet after Linux operating system.
Why Memcached?
Advantages:
It is an extremely light-weight process that can be co-hosted even with application servers.
It reduces database workload and Disk I/O, thereby making it best suited for web/mobile applications
Disadvantages: test
It cannot cache large objects > 1MB with key size > 255 characters
It has no replication. Hence, in case of clustering, the memory stores contain mutually exclusive datasets."
Co-hosted with Application Server : In this model the Memcached process runs on the same infrastructure like that of the
application server
Central Hosting: In this model, a large server runs the Memcached process which connects with different application
server instances through sockets.
Memcached follows a multi-state architecture with every instance having the ability to both read and write.
Redis follows a master-slave single state architecture with all writes happening to master and reads happening
from N slaves.
source: antirez.com
wget http://www.memcached.org/files/memcached-1.x.x.tar.gz
tar -zxvf memcached-1.x.x.tar.gz
cd memcached-1.x.x
./configure && make && make test && sudo make install
where 1.x.x is the latest version of the file you can download from http://www.memcached.org.
Download a stable version, in either 32-bit or 64-bit. I have tested the 64-bit version.
Create a directory similar to c:\memcached\
Copy the .zip content to that directory. There should be one memcached.exe file by itself (32-bit) or with a couple of .dll
files (64-bit)
Open command prompt as an administrator. Go to Start > Search and type 'cmd'
To start the service, type 'c:\memcached\memcached.exe -d start'
To stop the service, type 'c:\memcached\memcached.exe -d stop'
To change the memory pool size, type 'c:\memcached\memcached.exe -m 512' for 512MB
Source: ubergizmo
Memcached Configurations
All Memcached Configurations are available in the /etc/memcached.conf(for ubuntu only) .
You can alter these configurations on the configuration file for permanent changes.
In case of non-persistent changes, you can provide them as command-line arguments during Memcached startup.
Memcached Configurations
The configurations for Memcached are as follows:
-c: Max incoming connections for Memcached to be inline with ulimit parameter
In this diagram, Memcached is a large storage, partitioned for N servers where N denotes the number of application servers.
Every request to Memcached has a key in it and when the request is sent to the client the following steps take place:
This scenario is valid only when Memcached is centrally hosted. In case of co-hosting with application servers, the clients do not
have to route between different Memcached servers.
Slabs,Pages & Chunks
Slabs: Memcached memory pool contains memory slabs with certain fixed size ranges. Like 0-1000 bytes, 1001-2000
bytes, 2001 - 5000 bytes etc.
Pages: Every Slab can contain one or more pages and the size of the pages are limited to 1M.
Chunks: Every page contains one or more Chunks of standard size 2000Bytes. At the max, a page can have 5 chunks.
Traverses the pages in that slab to identify a chunk with the required amount of free space.
Consistent Hashing
$server_id = hashfunc($key) % $servercount;
A web application can talk to multiple Memcached servers, and the client running your application should get updated with the list
of IPs running the Memcached server.
Memcached client generates the hash of each key that is stored or retrieved. The modulus function is applied to identify which
server will store the information and vice versa, the process is repeated for retrievals as well.
Every object in the memory pool has a counter associated with it which contains the timestamp. On each fetch or update or
create, the timestamp is reset to its current time, leaving Memcached to identify Least Recently Used (LRU) objects and to evict
them.
Items are evicted when the slab class is left with no free chunks.
Note: Eviction is forced in Memcached and hence the durability of data within Memcached is extremely low.
Use language Specific clients like php5-memcached, python-memcached and connect to memcached server through
sockets.
As part of this course, we will try the telnet method while in real application scenarios second method will be prominently used.
Syntax:
add greeting 1 0 11
Hello world
Explanation: Use key greeting, use 1 as a flag (arbitrary metadata), use 0 as the expiry (i.e, never expire), and expect the value to
be 11 bytes long".
2 of 10
Syntax:
set newgreeting 1 0 14
Happy Birthday
Syntax:
get keyname
Memcached - Delete
Syntax:
delete greeting
stats items
STAT items:1:number 2
Numeric after the item is the slab number. To get all keys in the slab.
flush_all
flush_all 90
The first command clears all the objects and their corresponding keys while the second one clears them after 90 seconds.
Command: append, Syntax: append key 0 60 15, Description: Append data to existing key
Command: prepend , Syntax: prepend key 0 60 15, Descripton: Prepend data to existing key
Command: incr, Syntax: incr mykey 2, Description: Increments numerical key value by given number
Command: cas, Description: An operation that stores data, only if you have updated it last. Useful for resolving race
conditions on updating cache data.
You can read more on operations in this wiki
Consider that you need to retrieve 10 keys from a cache and there are 3 servers of Memcached.
Memcached - CAS
Latest versions of memcached(1.2+) provide an atomic operation called cas(). CAS also known as Check and Set or Compare and
Swap allows you to set only if the same memcached client is
requesting to change the data, else it returns with error.
CAS command may produce one of the following result −
Memcached - Locking
From Memcached docs,
A series of commands is not considered atomic. If you issue a 'get' against an item, operate on the data, and then wish to 'set' it
back into Memcached, you are not guaranteed to be the only process working on that value. In parallel, you could end up
overwriting a value set by something else
The easiest approach to implement locking is to use view count.
In this method, a psuedo-lock system is applied by using INCR operation in the count instead of the actual data.
Memcached Monitoring
Memcached servers can be monitored in two modes,
CLI Mode: Using the stats commands with multiple parameters and Memcached-top suitable for monolithic instances
Memcached Tools: Monitoring tools like phpMemcachedadmin can be used for monitoring and suitable for cluster
deployments.
Over the next few cards in this course, you will understand how to monitor usual suspects
Hit Rate indicates the efficiency of your Memcached server and is computed by get_hits / cmd_get
Fill percent represents free memory available for caching and is computed by used bytes / limit_maxbytes
Evictions is a scenario where items are removed before their expiry time due to lack of memory and item is selected
with a pseudo-LRU mechanism.
stats
STAT pid 27631
STAT uptime 104
STAT time 1401932723
STAT version 1.4.5
STAT pointer_size 64
STAT rusage_user 0.004999
STAT rusage_system 0.010998
STAT curr_connections 10
[...]
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 4354
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT evictions 23420009
STAT reclaimed 0
END
conn_yields or Connection Yields parameter defines the number of times Memcached throttled connections to a specific client. If
connection yields are high, it means that there are large multi-get requests or R value(number of connections to client) is less.
stats slabs
STAT active_slabs 0
STAT total_malloced 0
END
If your evictions count on specific slabs are high, then you need to probably work on altering the growth factor.
Growth factor denotes the increasing size between slabs. The default growth factor is 1.25
Memcached Replication
Memcached replication can allow the memory pool of one instance of Memcached to be synchronous with another.
Replication here doesn't bring the entire set of Memcached servers to the same state, instead it ensures that there are more than
one Memcached servers in the same state, offering extended durability.
.
Now, you should be able to replicate objects across both the instances of Memcached.
$ sudo apt-get update && sudo apt-get install memcached
$ add F 15 0 15 15
0-0
STORED
$ add N 0 15 0 0
0-0
STORED