In my previous two articles "Getting Started with Caching in .NET" and "A Deep Dive into Cache Eviction Policies" , I explained caching and cache eviction. A cache provides runtime storage for frequently accessed data, while cache eviction is the process of removing data from the cache.
A cache stampede is a situation that occurs when data is removed from the cache due to expiration or eviction, and multiple requests simultaneously try to fetch the same data from the underlying data source. In this article, we will discuss cache stampedes and explore different ways to prevent them.
What is Cache Stampede
A cache stampede (also called the thundering herd problem or dogpile effect) occurs when many requests simultaneously try to fetch the same data after a cache entry expires or is missing. Instead of one request regenerating the data and updating the cache, all requests hit the backend at once, potentially overwhelming the database or service.
ExampleImagine your application caches a popular product page for 5 minutes.
- Thousands of users request the page.
- The cache serves the data quickly.
- The cache entry expires.
- At that exact moment, 1,000 new requests arrive.
- Since the cache is empty, all 1,000 requests query the database.
- The database experiences a sudden spike in load, increasing latency or even failing.
Data Flow Example
Before expiration:
Users (1000)
↓
Cache (HIT)
↓
Response
After expiration:
Users (1000)
↓
Key Expired"
↓
1000 cache misses
↓
1000 DB queries for the SAME data
↓
DB overloaded
↓
DB becomes slow/timeouts
↓
More retries
↓
System can collapse
Cache Stampede Causes
- Hot key expires
- A highly requested key has a TTL.
- It expires.
- Thousands of requests miss it simultaneously.
- Every request hits the DB/API to rebuild the same value.
- Synchronized TTLs
- Many keys are created around the same time with the same TTL.
- They expire together, causing a large wave of cache misses—sometimes called a cache avalanche.
- No request coalescing / locking
- Nothing ensures that only one request regenerates a missing value.
- So
Nconcurrent requests produceNidentical DB queries.
- Cold cache
- Cache restart, deployment, flush, or eviction can remove many frequently used entries.
- Traffic immediately falls through to the backend.
- Slow cache regeneration
- If rebuilding the value takes a long time, more requests arrive while the cache is still empty.
- This increases the number of concurrent backend requests.
- Retry amplification
- The DB/API becomes slow because of the stampede.
- Clients retry timed-out requests.
- Those retries create even more load, making recovery harder.
Common Solutions
- Request coalescing (Single Flight): Allow only one request to regenerate the cache.
- Distributed locking
If you have multiple application servers, use a distributed lock (for example, using Redis) so only one server rebuilds the cache.
Server A ---- acquires Redis lock ----> DB Server B ---- waits Server C ---- waits - Stale-While-Revalidate (SWR)
Instead of removing expired data immediately:- Serve the slightly stale cached value.
- Refresh it in the background.
This minimizes user-facing latency.User │ ▼ Cache (stale) │ ├──► Return stale response immediately │ └──► Background refresh │ ▼ Database - Randomized expiration (TTL jitter)
If many keys are created at the same time with the same TTL, they may all expire together.
Instead of:TTL = 300 seconds
Use:TTL = 300 + random(0-60)
This spreads expirations over time and reduces synchronized load spikes. - Cache warming (preloading) Refresh frequently accessed cache entries before they expire.
- Never let hot keys expire
For extremely popular data:- Use a very long TTL or no expiration.
- Invalidate the cache explicitly when the underlying data changes.
Important: In modern high-traffic systems, it's common to combine several techniques for example, request coalescing, stale-while-revalidate, and TTL jitter to keep backend load stable even when cache entries expire.
Best Practices
The best practice is to combine several defenses, rather than rely on one technique. A cache stampede (thundering herd) happens when a popular key expires and many requests simultaneously hit the database/backend.
Summary
A practical guide to cache stampedes, covering how simultaneous cache misses can overwhelm backend services, common causes, real-world scenarios, and proven prevention techniques.
Thanks