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

← All Caching questions