A cache stampede happens when:
- Cached data expires
- Multiple requests hit the backend simultaneously
- Database or downstream service gets overloaded
- System latency spikes or crashes
This usually happens under high traffic.
- Popular API endpoint
- Cache TTL = 5 minutes
- Cache expires
- 5,000 concurrent users hit the same endpoint
- All requests bypass cache
- Database load explodes
Result: High latency or outage.
Allow only one request to rebuild cache. Other requests wait or serve stale data.
Serve stale cache temporarily. Refresh cache in background.
Improves user experience under load.
Instead of fixed TTL: TTL = base + random offset
Prevents simultaneous expiration.
Protect backend during spike. Return controlled responses instead of crashing.
Design for failure. Assume traffic spikes. Assume cache will expire at worst time.
Production engineering is about prevention, not reaction.
— CodeWithIshwar