Memcached
medium 30 minMemcached is a small, fast, shared cache for plain values.
When it comes up
The moments this idea belongs in a design interview, and the moments it does not.
Signals
When it comes up
| You hear | Say | Because |
|---|---|---|
| Cache query results, nothing else | Memcached is enough | Plain get and set |
| Hundreds of millions of tiny values | Look at item overhead and slab sizes | Memory is the bill |
| Leaderboard, queue, Pub/Sub | Redis | Memcached has no such types |
| Must survive a restart | Not Memcached | It is a cache |
| We double the servers every year | Consistent hashing and a warm-up plan | Moved keys are misses |
In the interview
- Say the client shards
- Say what happens when a node dies
- Give the hit rate and the database load
- Mention the TTL with every key
Avoid
- Claiming the servers replicate
- Storing sessions nobody can rebuild
- Hash modulo N
- Quoting speeds without a machine
Options
Explain your choices
The cache needs twice the memory. How do you grow from 4 to 8 servers?
BreaksChange the list with modulo hashing
- How
- Deploy a new list of 8 servers.
- Limit
- From 4 to 8 half of the keys move at once, and clients with the old list disagree with the others during the deploy.
- Choose when
- A test environment.
WorksConsistent hashing, add them one at a time
- How
- Each new server takes its share. Wait for the hit rate to recover before the next.
- Limit
- Slow, and still many misses in total.
- Choose when
- The database has room to spare.
StrongA warm second pool behind a proxy
- How
- Build the pool of 8, write and delete in both, fill the new pool from the old on a miss, then move reads over.
- Limit
- Both pools run for a while, and deletes must reach both.
- Choose when
- The database cannot take the misses.