Caching and search
Memcached
Memcached is a simple distributed in-memory cache that stores key-value pairs with no persistence.
What it is
Memcached was created by Brad Fitzpatrick for LiveJournal in 2003. It is one of the first mainstream caches: a server holds byte values under string keys and evicts old ones when memory runs out.
It is deliberately minimal: no data structures, replication or disk storage. In return it is multithreaded, very predictable and scales out easily, because the client picks the node by hashing the key.
Memcached fits when you only need to cache query results or rendered pages quickly and losing them on restart is fine.
Use it when
- You need a simple cache of strings or serialized objects.
- You have many CPU cores and the load is plain get and set.
- The cache needs no persistence, replication or rich types.
Avoid it when
- You need counters with sets, queues, pub/sub or leaderboards: pick Redis or Valkey.
- The data must survive a node restart.
- You need built-in failover and replication.
Key concepts
- Slab allocator
- Memory is split into size classes of chunks to avoid fragmentation.
- LRU eviction
- When memory is full, the least recently used keys are removed first.
- Client-side sharding
- Servers do not know each other: the client picks the node by key hash, usually with consistent hashing.
- TTL
- Each key can have a time to live after which it counts as missing.
Example
Interview questions
1. How does Memcached differ from Redis?
Memcached caches only strings, without persistence or replication, but it is multithreaded and very simple. Redis has data structures, disk persistence and pub/sub.
2. How are keys distributed across nodes?
The client does it: it hashes the key and picks a server, usually with consistent hashing. Adding a node then reshuffles only a small share of keys.
3. What happens if a Memcached node dies?
Its cache is lost and requests go to the data source. So the app must work without the cache, and the cache warms up again gradually.
Comparisons with this technology
Related technologies
Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).