AlgoViz

Memcached

medium 30 min

Memcached 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 hearSayBecause
Cache query results, nothing elseMemcached is enoughPlain get and set
Hundreds of millions of tiny valuesLook at item overhead and slab sizesMemory is the bill
Leaderboard, queue, Pub/SubRedisMemcached has no such types
Must survive a restartNot MemcachedIt is a cache
We double the servers every yearConsistent hashing and a warm-up planMoved 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.