4 Ways to Fix Cache Stampede
Machine-readable: Markdown · JSON API · Site index
Описание видео
Your cache just expired and ten THOUSAND requests hit your database at once. That's a cache stampede, and most engineers only know one way to fix it.
Let's go through four approaches, fast.
First, cache locking. A lock lets one request rebuild the cache while everyone else waits in line. Simple to implement, especially if you're already on Redis. The downside of this is if that rebuild is slow or fails, you've got thousands of requests timing out.
Another approach is to collapse requests instead of queueing them. Every duplicate in-flight request gets folded into one upstream call, and everyone gets the same result back. Cloudflare does this at the CDN layer. Even if a million users hit the same key, your backend sees at most one request per app server.
We can also rely on randomness. As a cache entry ages toward its TTL, each request has a small, growing chance of triggering a background refresh. At minute 50 of a 60-minute TTL, maybe 1% of requests refresh it. At minute 59, maybe 20%. Refreshes spread out naturally.
The last is background refresh. A dedicated worker proactively recomputes hot keys before they expire. Zero stampede risk. The tradeoff is you need to know what's hot ahead of time. Ticketmaster event pages are a perfect fit. Random traffic spikes are not.
Start with request coalescing. It's the easiest win and works at the CDN or app layer with no TTL changes. Add probabilistic early refresh for unpredictable spikes. Use background refresh only when you can predict demand. #shorts