A popular key expires and forty thousand requests reach the database in the same second. What do you change?
A cache stampede happens when one hot key expires and every concurrent miss rebuilds the same value. Let one request rebuild while the rest wait or serve stale, then prevent repeats with stale-while-revalidate, jittered TTLs and refresh-before-expiry. Use this caching answer to show the decision, trade-off, and evidence rather than a memorised definition.
Answer
Short answer
A cache stampede is duplicate rebuild work, not ordinary traffic. Use single-flight locking so one request rebuilds the hot value while others wait, or serve the stale value while one request refreshes it in the background. Then add TTL jitter and early refresh so hot keys do not expire together or wait until the first miss to be rebuilt.
Was this answer helpful?
Likely follow-ups
- The single rebuilding request fails with a database timeout. What do the waiters see, and who retries?
- How would you refresh a value before it expires without knowing in advance which keys are hot?
- Your cache is a cluster and the hot key lives on one node whose CPU is now saturated. What changes?
- When is it correct to let the stampede happen and protect the database with a queue instead?
Related questions
- How do you handle cache invalidation, and what goes wrong at scale?hardAlso on caching and redis5 min
- Your rate limiter has to work across twenty gateway instances. What does Redis do, and what breaks first?hardAlso on redis5 min
- A document was updated an hour ago and the assistant is still quoting the old version. Walk me through the diagnosis.hardAlso on caching6 min
- Your rate limiter runs in five regions with a cap of 100 requests a minute per user. One user gets 500 through. Where did the counting go wrong?hardAlso on redis5 min