Cache Stampede Problem and Solutions
You'll learn how to prevent a thundering herd of requests from overwhelming your backend when a popular cache entry expires.
What Is Cache Stampede?
A cache stampede (also called "thundering herd") occurs when a heavily-accessed cache entry expires, and suddenly hundreds or thousands of concurrent requests all miss the cache simultaneously. Each request then rushes to regenerate the same data from the backend, creating a massive spike in database or API load—exactly what caching was supposed to prevent.
Think of it like a popular coffee shop that runs out of a special drink. If 100 customers arrive at once and each demands a fresh batch be made right now, the barista is overwhelmed—even though they're all asking for the identical recipe.
Common Solutions
Probabilistic Early Expiration
Regenerate the cache entry before it expires, with a small random probability. This spreads regeneration across time rather than concentrating it at the exact expiration moment.
Locking (Mutex Pattern)
When a cache miss occurs, the first request acquires a lock (using Redis SETNX or similar) to regenerate the entry. Subsequent requests detect the lock and either wait briefly or serve stale data temporarily instead of hitting the backend.
Serving Stale While Revalidating
Return the expired (stale) cache entry immediately to users while asynchronously refreshing it in the background. This keeps response times fast and prevents backend overload.
Cache-Warming
Proactively refresh popular entries before they expire using background jobs—the refresh-ahead pattern you've learned.
Key Takeaway:
Cache stampede happens when many requests simultaneously regenerate expired cache entries; prevent it with locking, probabilistic early refresh, stale-while-revalidate, or proactive warming.