Thundering herd, hot keys, stampedes and cache penetration
A cache that works on average can still fail in four ways. One hot key expires and thousands of requests rebuild it together, or many keys vanish at once. One key can outgrow its node, and requests for keys that don't exist go straight through. Each has a standard fix, and interviewers expect you to raise them first.
The cinema snack counter
Ten minutes before a big film, the popcorn tray runs empty. Five staff with popcorn orders each start a batch of the same popcorn at the one machine while the queue waits. A well-run counter has one person make the next batch while the others serve the last scoops, and starts it when the tray is half empty, not bare. On a blockbuster night, half the queue wants large salted, so it gets several trays along the counter. And a menu by the queue answers people asking for flavours the cinema doesn't sell.
- the staff → the app servers and the requests they serve
- the popcorn machine → the database query that rebuilds the value
- the tray running empty → a hot key expiring
- five batches at once → a cache stampede
- one person makes the batch → request coalescing, or a lease or lock
- starting at half empty → early refresh
- several trays of the favourite → copies of a hot key
- the menu by the queue → a Bloom filter against cache penetration