CodePark/ questions / Invalidation, stampedes & consistency
START PRACTICINGPRACTICE
Backend question · Redis & caching › Invalidation, stampedes & consistency

What is a cache stampede (thundering herd), and how do you prevent it?

A cache stampede happens when a popular key expires or is deleted, and many requests miss it at the same moment. They all run the same expensive query or computation in parallel. This can overload the database exactly when traffic is highest. It is also called a thundering herd.

Ways to prevent it:

  • Single-flight locking: only one request rebuilds the value. It is the request that wins SET lock:key token NX PX 5000 (set the lock only if it does not exist, for 5 seconds). The others wait briefly and read again, or get stale data. Inside one Node process, share one in-flight promise per key.
  • Stale-while-revalidate: store a soft expiry time inside the value, and give the key a longer real TTL. After the soft expiry, serve the stale value and refresh it in the background.
  • Probabilistic early refresh: each request has a small chance to refresh the value before it expires. The chance grows as expiry gets closer. So one request refreshes early, before the crowd arrives.
  • Jittered TTLs: add a random amount to each TTL, so many keys do not expire together.
  • Proactive refresh: for critical keys, refresh them with a background job before they expire.
  • Many requests missing one hot key at once overload the data source.
  • Use a lock or single-flight so only one caller rebuilds the value.
  • Serve the stale value while it refreshes in the background.
  • Refresh early with a probability that grows near expiry.
  • Add jitter to TTLs and pre-warm critical keys.