0
0
HLDsystem_design~10 mins

Cache stampede prevention in HLD - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define the cache key for storing user data.

HLD
cache_key = "user_data_[1]"
Drag options to blanks, or click blank then click option'
Arequest_id
Buser_id
Ctimestamp
Dsession_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using session_id or request_id which are not stable identifiers for user data.
2fill in blank
medium

Complete the code to check if the cache has expired before fetching fresh data.

HLD
if cache_timestamp [1] current_time:
Drag options to blanks, or click blank then click option'
A<
B>
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or == which do not correctly detect expiration.
3fill in blank
hard

Fix the error in the code to acquire a lock before refreshing the cache.

HLD
if cache_miss:
    if cache_lock.[1]():
        refresh_cache()
Drag options to blanks, or click blank then click option'
Await
Bunlock
Cacquire
Drelease
Attempts:
3 left
💡 Hint
Common Mistakes
Using release or unlock before acquiring the lock.
4fill in blank
hard

Fill both blanks to implement cache stampede prevention using a lock and cache update.

HLD
if cache_expired:
    if cache_lock.[1]():
        data = fetch_fresh_data()
        cache_store.[2](cache_key, data)
Drag options to blanks, or click blank then click option'
Aacquire
Bdelete
Cset
Drelease
Attempts:
3 left
💡 Hint
Common Mistakes
Using delete instead of set to update cache.
5fill in blank
hard

Fill all three blanks to implement a cache with expiration, lock, and fallback to stale data.

HLD
if cache_data is None or cache_timestamp [1] current_time:
    if cache_lock.[2]():
        fresh_data = fetch_data()
        cache_store.[3](cache_key, fresh_data)
    else:
        use_stale_cache()
Drag options to blanks, or click blank then click option'
A<
Bacquire
Cset
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' for expiration check.
Not acquiring lock before cache update.