Caching interview question · Most asked
What is a cache stampede (thundering herd) and how do you prevent it?
Summary
A stampede happens when a popular key expires and many concurrent requests miss at once, all hitting the database to recompute the same value, which can overload it. Prevent it with request coalescing/locks, early (probabilistic) recomputation, stale-while-revalidate, and jittered TTLs.
Longer Answer
Options: (1) a short lock (SET key:lock NX EX 5) so only one request recomputes while others wait or serve stale; (2) refresh before expiry with probability increasing as expiry nears (XFetch); (3) serve the stale value while one background task refreshes; (4) add random jitter to TTLs so many keys don't expire together.
Key Points
- Many misses on one hot key
- Single-flight locks
- Early/probabilistic refresh
- Stale-while-revalidate
- TTL jitter
More most-asked Caching questions
- Explain the main caching patterns: cache-aside, read-through, write-through and write-behind.
- Why is cache invalidation hard, and what strategies help?
- How does HTTP caching work (Cache-Control, ETag, CDNs)?
- Explain the difference between cache penetration, cache breakdown and cache avalanche.
- In cache-aside, why can a value be stale even though you delete the key on every write?
- How can a CDN leak one user's data to another, and how do you prevent it?
- Should you invalidate the cache before or after the database commit?
- How does stale-while-revalidate work, and when is it inappropriate?