防止缓存击穿与惊群
了解缓存击穿是什么、热门密钥过期时为何会压垮数据库,以及如何使用锁和刷新技术让后端在高负载下保持安全
防止缓存击穿与惊群 是 CoddyKit 上的免费 Redis Caching & Messaging (Pub/Sub, Streams) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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
常见问题解答
「防止缓存击穿与惊群」课时是免费的吗?
是的 — 「防止缓存击穿与惊群」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Redis Caching & Messaging (Pub/Sub, Streams) 课程的其余内容,请升级到 CoddyKit PRO。 Redis Caching & Messaging (Pub/Sub, Streams) 课程共包含 4 节课。
「防止缓存击穿与惊群」这节课中我会学到什么?
了解缓存击穿是什么、热门密钥过期时为何会压垮数据库,以及如何使用锁和刷新技术让后端在高负载下保持安全 你通过在浏览器中直接运行的动手代码来练习 Redis Caching & Messaging (Pub/Sub, Streams),全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Redis Caching & Messaging (Pub/Sub, Streams) 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Redis Caching & Messaging (Pub/Sub, Streams) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「防止缓存击穿与惊群」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Redis Caching & Messaging (Pub/Sub, Streams) 课中编写并运行代码吗?
能。每节 Redis Caching & Messaging (Pub/Sub, Streams) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 为什么需要缓存?缓存简介
- 实现基本缓存模式
- 缓存淘汰与过期
- 防止缓存击穿与惊群