4 Ways to Fix Cache Stampede
1:44

4 Ways to Fix Cache Stampede

Hello Interview - SWE Interview Preparation 06.05.2026 30 070 просмотров 1 358 лайков

Machine-readable: Markdown · JSON API · Site index

Поделиться Telegram VK Бот
Транскрипт Скачать .md
Анализ с AI
Описание видео
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

Оглавление (1 сегментов)

Segment 1 (00:00 - 01:00)

Your cash just expired and 10,000 requests hit your database at once. That's a cash stampede and most engineers only know one way to fix it. Let's go through four approaches. First, cash locking. A lock lets one request rebuild the cash while everyone else waits in line. Simple to implement, especially if you've already got Redis in your system. The downside of this is that if the rebuild is slow or fails, you've got thousands of requests that are going to be timing out. Another approach is to collapse requests instead of queuing 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 back-end sees at most one request per app server. We can also rely on randomness. As a cash entry ages toward its TTL, each request has a small but growing chance of triggering a background refresh. So, at minute 50 of a 60-minute TTL, maybe 1% of requests refresh it. At minute 59, maybe 20%. Refresh is spread out naturally. The last is a background refresh. A dedicated worker proactively recomputes hot keys before they expire. This means zero stampede risk. The trade-off is you need to know what's hot ahead of time. Ticketmaster event pages are a perfect fit, but random traffic spikes are not. So, start with request coalescing. It's the easiest win and works at the CDN or app layer with no TTL changes. You can add a probabilistic early refresh for unpredictable spikes. Use background refresh only when you can predict demand. Like and follow us for more system design tips.

Другие видео автора — Hello Interview - SWE Interview Preparation

Ctrl+V

Экстракт Знаний в Telegram

Экстракты и дистилляты из лучших YouTube-каналов — сразу после публикации.

Подписаться

Дайджест Экстрактов

Лучшие методички за неделю — каждый понедельник