Bird
Raised Fist0
HLDsystem_design~20 mins

Cache stampede prevention in HLD - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Cache Stampede Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the primary cause of a cache stampede?

Imagine many users request the same data at the same time, but the cache for that data has expired. What causes a cache stampede in this scenario?

AThe database is down and cannot respond to any requests.
BThe cache server crashes due to overload from unrelated requests.
CMultiple requests simultaneously miss the cache and all try to fetch data from the database at once.
DThe cache data is corrupted and cannot be read by any request.
Attempts:
2 left
💡 Hint

Think about what happens when many requests try to get data that is not in the cache.

❓ Architecture
intermediate
2:00remaining
Which design best prevents cache stampede by allowing only one request to refresh the cache?

Choose the architecture pattern that ensures only one request fetches fresh data from the database while others wait for the cache to be updated.

ADisable caching entirely to avoid stale data.
BUse a distributed lock or mutex around cache refresh operations.
CAllow all requests to fetch data from the database simultaneously.
DUse a random delay before each request tries to refresh the cache.
Attempts:
2 left
💡 Hint

Think about how to coordinate requests so only one updates the cache at a time.

❓ scaling
advanced
2:00remaining
How does 'early recomputation' help in cache stampede prevention?

Consider a cache system that refreshes data before it expires. What is the main benefit of this approach?

AIt reduces the chance that many requests find the cache expired simultaneously.
BIt increases the cache expiration time to avoid frequent refreshes.
CIt disables caching for high-traffic data to prevent stale reads.
DIt forces all requests to bypass the cache and go to the database.
Attempts:
2 left
💡 Hint

Think about how refreshing cache early affects request timing.

❓ tradeoff
advanced
2:00remaining
What is a tradeoff when using 'request coalescing' to prevent cache stampede?

Request coalescing groups multiple requests for the same data to a single database fetch. What is a downside of this approach?

AIt can increase latency for some requests waiting for the first to complete.
BIt causes the cache to expire more quickly than normal.
CIt requires disabling the cache for all requests.
DIt duplicates database queries for each request.
Attempts:
2 left
💡 Hint

Consider what happens to requests that wait for others to finish fetching data.

❓ estimation
expert
2:00remaining
Estimate the maximum database load during a cache stampede for 10,000 simultaneous requests with no prevention.

If 10,000 users request the same expired cache key at the same time, and each cache miss triggers one database query, what is the maximum number of database queries generated?

A1 query
B1,000 queries
C100 queries
D10,000 queries
Attempts:
2 left
💡 Hint

Think about how many requests miss the cache and query the database simultaneously.

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

  1. 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.
  2. Step 2: Identify the goal of prevention

    The goal is to stop all these requests from hitting the database at once to avoid overload.
  3. Final Answer:

    To prevent many requests from hitting the database simultaneously -> Option A
  4. Quick Check:

    Cache stampede prevention = prevent database overload [OK]
Hint: Focus on reducing database hits during cache misses [OK]
Common Mistakes:
  • Confusing cache size with stampede prevention
  • Thinking encryption is related to stampede
  • Assuming reducing cache servers helps stampede
2. Which of the following is a common technique used to prevent cache stampede?
easy
A. Storing data only in the database
B. Increasing cache TTL to infinite
C. Disabling cache for all requests
D. Using distributed locks around cache refresh

Solution

  1. Step 1: Review common stampede prevention methods

    Techniques include locking, early expiration, and request coalescing to control cache refresh.
  2. Step 2: Match technique to options

    Distributed locks prevent multiple processes from refreshing cache simultaneously, stopping stampede.
  3. Final Answer:

    Using distributed locks around cache refresh -> Option D
  4. 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

  1. Step 1: Analyze the role of the lock

    The lock ensures only one request fetches data from DB and updates cache on miss.
  2. Step 2: Consequence of missing lock

    Without lock, many requests detect cache miss and all fetch from DB, causing overload.
  3. Final Answer:

    Multiple requests fetch from DB simultaneously causing overload -> Option A
  4. 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

  1. Step 1: Understand early expiration technique

    Early expiration refreshes cache before data TTL ends to avoid stampede.
  2. Step 2: Identify cause of stale data

    If cache refresh is not coordinated (missing lock), cache may not update properly, causing stale data.
  3. Final Answer:

    Cache is not refreshed after early expiration due to missing lock -> Option B
  4. 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

  1. 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.
  2. Step 2: Identify best practice combination

    Short TTL triggers refresh often; distributed locking prevents multiple refreshes; request coalescing merges requests to reduce DB hits.
  3. Final Answer:

    Use short cache TTL with distributed locking and request coalescing -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard