Skip to content

Redis / caching · 1 of 9

Step 1 · The pattern

Check the cache first

Every request asks the cache (Redis) before the database. If the value is there — a hit — it comes back in about a millisecond and the database is never touched.

  • cache hit
  • request in flight

In short

Cache stampede: how it works and how it breaks

A cache-aside setup checks Redis before the database. Hits return in about a millisecond; a miss reads from the database and writes the value back. That is fine for ordinary keys. A hot key is different: when it expires, every request that arrives during the rebuild also misses, and all of them run the same expensive query at once. The database saturates, the rebuild slows down, and the window of misses grows. This is a cache stampede, also called a thundering herd or dog-piling. The fix is to let exactly one request rebuild the value. On a miss, the cache hands a short-lived lease to the first caller only. Everyone else waits a few milliseconds and retries the cache, or is served a slightly stale copy. Same traffic, one database query. Related techniques: request coalescing, TTL jitter and stale-while-revalidate.

How this comes up in interviews

"A popular cached item expires and your database load suddenly spikes. What is happening, and how would you stop it?"

Then they ask

  1. What should the requests that do not get the lease do, and what are the costs of each option?
  2. The request holding the lease crashes halfway through the rebuild. What happens next?
  3. How would you bring an empty cache into service without overloading the database?

A strong answer covers

  • Name it: a cache stampede, where every request that misses a hot key runs the same expensive query at once.
  • Let exactly one request rebuild: a lease, a lock or request coalescing.
  • Other requests wait briefly and retry the cache, or are served a slightly stale copy. They do not go to the database.
  • The lease must expire so a crashed holder cannot block the rebuild.
  • TTL jitter helps when many keys expire together. It does not fix one hot key.

Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.

On one page: the track cheat sheet.

Last updated: Research paper

Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.