Challenge - 5 Problems
Cache Stampede Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
What is the main goal of cache stampede prevention?
Imagine many users request the same data at the same time, but the cache is empty or expired. What problem does cache stampede prevention solve?
Attempts:
2 left
💡 Hint
Think about what happens when many users ask for the same missing cache data at once.
✗ Incorrect
Cache stampede prevention stops many requests from querying the database at the same time when cache is empty or expired. This avoids database overload and improves performance.
❓ query_result
intermediate2:00remaining
What is the output of this Redis Lua script for cache stampede prevention?
Given this Lua script run in Redis to set a lock key for cache rebuild:
local lock_key = KEYS[1]
local lock_ttl = tonumber(ARGV[1])
if redis.call('SETNX', lock_key, 'locked') == 1 then
redis.call('PEXPIRE', lock_key, lock_ttl)
return 'LOCK_ACQUIRED'
else
return 'LOCK_EXISTS'
end
What will the script return if the lock key does not exist?
Redis
local lock_key = KEYS[1] local lock_ttl = tonumber(ARGV[1]) if redis.call('SETNX', lock_key, 'locked') == 1 then redis.call('PEXPIRE', lock_key, lock_ttl) return 'LOCK_ACQUIRED' else return 'LOCK_EXISTS' end
Attempts:
2 left
💡 Hint
SETNX sets the key only if it does not exist.
✗ Incorrect
If the lock key does not exist, SETNX sets it and returns 1, so the script returns 'LOCK_ACQUIRED'.
📝 Syntax
advanced2:00remaining
Which Redis command sequence correctly implements a cache stampede lock with expiration?
You want to set a lock key 'cache_lock' with a 5-second expiration only if it does not exist. Which option is correct?
Attempts:
2 left
💡 Hint
Look for the command that sets the key only if it does not exist and sets expiration in milliseconds.
✗ Incorrect
Option B uses SET with NX (only if not exists) and PX 5000 (expire in 5000 ms), which is the correct way to set a lock with expiration atomically.
❓ optimization
advanced2:00remaining
How can you reduce the chance of cache stampede when cache expiration times are the same for many keys?
If many cache keys expire simultaneously, many requests may hit the database at once. What is a good strategy to reduce this problem?
Attempts:
2 left
💡 Hint
Think about spreading out expiration times to avoid many keys expiring together.
✗ Incorrect
Adding random jitter to expiration times staggers cache expiry, preventing many keys from expiring simultaneously and reducing database load.
🔧 Debug
expert2:00remaining
Why does this Redis Lua script cause a deadlock in cache stampede prevention?
Consider this Lua script:
local lock_key = KEYS[1]
local lock_ttl = tonumber(ARGV[1])
if redis.call('SETNX', lock_key, 'locked') == 1 then
-- Missing expiration set here
return 'LOCK_ACQUIRED'
else
return 'LOCK_EXISTS'
end
Why can this cause a deadlock?
Attempts:
2 left
💡 Hint
Think about what happens if a lock is set but never removed or expired.
✗ Incorrect
Without setting an expiration, the lock key remains forever, blocking other clients indefinitely and causing a deadlock.