A popular product page is fast all morning. At noon, its cached information expires. A hundred people request the page before the first replacement has been loaded, and the database suddenly receives a hundred very similar requests. The cache did not create the customers. It concentrated their work at precisely the moment it stopped supplying the answer.
A cache stampede happens when multiple requests encounter the same missing or expired cached value and independently try to rebuild it. The Redis cache-aside guide describes this pattern and possible coordination techniques. The underlying data source can receive far more work than one refresh actually requires, and the added delay can create still more waiting requests.
That does not mean every cache miss is a problem or that every system needs a distributed lock. The useful questions are which value is popular, how expensive it is to rebuild, how much staleness is acceptable, and what happens when the refresh fails.
Follow one request through the miss
In a simple cache-aside design, an application checks a cache for a value. On a hit it can use the cached result. On a miss it reads the source, prepares a result and puts that result in the cache for subsequent requests. The cache and source may be separate services with different availability and capacity.
Suppose a fictional store caches an assembled availability display for a particular product. Rebuilding it requires a database query and some application processing. If one visitor arrives after expiration and the rebuild takes half a second, the next visitor may find the newly cached value. If many visitors arrive during that half-second, each may see the same miss before anyone has completed the rebuild.
For a simple illustration, assume 80 independent requests arrive during the refresh interval and each starts one database query. The source sees 80 queries rather than one. The exact overlap depends on arrival timing, cache behavior and implementation; it is not a prediction that every group of 80 visitors produces 80 simultaneous queries.
The danger becomes larger if the source slows down as work accumulates. A slower refresh creates a longer interval in which more arrivals can miss. Those additional queries can further slow the source. Waiting connections, request timeouts and retries can spread the impact beyond the original product page.
Expiration is not the only trigger
A time-to-live, often shortened to TTL, sets how long a cache entry remains available under a particular policy. When a popular entry expires, concurrent readers can all discover the missing value. But the same pattern can follow an explicit invalidation, an application restart, a newly deployed cache, eviction under memory pressure or a temporary cache outage.
The distinction matters during diagnosis. Extending a TTL might reduce a scheduled expiration event, but it will not automatically solve a mass invalidation after an update. Prewarming can reduce cold-start work, yet it can itself overload the source if every application instance warms the same large set of keys at once.
Not all entries deserve the same treatment. A rarely requested report that takes ten seconds to rebuild may never experience simultaneous misses. A moderately expensive page requested thousands of times per minute may be much more exposed. Look at request rate for the specific key or group of keys, rebuild duration and source cost together.
One refresh, several waiters
One approach is to coordinate requests so that one worker performs a refresh while others wait briefly for its result. This pattern is often called request coalescing or single-flight loading. Redis demonstrates an example using a short-lived lock and a check that the caller still owns the lock before releasing it in its cache-aside implementation guide.
The important word is coordinate. A lock held only inside one application process may prevent duplicate work within that process while several other processes each perform their own refresh. A shared lock can cover a wider group, but introduces another dependency and its own failure behavior. Decide the intended scope before assuming a mechanism covers the whole fleet.
Waiting requests need a limit. If the loader fails, loses its connection or takes longer than expected, other requests cannot wait forever. A lock can expire while the original loader is still working, allowing another loader to begin. The design needs a deliberate timeout, safe ownership checks and a plan for how a late result is handled. A lock reduces certain duplication; it does not make every race disappear.
Also ask what happens when the coordination service itself is unavailable. Falling straight back to an unlimited number of source reads can reproduce the original incident. Rejecting every request may be unnecessarily harsh. The appropriate fallback depends on the source’s capacity, the value of a response and whether an older result can be used safely.
Choose which failure the customer sees
Some data can remain useful for a short time after its preferred refresh time. A restaurant’s editorial description might tolerate a brief delay. A paid customer’s access state, account balance or safety-critical instruction may have a very different tolerance. The application owner should define the permitted age and consequences rather than treating “serve stale” as a universal performance setting.
Picture the same item after a short source outage. The last price was refreshed 45 seconds ago; the preferred refresh interval is 30 seconds. One possible policy allows that older price to display for at most another minute while one worker refreshes it, but requires a fresh inventory and price check before checkout. Another waits briefly for fresh data, then shows an unavailable message if the source is still failing. The first protects browsing continuity but risks showing a price that changes at checkout. The second protects freshness but can interrupt more shoppers. Neither is a default: choose the maximum age and the final transaction check deliberately.
A third policy sends every waiting request straight to the source. It sounds fresher until the source is overwhelmed and answers nobody. Bounded concurrency and a visible temporary failure may produce fewer incorrect expectations and a faster recovery. State which data can be stale, how long it can remain so, and which action must revalidate it. “Serve stale” is a product decision wearing an infrastructure badge.
Privacy and access boundaries matter too. A cache key must distinguish the dimensions that determine which data a caller may see. Coalescing requests under an overly broad key can combine work that should remain separate. Performance measurements are not a substitute for checking authorization and data isolation.
Spread work without inventing capacity
If many different keys receive the same expiration schedule, small randomized differences in TTL can spread their refreshes over time. This is commonly called expiration jitter. It is most useful when the problem is many keys expiring together; it does not by itself stop a crowd from requesting the same single key as soon as that key expires.
Early refresh can move some work away from the moment a value expires. A queue or concurrency limit can bound the number of simultaneous rebuilds. Prewarming a small set of genuinely popular entries before a planned traffic event can help if it is paced and measured. Each option spends resources at a different time, and none creates unlimited database capacity.
Retries deserve attention. If a request times out and the client immediately retries several times, one user action can become several source attempts. Backoff, bounded retries and a clear failure response can prevent a bad interval from becoming a larger wave. Our guide to safe retries explains why repeated operations need their own design; suppressing duplicate refresh work is related but does not make a write automatically safe to repeat.
Measure the key and the bottleneck
Start with a timeline: request volume, cache hit and miss rates, source query count, rebuild latency, cache errors and application timeouts. A low overall miss rate can hide a single hot key if the rest of the cache serves many inexpensive hits. Grouping or sampling must preserve enough detail to identify the affected route without exposing private identifiers in logs.
Compare source requests with successful refreshes. If hundreds of source reads correspond to one expiring item and only one resulting cache value was needed, duplicate work is a plausible explanation. If each query serves genuinely different inputs or authorization contexts, combining them might produce the wrong answer. Confirm the cache key and actual query parameters before changing behavior.
Then look downstream. A database connection pool can exhaust even when the database is not the only slow component. The connection-pool guide distinguishes waiting for a connection from time spent executing a query. Both can increase during a stampede, but they call for different measurements.
Test failure, not just the happy refresh
In a representative nonproduction environment, arrange concurrent requests for one missing key and observe how many times the source loader runs. Repeat with a slow source, a failed refresh, an expired lock and a cache-service interruption. Check the result each caller receives, not merely whether a lock was acquired.
For a stale-serving policy, test the age limit and the transition when the source remains unavailable. For a concurrency limit, verify that callers receive a useful failure or wait response rather than silently piling up. For key design, include users or regions that must not share the same answer. Record the expected result before judging the test.
Write the choice down in the incident plan: one loader, a bounded wait, an allowed stale age where appropriate, and the exact point at which the customer sees a failure instead. Then test what happens when the source stays slow longer than anyone expected. That is the moment a cache policy stops being a diagram and starts being a promise.
Explore Infinite Upside
For Vijay Sheth’s personal accounts, read Infinite Upside: Building Wealth With Room to Live, published by Sheth Capital, Inc.
Explore a related guide
Why Do Database Connection Pools Run Out?