防范惊群效应
了解热门键过期时缓存击穿是如何发生的,以及如何通过请求合并、锁、提前重新计算和带抖动的 TTL 来防止这种情况。
防范惊群效应 是 CoddyKit 上的免费 Caching Strategies: Redis + CDN + Edge Computing 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Caching Strategies: Redis + CDN + Edge Computing 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Caching Strategies: Redis + CDN + Edge Computing 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
The Thundering Herd Problem
When a hot cache key expires, every concurrent request misses at once and rushes the origin together. This cache stampede (or thundering herd) can overwhelm the database in an instant.
Why It Is Dangerous
A single popular item serving 10,000 requests/second normally hits the cache. The moment it expires, those 10,000 requests all hit the database simultaneously, often causing a spike that takes the origin down.
Cause: Synchronized Expiry
The root cause is many keys (or many requests on one key) expiring at the same moment. The fix strategies all aim to spread or serialize the resulting recomputation.
Fix 1: Request Coalescing
Let only the first request recompute the value; everyone else waits for that result. This is also called single-flight.
in_flight = {}
def get(key, compute):
if key in in_flight:
return 'waiting for in-flight result'
in_flight[key] = True
return compute()
print(get('hot', lambda: 'computed once'))Fix 2: Mutex Lock
Use a distributed lock (e.g. a Redis key with NX) so only one process recomputes. Others briefly serve stale data or retry after a short wait.
lock = None
def acquire_lock(holder):
global lock
if lock is None:
lock = holder
return True
return False
print(acquire_lock('worker-1'))
print(acquire_lock('worker-2'))Fix 3: Jittered TTL
Add randomness to each entry's TTL so they do not all expire together. A base TTL plus random jitter spreads recomputation over time.
import random
base_ttl = 300
jitter = random.randint(0, 60)
print('TTL for this entry:', base_ttl + jitter, 'seconds')Fix 4: Early Recomputation
Refresh a value before it expires. When an entry is close to its TTL, a background task (or a probabilistic check) recomputes it so it never actually goes cold for users.
Probabilistic Early Expiration
A clever trick: as a key nears expiry, give each request a small, growing probability of recomputing early. One lucky request refreshes the value while others still serve the cached copy.
import random
time_left = 5
beta = 1.0
should_refresh = random.random() < (1 / max(time_left, 1)) * beta
print('Refresh early?', should_refresh)Fix 5: Serve Stale While Revalidating
Return the expired value immediately while a background job fetches fresh data. Users get a fast (slightly stale) response and the origin sees only one refresh request.
Combining Defenses
Real systems layer these: jittered TTLs to avoid synchronized expiry, plus coalescing or a lock to serialize the inevitable misses, plus stale-while-revalidate for the best user experience.
Watch for Cache Penetration Too
A related issue is penetration: requests for keys that never exist always miss and hit the origin. Cache negative results (or use a bloom filter) so missing keys are also absorbed.
Quick Check
Which technique prevents a cache stampede by ensuring only one request recomputes the value while the rest wait for that result?
Recap
You learned to defend against the thundering herd:
- Stampedes happen when hot keys expire and many requests miss at once.
- Coalescing and locks serialize recomputation.
- Jittered TTLs and early recomputation spread the load.
- Stale-while-revalidate keeps responses fast.
Combine these to keep your origin safe under load.
用 AI 导师学习 Caching Strategies: Redis + CDN + Edge Computing — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 48
常见问题解答
「防范惊群效应」课时是免费的吗?
是的 — 「防范惊群效应」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Caching Strategies: Redis + CDN + Edge Computing 课程的其余内容,请升级到 CoddyKit PRO。 Caching Strategies: Redis + CDN + Edge Computing 课程共包含 4 节课。
「防范惊群效应」这节课中我会学到什么?
了解热门键过期时缓存击穿是如何发生的,以及如何通过请求合并、锁、提前重新计算和带抖动的 TTL 来防止这种情况。 你通过在浏览器中直接运行的动手代码来练习 Caching Strategies: Redis + CDN + Edge Computing,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Caching Strategies: Redis + CDN + Edge Computing 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Caching Strategies: Redis + CDN + Edge Computing 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「防范惊群效应」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Caching Strategies: Redis + CDN + Edge Computing 课中编写并运行代码吗?
能。每节 Caching Strategies: Redis + CDN + Edge Computing 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。