0
0
Redisquery~20 mins

Cache stampede prevention in Redis - Practice Problems & Coding Challenges

Choose your learning style9 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 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?
AIt duplicates cache data across multiple servers for backup.
BIt speeds up the cache to store more data permanently.
CIt deletes all cache entries regularly to keep data fresh.
DIt prevents many requests from hitting the database simultaneously, avoiding overload.
Attempts:
2 left
💡 Hint
Think about what happens when many users ask for the same missing cache data at once.
query_result
intermediate
2: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
A'LOCK_ACQUIRED'
B'LOCK_EXISTS'
Cnil
DError: wrong number of arguments
Attempts:
2 left
💡 Hint
SETNX sets the key only if it does not exist.
📝 Syntax
advanced
2: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?
ASET cache_lock locked EX 5 NX
BSET cache_lock locked NX PX 5000
CSETNX cache_lock locked EX 5
DSET cache_lock locked PX 5000
Attempts:
2 left
💡 Hint
Look for the command that sets the key only if it does not exist and sets expiration in milliseconds.
optimization
advanced
2: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?
ADisable cache expiration and refresh data manually only.
BSet all cache keys to expire exactly at the same time for consistency.
CAdd random jitter to cache expiration times so keys expire at different moments.
DIncrease cache size to store more data permanently.
Attempts:
2 left
💡 Hint
Think about spreading out expiration times to avoid many keys expiring together.
🔧 Debug
expert
2: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?
ABecause the lock key never expires, so other clients wait forever.
BBecause SETNX is not atomic and can fail silently.
CBecause the script returns wrong values causing client confusion.
DBecause the lock key is deleted immediately after setting.
Attempts:
2 left
💡 Hint
Think about what happens if a lock is set but never removed or expired.