Pages

Wednesday, October 2, 2013

How Memcached Works




What is Memcached?
Memcached is a distributed memory caching system more like an in-memory key-value store (hashtable).

When to use Memcached?
There is no specific ruleset on when to use it. You need to understand how your application works and where are most of the bottlenecks. It is mostly used to cache dataset for frequent database queries and avoid disk i/o. 

Details for installing memcache can be found here

Once memcached is installed run following command to see memcache version and help:
$ memcached -h
memcached 1.2.6

Lets assume you have a dedicated memcache server with RAM 60 GB. You decide to you 50GB of memory for caching. 

Start a memcache daemon
-d to start as daemon in background
-p port number on which memcache will listen (11211)
-m memory allocated for memcache (50GB)
By default page allocated will be of size 1MB

$ memcached -d -p 11211 -vv -m 60000 -n 100000
slab class   1: chunk size    100048 perslab      10
slab class   2: chunk size    125064 perslab       8
slab class   3: chunk size    156336 perslab       6
slab class   4: chunk size    195424 perslab       5
slab class   5: chunk size    244280 perslab       4
slab class   6: chunk size    305352 perslab       3
slab class   7: chunk size    381696 perslab       2
slab class   8: chunk size    477120 perslab       2
slab class   9: chunk size    596400 perslab       1
slab class  10: chunk size    745504 perslab       1
slab class  11: chunk size   1048576 perslab       1



Pages: When memcache starts, it partitions its allocated memory into smaller parts called pages.Each of those pages can be assigned to a slab.

Slabs: Memcached have a concept of slabs. It allocates pages to its slabs. When memcache allocates pages, it virtually attaches those pages to one of the slabs. Memcache will be able to allocate 50GB/1MB = 50,000 pages in total.

Chunks: When you try to store a value into the cache, memcached checks the size of the value that you are adding to the cache and determines which slab contains the right size allocation for the item. If a slab with the item size already exists, the item is written to the block within the slab.

If the new item is bigger than the size of any existing blocks, then a new slab is created, divided up into blocks of a suitable size. If an existing slab with the right block size already exists, but there are no free blocks, a new slab is created. If you update an existing item with data that is larger than the existing block allocation for that key, then the key is re-allocated into a suitable slab.

Slab 1: Lets say an item(key, value and overhead) of size 90 kbytes is being inserted into memcached. It will find that 1st slab contains chunks of size 100 kbytes. So it will insert into 1st slab. Note that 100-90=10kbytes memory will be wasted. Total chunks allocated created in a single page of 1st slab will be 1MB/100KB = 10 chunks. So a single page in 1st slab will contain 10 chunks. If all 10 chunks get filled in, memcached will allocate more pages in slab 1. (See Figure 2)

Slab 2: Lets say an item(key, value and overhead) of size 120 kbytes is being inserted into memcached. It will go into 2nd slab which contains chunks of size 125 kbytes. By default chunk size increases with factor of 1.25. So it will get inserted into 2nd slab. Note that 125-120=5kbytes memory will be wasted. Total chunks allocated created in a single page of 2nd slab will be 1MB/125KB = 8 chunks. So a single page in 2nd slab will contain 8 chunks. If all 8 chunks get filled in, memcached will allocate more pages in slab 2. (See Figure 3)




Eviction Policy: Memcached eviction is based on LRU (least recently used) algorithm. It means it evicts the oldest item in the slab to make space for new items, if no more pages can be allocated. Every item has a timestamp counter in the header and its updated to current time everytime you fetch/ update the item. 
NOTE: If an item is to be inserted in slab 1, it will evict item from slab 1 only, and not borrow page from another slab (although slab reassigning has been started in memcached 1.4.14)

No comments:

Post a Comment