0
0
Redisquery~10 mins

Cache stampede prevention in Redis - Interactive Code Practice

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

Complete the code to set a cache key with an expiration time to prevent stale data.

Redis
redis.set('user:123', user_data, ex=[1])
Drag options to blanks, or click blank then click option'
A-1
BNone
C3600
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or negative values disables expiration, causing stale cache.
2fill in blank
medium

Complete the code to acquire a lock key before regenerating cache to prevent cache stampede.

Redis
if redis.setnx('lock:user:123', '1'):
    redis.expire('lock:user:123', [1])
    # regenerate cache
Drag options to blanks, or click blank then click option'
A10
B-5
CNone
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Not setting expiration can cause deadlocks.
3fill in blank
hard

Fix the error in the code that tries to get cached data and falls back to regeneration if missing.

Redis
data = redis.get('user:123')
if data is [1]:
    # regenerate cache
Drag options to blanks, or click blank then click option'
AFalse
B''
C0
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for False or empty string causes incorrect cache miss detection.
4fill in blank
hard

Fill both blanks to implement a safe cache regeneration with lock and expiration.

Redis
if redis.setnx('lock:user:123', '1'):
    redis.expire('lock:user:123', [1])
    data = regenerate_data()
    redis.set('user:123', data, ex=[2])
    redis.delete('lock:user:123')
Drag options to blanks, or click blank then click option'
A10
B3600
C60
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Setting lock expiration too long causes blocking.
Setting cache expiration too short causes frequent regeneration.
5fill in blank
hard

Fill all three blanks to implement cache stampede prevention with lock check, regeneration, and fallback.

Redis
data = redis.get('user:123')
if data is [1]:
    if redis.setnx('lock:user:123', '1'):
        redis.expire('lock:user:123', [2])
        data = regenerate_data()
        redis.set('user:123', data, ex=[3])
        redis.delete('lock:user:123')
    else:
        time.sleep(0.1)
        data = redis.get('user:123')
Drag options to blanks, or click blank then click option'
ANone
B10
C3600
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using False instead of None for cache miss.
Not setting lock expiration.
Setting cache expiration too short.