What if millions of users crash your system just because your cache expired at the same time?
Why Cache stampede prevention in HLD? - Purpose & Use Cases
Imagine a popular website where thousands of users request the same data at the same time. Without any special handling, all these requests hit the database simultaneously when the cache expires.
This causes the database to overload, slowing down responses or even crashing the system. Manually trying to handle this by limiting requests or adding delays is complex and often fails under heavy load.
Cache stampede prevention techniques ensure that when cached data expires, only one request fetches fresh data while others wait or get stale data. This keeps the system stable and fast without overwhelming the database.
if cache expired: fetch data from DB update cache return cache data
if cache expired: if lock acquired: fetch data from DB update cache release lock else: wait for cache update return cache data
It enables systems to handle huge traffic spikes smoothly without crashing or slowing down.
A news website updating its homepage data every few minutes can serve millions of users without database overload by preventing cache stampedes.
Manual cache refresh causes database overload under high traffic.
Cache stampede prevention lets only one request refresh cache at a time.
This keeps systems fast, stable, and scalable during traffic spikes.
Practice
Solution
Step 1: Understand cache stampede problem
Cache stampede happens when many requests try to fetch data from the database because the cache expired or missed.Step 2: Identify the goal of prevention
The goal is to stop all these requests from hitting the database at once to avoid overload.Final Answer:
To prevent many requests from hitting the database simultaneously -> Option AQuick Check:
Cache stampede prevention = prevent database overload [OK]
- Confusing cache size with stampede prevention
- Thinking encryption is related to stampede
- Assuming reducing cache servers helps stampede
Solution
Step 1: Review common stampede prevention methods
Techniques include locking, early expiration, and request coalescing to control cache refresh.Step 2: Match technique to options
Distributed locks prevent multiple processes from refreshing cache simultaneously, stopping stampede.Final Answer:
Using distributed locks around cache refresh -> Option DQuick Check:
Locking = stampede prevention [OK]
- Choosing infinite TTL which can cause stale data
- Disabling cache which increases load
- Ignoring locking as a prevention method
if cache miss:
acquire lock
if cache still miss:
data = fetch from DB
update cache
release lock
return cached data
What happens if the lock is not used here?Solution
Step 1: Analyze the role of the lock
The lock ensures only one request fetches data from DB and updates cache on miss.Step 2: Consequence of missing lock
Without lock, many requests detect cache miss and all fetch from DB, causing overload.Final Answer:
Multiple requests fetch from DB simultaneously causing overload -> Option AQuick Check:
No lock = DB overload [OK]
- Thinking cache never updates without lock
- Confusing deadlock with missing lock
- Assuming cache size grows without lock
Solution
Step 1: Understand early expiration technique
Early expiration refreshes cache before data TTL ends to avoid stampede.Step 2: Identify cause of stale data
If cache refresh is not coordinated (missing lock), cache may not update properly, causing stale data.Final Answer:
Cache is not refreshed after early expiration due to missing lock -> Option BQuick Check:
Missing lock = stale cache after early expiration [OK]
- Assuming TTL mismatch causes stale data
- Blaming database downtime without evidence
- Confusing cache size with freshness
Solution
Step 1: Evaluate each option for stampede prevention
Use long cache TTL with no locking and rely on DB scaling risks DB overload without locking; C disables cache increasing DB load; D risks stale data.Step 2: Identify best practice combination
Short TTL triggers refresh often; distributed locking prevents multiple refreshes; request coalescing merges requests to reduce DB hits.Final Answer:
Use short cache TTL with distributed locking and request coalescing -> Option CQuick Check:
Locking + coalescing + short TTL = best stampede prevention [OK]
- Ignoring locking and causing DB overload
- Disabling cache increases latency and load
- Using infinite TTL causes stale data
Ready to prove it?
Pass the test and this pattern moves unknown
