Your dashboard loads in 40 milliseconds all morning. Then, at exactly the minute the cache expires, response times spike to 20 seconds and the database starts refusing connections. Nothing was deployed. Nothing changed. What happened is a cache stampede: the key expired, and hundreds of requests tried to rebuild it at the same time. Here is what causes it, and the lock-based pattern I use in Laravel to stop it.
What a cache stampede actually is
Consider a common setup: a report or dashboard summary that is expensive to compute, cached in Redis for one hour with Cache::remember(). When the TTL expires, the next request finds an empty cache and runs the heavy query. If that happens during busy traffic, it is not one request that finds an empty cache. It is hundreds, all in the same few seconds. Every one of them runs the full query against the database. The database, which was comfortably handling one expensive query per hour, suddenly gets several hundred at once. Connection pools fill up, queries queue, and the whole application slows down while the cache sits empty because every rebuild attempt is timing out.
Why the naive pattern is vulnerable
This is the code most of us write first:
$summary = Cache::remember('dashboard:summary', 3600, function () {
return ReportService::buildSummary(); // slow query, 8 seconds
});
Cache::remember() does exactly what it promises: get the key, and if missing, run the callback and store the result. It makes no promise about concurrency. Ten requests arriving in the same second will each see a missing key and each run the eight-second query. That is the stampede. The longer the query takes and the more traffic you have, the worse it gets.
The fix: let one request rebuild, make the rest wait
The standard fix is a lock. The first request to notice the missing key takes a lock and starts rebuilding. Every other request sees that a rebuild is in progress, waits briefly, then reads the fresh value from the cache. Redis locks are atomic, which makes Redis the right place for this. In Laravel:
use Illuminate\Support\Facades\Cache;
function rememberWithLock(string $key, int $ttl, callable $compute, int $maxWait = 30): mixed
{
$value = Cache::get($key);
if ($value !== null) {
return $value;
}
$lock = Cache::lock($key . ':rebuild', $maxWait);
if ($lock->acquire()) {
try {
$value = $compute();
Cache::put($key, $value, $ttl);
return $value;
} finally {
$lock->release();
}
}
// Another request is rebuilding. Wait for it, then read the fresh value.
$deadline = time() + $maxWait;
while (time() < $deadline) {
usleep(100000); // 100ms
$value = Cache::get($key);
if ($value !== null) {
return $value;
}
}
// Rebuild did not finish in time. Compute rather than hang the request.
return $compute();
}
One request holds dashboard:summary:rebuild, everyone else polls the cache for up to thirty seconds. The database runs the heavy query once, not hundreds of times.
Tuning the two numbers that matter
Two parameters decide whether this works. The lock TTL must be longer than the worst-case time your compute callback takes, or the lock expires mid-rebuild and a second request starts its own rebuild. The wait budget must be long enough to cover a normal rebuild, but short enough that a stuck rebuild does not hold hundreds of PHP workers. Waiting requests each hold a PHP-FPM worker while they sleep, so keep $maxWait modest and make sure you have the workers to cover a stampede window without starving normal traffic.
An alternative: serve stale while refreshing
If even a short wait is unacceptable, the stale-while-revalidate pattern avoids waiting entirely: keep the expired value readable for a grace period and refresh it in the background. Laravel 11 and later ships this as Cache::flexible():
$summary = Cache::flexible('dashboard:summary', [3600, 600], function () {
return ReportService::buildSummary();
});
The first array value is the fresh TTL and the second is the stale grace period. When the fresh TTL expires, requests keep receiving the stale value while the cache refreshes behind the scenes. The trade-off is real: users can see data up to ten minutes old, and it only fits workloads where slightly stale data is acceptable. For reports and dashboards it usually is.
How to tell it is working
Stampedes have a recognizable signature in monitoring: periodic latency spikes that line up exactly with cache TTL boundaries, followed by a burst of concurrent slow queries. After adding locks, watch three things. Cache hit ratio should stay high even across expiry boundaries. Database concurrent connections should stop spiking at those boundaries. And p99 response time for the cached endpoint should flatten. If you log the times the fallback path is hit, that tells you the wait budget needs tuning.
A cache stampede is not a sign your caching is wrong. It is a sign your caching is load-bearing and popular enough to need one more piece of machinery. One lock, one rebuild, everyone else reads. If the stampede already hammered your database and you need to find which query hurt the most, read how to read PostgreSQL EXPLAIN ANALYZE to find the slow query.