ProductionAdvanced

What is a cache stampede and how do you prevent it?

“A deploy briefly takes the database down. Explain and fix.”

What this tests

  • Thundering herd
  • Stampede mitigations

Answers by level

Read the beginner answer first and notice what is missing.

A stampede: a popular key expires (or a deploy flushes the cache) and many concurrent requests miss simultaneously, all querying the database for the same rows. The database gets the full read load it had been shielded from, at peak traffic.

Fixes: TTL jitter so keys do not expire together; a single-flight lock so only the first miss repopulates while others wait; stale-while-revalidate so an expired value is served during a grace window while one request refreshes it.

Green flags · Red flags

Strong green flag · Offers all three mitigations and their tradeoffs.
Green flags
  • Names the herd of simultaneous misses
  • Jitter / single-flight / SWR
  • Deploy-flush awareness
Red flags
  • "Add servers"
  • Uniform TTLs with no jitter

Follow-up questions

F1
Why does a deploy cause a stampede?

Scenario

Database CPU spikes to 100% for 10 seconds after every deploy, then recovers. Diagnose and fix.

Learn this topic