Cache Stampede¶
A cache stampede, also known as thundering herd or dog-piling, is a failure mode that occurs when many concurrent requests simultaneously discover that a cached value has expired or is missing. All requests then attempt to regenerate the cache at the same time, flooding the backend (database, API, or computation layer) with redundant work. This is especially destructive under high traffic because the backend, already under load, receives a sudden spike just when it is needed most.
Common mitigation strategies include: locking, where only one process regenerates the cache while others wait or serve stale data, probabilistic early expiration, where the cache refreshes slightly before it expires, based on a probability, and background revalidation, which returns stale data immediately while refreshing asynchronously.
<?php
// Naive approach — prone to cache stampede
$value = $cache->get('key');
if ($value === null) {
// All concurrent requests reach here simultaneously on expiry
$value = expensiveComputation();
$cache->set('key', $value, ttl: 60);
}
// Lock-based mitigation
$value = $cache->get('key');
if ($value === null) {
$lock = $cache->lock('key:lock', ttl: 5);
if ($lock->get()) {
try {
$value = expensiveComputation();
$cache->set('key', $value, ttl: 60);
} finally {
$lock->release();
}
} else {
// Wait for the lock holder to populate the cache
$lock->block(4);
$value = $cache->get('key');
}
}
?>
See also Cache stampede (Wikipedia), Preventing Cache Stampede with PHP and Redis, Laravel Cache Locks and XFetch: optimal probabilistic cache stampede prevention.
Related : Cache, Time To Live (TTL), Race Condition, , Lock, , Cache Stampede, Rate Limiting, Availability, Reliability
Related packages : symfony/lock, malkusch/lock, php-cache/stampede-protection, symfony/cache, ronnylt/redlock-php