Cache stampede: popular key expires simultaneously for all requests. Every one hits the DB. DB dies. Users see errors. Multiple techniques prevent it.

Req 1Req 2Req 3…1000 reqsExpired key!Lockingone refreshesXFetchprobabilisticStale-while-revalidateBackground refreshDB survivessingle query
Cache stampede: many solutions to prevent thundering-herd on expiry
Advertisement

The problem

Popular key expires. 1000s of concurrent requests see cache miss. All query DB. DB CPU spikes. Cascading failure.

The problem

Popular key expires. 1000s of concurrent requests see cache miss. All query DB. DB CPU spikes. Cascading failure.

Advertisement

Locking (single-flight)

First miss acquires a lock. Others wait. Winner writes to cache. Waiters see fresh value. Redis SETNX + wait pattern.

XFetch (probabilistic)

Refresh probability increases as expiry approaches. Some requests refresh early. Others still hit cache. Smoothes load.

Stale-while-revalidate

Serve stale value while background job fetches fresh. HTTP header + CDN pattern. Users never see miss.

Background refresh

Never let popular keys expire. Refresh proactively before TTL. Cost: continuous background load.

Lock + probabilistic + stale-serve + background refresh. Combine as needed. Never let hot keys expire cold.