キャッシュスタンピードとThundering Herdを防ぐ
キャッシュスタンピードとは何か、人気のキーが期限切れになったときにデータベースへ過大な負荷がかかる理由、そして負荷の高い状況でもバックエンドを守るロックや更新の手法を学びます。
「キャッシュスタンピードとThundering Herdを防ぐ」はCoddyKit上の無料Redis Caching & Messaging (Pub/Sub, Streams)レッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはRedis Caching & Messaging (Pub/Sub, Streams)学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Redis Caching & Messaging (Pub/Sub, Streams)コースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
What Is a Cache Stampede?
A cache stampede (or thundering herd) happens when a popular cached key expires and many requests miss the cache at the same moment. They all rush to the database to rebuild the value, overwhelming it.
Why It Is So Dangerous
Under high traffic, hundreds or thousands of concurrent misses can hit the database in milliseconds. The backend that normally serves zero load for that key suddenly takes the full firehose, causing latency spikes or outages.
The Naive Cache-Aside Flow
The basic pattern reads the cache, and on a miss queries the DB and stores the result. Every concurrent miss runs this DB query — that is the vulnerability.
Solution 1: A Recompute Lock
Let only the first missing request rebuild the value while others wait or serve stale. Redis SET NX grants a short-lived lock to exactly one client.
SET lock:product:42 "1" NX EX 10How the Lock Flow Works
On a miss:
- Try to acquire the lock with
SET NX - If you got it, query the DB and repopulate the cache
- If not, briefly wait and re-read the cache
Only one DB query runs per expiry.
Solution 2: Stale-While-Revalidate
Store the value with a logical expiry earlier than its physical TTL. When the logical time passes, serve the stale value immediately and refresh it in the background, so users never wait on a miss.
Solution 3: Early Probabilistic Expiry
Each request, with a small growing probability as expiry nears, voluntarily refreshes the key before it dies. This spreads recomputation across time so the herd never forms.
Adding Jitter to TTLs
If many keys are written together (e.g. on deploy) they expire together, causing a synchronized stampede. Add random jitter to each TTL so expiries spread out.
SET product:42 "..." EX 305
SET product:43 "..." EX 318Releasing the Lock Safely
After rebuilding, delete the lock. Give it a short TTL too, so a crashed worker does not hold it forever and block refreshes.
DEL lock:product:42Choosing a Strategy
Guidelines:
- Lock: simplest, briefly delays some requests
- Stale-while-revalidate: best UX, needs background refresh
- Probabilistic + jitter: smooths load, no waiting
Combine them for very hot keys.
Negative Caching
A related danger is the cache penetration miss: many requests for a key that does not exist in the DB always miss the cache and hammer the backend. Cache the not-found result briefly too, so repeated lookups are absorbed.
SET product:9999 "__NULL__" EX 30Quick Check
Test your stampede-prevention knowledge.
Recap
You learned to stop cache stampedes:
- A stampede floods the DB when a hot key expires
- A
SET NXrecompute lock limits rebuilds to one client - Stale-while-revalidate serves old data while refreshing
- Probabilistic early expiry spreads recomputation out
- TTL jitter prevents synchronized mass expiry
よくある質問
「キャッシュスタンピードとThundering Herdを防ぐ」レッスンは無料ですか?
はい。「キャッシュスタンピードとThundering Herdを防ぐ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Redis Caching & Messaging (Pub/Sub, Streams)コースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Redis Caching & Messaging (Pub/Sub, Streams)コースには全4レッスンが含まれています。
「キャッシュスタンピードとThundering Herdを防ぐ」で何を学びますか?
キャッシュスタンピードとは何か、人気のキーが期限切れになったときにデータベースへ過大な負荷がかかる理由、そして負荷の高い状況でもバックエンドを守るロックや更新の手法を学びます。 ブラウザで直接実行するハンズオンコードでRedis Caching & Messaging (Pub/Sub, Streams)を演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Redis Caching & Messaging (Pub/Sub, Streams)を始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのRedis Caching & Messaging (Pub/Sub, Streams)は初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「キャッシュスタンピードとThundering Herdを防ぐ」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このRedis Caching & Messaging (Pub/Sub, Streams)レッスンでコードを書いて実行できますか?
はい。すべてのRedis Caching & Messaging (Pub/Sub, Streams)レッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- なぜキャッシュするのか?キャッシュ入門
- 基本的なキャッシュパターンの実装
- キャッシュの追い出しと有効期限
- キャッシュスタンピードとThundering Herdを防ぐ