What is Memcached?
Memcached is Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load. Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.
How Memcached Works
- Memcached assign memory that can be used to store data item via (
-m) option. In ElastiCache this was configured using
max_cache_memoryparameter in parameter group. Value of
max_cache_memoryparameter cannot be changed. This is not directly requested to operating systems when
- Memcached split the memory into small part called page. A page in Memcached have fixed size of 1 MB. (source code reference)
- Memcached is having a concepts of slabs to manage memory. On start, memcached will define slab classes.
- Each slab class will have its own chunk size. Chunk size for each slab class is defined by the following elasticache parameters.
- chunk_size (default : 48 (bytes))
- chunk_size_growth_factor (default : 1.25)
- slab_chunk_max (default : 524288 (bytes))
- When we store item in Memcached, it will find slab class with chunk size that fit with key-value data size and additional metadata. If the total data is not fit to a slab class, it will use the next slab class
- If no chunk left on a page for a slab class Memcached will allocate another page and put them on the same slab class.
Memory Waste in Memcached
- There are two potential memory waste from
- Once a chunk is used to store an item, the remaining free space cannot be used to store another item or part of another items.
- Since Memcached is requesting memory per page (1MB), the memory is already allocated for specific slab class even if only 1 chunk is being used on the slab class.
Clustering in memcached
- Clustering in Memcached is just a bunch of nodes
- No communication / replication between nodes
- There is no master node, slave / replica nodes
- Server rely on client (application that use Memcached) hashing mechanism to know which node in cluster have the key value it needs.
- Max key size is 250 bytes (source code reference)
- Max Item Size is 1 MB by default
- 1M (from start)
- 128 M (since version ?)
- 1G (since version ?)
Memcached Slab Classes
Below table is reference of Memcached slab classes with its chunk size using default settings (default parameter group)
|Slab Class||Chunk Size (bytes)||Chunks Per Page||Usable space per chunk (Chunk Size – 48 bytes metadata)|
Large Item Size in Memcached
- Before version 1.4.29 max-item-size parameter is tied largest chunk size.
Benefit Upgrading to Memcached 1.5.10
- Cumulative fixes, such as ASCII multigets, (CVE-2017-9951) Fixed in [Memcached 1.4.39](https://github.com/memcached/memcached/wiki/ReleaseNotes1439) and (limit crawls for metadumper).
- Better connection management by closing connections at the connection limit.
- Improved item-size management for item size above 1MB.
- Before version 1.5.0 Item larger than 1 MB is always using largest slab size (around 512k). For example if we store 700 k item it will use two chunks in slab 39 (chunk size 524288). Since we only use 700k on two chunk of slab class 39 we will waste around 300k of memory
- Starting on version 1.5.0, Memcached will use multiple slab class that minimize memory waste.
- Better performance and memory-overhead improvements by reducing memory requirements per-item by a few bytes.
- On Version 1.4.39 (Release Notes) : save four bytes per item if client flags is set to 0.