Index/Memcached

SponsorGitHub
Key technologyCache3 min

Memcached

A cache that does nothing but get and set — fewer features than Redis, and that is the argument for it.

At a glance

Model
One opaque blob under a string key, with an expiry
Threads
Multithreaded: more raw ops per second per box than Redis
Memory
Slab allocator, fixed size classes, no fragmentation
Cluster
None — the client picks a node by consistent hashing
Durability
None, and no replication: a dead node is a cold node
Value size
1 MB by default; anything bigger belongs in object storage

Key concepts and capabilities

The short listwhat it gives you
The whole APIget, set, add, delete, incr/decr and cas, and nothing else
cas returns a version with each read and rejects a stale write — the one primitive for safe read-modify-write
No replication — losing a node loses its keys, which for a look-aside cache is latency, not correctness
Client-side consistent hashing — adding a node moves only its share of keys instead of reshuffling everything
Slab allocator — items go into fixed size classes, so memory can be stranded when item sizes drift
Leases — on a miss, one client gets the token to refill while the rest briefly serve stale (the memcache paper)
Multithreaded — one box uses all its cores, which is the per-byte and per-core cost argument
Versus Redis — no data structures, no persistence, no replication, no cluster; that simplicity is the pitch

Use cases

A look-aside cache for rendered fragments

The shape Memcached exists for: an opaque blob under a key, read far more often than it is written, cheap enough per byte that a huge fleet of cache nodes is affordable. A rendered HTML fragment, a serialized API response, a query result.

DatabaseFocusClick a node for details

Surviving a dead node

There is no cluster: the client holds the server list and picks a node by consistent hashing. A node dying costs you its share of the keys and nothing else — provided the database behind it can absorb that share of misses, which is the question you will be asked.

DatabaseFocusClick a node for details

Stopping a stampede with leases

A popular key expires and every request misses at once. The lease is the named technique: exactly one client is handed the token to recompute, and the rest serve the stale value for the moment it takes.

DatabaseFocusClick a node for details