A cache stampede happens when many requests try to fetch data from the cache at the same time, but the cache is empty or expired. This causes all requests to hit the database or backend, leading to overload.
Click to reveal answer
intermediate
Explain the 'lock' or 'mutex' method for cache stampede prevention.
This method uses a lock to allow only one request to fetch and update the cache while others wait. It prevents multiple requests from hitting the backend simultaneously.
Click to reveal answer
intermediate
What is 'early recomputation' in cache stampede prevention?
Early recomputation means refreshing the cache before it expires. This way, requests get fresh data and avoid many requests triggering backend calls at once.
Click to reveal answer
advanced
Describe the 'request coalescing' technique.
Request coalescing groups multiple requests for the same data into one backend call. The result is shared among all waiting requests, reducing load.
Click to reveal answer
intermediate
Why is random expiration time useful in cache stampede prevention?
Random expiration times spread out cache expiry moments. This avoids many caches expiring simultaneously, which can cause a stampede.
Click to reveal answer
What problem does cache stampede prevention solve?
ACache data being outdated
BToo many requests hitting backend when cache expires
CCache memory running out
DSlow network connections
✗ Incorrect
Cache stampede prevention stops many requests from hitting the backend at once when cache expires.
Which method allows only one request to update the cache while others wait?
ALock or mutex
BEarly recomputation
CRandom expiration
DRequest coalescing
✗ Incorrect
Lock or mutex ensures only one request updates the cache to prevent stampede.
What does early recomputation do?
ADeletes cache early
BGroups requests
CRefreshes cache before expiry
DLocks cache access
✗ Incorrect
Early recomputation refreshes cache before it expires to avoid many backend hits.
How does random expiration help prevent stampede?
ABy making cache expire at the same time
BBy deleting cache
CBy locking cache
DBy spreading cache expiry times
✗ Incorrect
Random expiration spreads expiry times to avoid many caches expiring simultaneously.
What is request coalescing?
AGrouping requests to one backend call
BMaking multiple backend calls
CDeleting cache early
DLocking cache access
✗ Incorrect
Request coalescing groups multiple requests into one backend call to reduce load.
Explain what causes a cache stampede and describe two methods to prevent it.
Think about what happens when cache is empty and how to control request flow.
You got /3 concepts.
Describe how request coalescing works and why it helps in cache stampede prevention.
Imagine many friends asking the same question and one person answering for all.
You got /4 concepts.
Practice
(1/5)
1. What is the main goal of cache stampede prevention in system design?
easy
A. To prevent many requests from hitting the database simultaneously
B. To increase the size of the cache storage
C. To reduce the number of cache servers
D. To encrypt cached data for security
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 A
Using distributed locks around cache refresh -> Option D
Quick Check:
Locking = stampede prevention [OK]
Hint: Look for locking or coordination methods in options [OK]
Common Mistakes:
Choosing infinite TTL which can cause stale data
Disabling cache which increases load
Ignoring locking as a prevention method
3. Consider this pseudo-code for cache stampede prevention:
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?
medium
A. Multiple requests fetch from DB simultaneously causing overload
B. Cache never updates and returns stale data
C. Requests wait indefinitely causing deadlock
D. Cache size increases exponentially
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 A
Quick Check:
No lock = DB overload [OK]
Hint: Lock prevents simultaneous DB fetch on cache miss [OK]
Common Mistakes:
Thinking cache never updates without lock
Confusing deadlock with missing lock
Assuming cache size grows without lock
4. A system uses early expiration to prevent cache stampede by setting cache TTL shorter than data TTL. However, users report stale data sometimes. What is the likely cause?
medium
A. Cache TTL is longer than data TTL causing stale reads
B. Cache is not refreshed after early expiration due to missing lock
C. Database is down causing stale cache
D. Cache size is too small causing evictions
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 B
Quick Check:
Missing lock = stale cache after early expiration [OK]
Hint: Check if locking coordinates cache refresh [OK]
Common Mistakes:
Assuming TTL mismatch causes stale data
Blaming database downtime without evidence
Confusing cache size with freshness
5. You design a high-traffic API with cache stampede prevention. Which combination best prevents stampede while ensuring fresh data and minimal DB load?
hard
A. Disable cache and use database connection pooling
B. Use long cache TTL with no locking and rely on DB scaling
C. Use short cache TTL with distributed locking and request coalescing
D. Use infinite cache TTL and manual cache invalidation
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 C
Quick Check:
Locking + coalescing + short TTL = best stampede prevention [OK]
Hint: Combine locking and coalescing with short TTL for best results [OK]
Common Mistakes:
Ignoring locking and causing DB overload
Disabling cache increases latency and load
Using infinite TTL causes stale data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.