Cache stampede / thundering herd

The traffic pileup when a popular cache entry expires and many requests hit the origin together to rebuild the same thing.

site falls over when the cache expireseveryone misses the cache at the same timeone expired page sends thousands of requeststraffic spike right after a purgetoo many workers rebuilding the same thingthe database gets slammed every time the cache clearsdogpile effectcache stampeed

See it

Live demo coming soon

What it is

A cache stampede happens when many requests discover the same missing or expired entry together and all try to rebuild it. One popular page that normally costs one origin request per minute can suddenly launch a hundred identical database queries in the same millisecond. It is also called a thundering herd or dogpile.

Prevent it with request collapsing, also called single-flight: the first request refreshes the value while the rest wait for its result. Nginx spells this 'proxy_cache_lock on'; Go ships it as the singleflight package; Varnish and most CDNs do it for you on the cache path. Stale-while-revalidate lets those waiting visitors receive the old copy instead. Add random TTL jitter when many different keys would otherwise expire on the same second.

Gotcha: a lock without an expiry can turn one crashed refresher into a permanent outage, while a lock that expires too soon lets a second rebuild start beside the first. A purge-everything creates the same problem across every PoP at once, so precise purges and an origin shield still matter.

Ask AI for it

Protect this cache refill path from a thundering herd. Use Redis SET with NX and PX to acquire a short-lived per-key refresh lock, store a unique token as its value, and release it with a compare-and-delete Lua script so one worker cannot delete another's lock. Let the lock owner rebuild once while other requests serve stale data under Cache-Control: stale-while-revalidate. Add 10 percent random TTL jitter, bound the wait time, and test 100 simultaneous misses to prove only one origin rebuild runs.

You might have meant

origin shieldcache warmingcache invalidation purgestale while revalidatecache control cache ttl