A cache stampede happens when many requests try to get the same data from the cache at the same time, but the data is missing or expired, causing all requests to hit the database simultaneously.
Click to reveal answer
beginner
How does using a lock help prevent cache stampede?
A lock allows only one request to fetch and update the cache while others wait, preventing many requests from hitting the database at once.
Click to reveal answer
intermediate
What is the 'early recomputation' strategy in cache stampede prevention?
Early recomputation means refreshing the cache before it expires, so requests always get fresh data and avoid many cache misses at once.
Click to reveal answer
intermediate
Explain the 'request coalescing' technique.
Request coalescing groups multiple requests for the same missing cache data so only one fetches from the database, and others wait for the result.
Click to reveal answer
intermediate
What Redis feature can be used to implement locking for cache stampede prevention?
Redis supports commands like SET with NX and EX options to create locks that expire automatically, helping to implement distributed locks.
Click to reveal answer
What causes a cache stampede?
AOnly one request accesses the cache
BCache data is too large
CCache is never updated
DMany requests miss the cache and hit the database simultaneously
✗ Incorrect
A cache stampede happens when many requests miss the cache at the same time and all query the database.
Which Redis command option helps create a lock that only sets if the key does not exist?
AEXPIRE NX
BGET EX
CSET NX
DDEL NX
✗ Incorrect
SET NX sets the key only if it does not exist, useful for locking.
What is the main benefit of early recomputation in cache management?
ADeletes cache faster
BAvoids many cache misses at once
CIncreases database load
DPrevents cache from storing data
✗ Incorrect
Early recomputation refreshes cache before expiry to avoid many misses simultaneously.
Request coalescing means:
ACombining multiple requests to fetch data once
BDeleting cache entries quickly
CIgnoring cache misses
DStoring duplicate cache data
✗ Incorrect
Request coalescing groups requests so only one fetches data, reducing load.
Why use locks in cache stampede prevention?
ATo allow only one request to update cache at a time
BTo block all requests permanently
CTo delete cache data randomly
DTo increase cache size
✗ Incorrect
Locks ensure only one request updates cache, preventing many database hits.
Describe what a cache stampede is and why it is a problem.
Think about what happens when many users ask for the same data but the cache is empty.
You got /4 concepts.
Explain two methods to prevent cache stampede using Redis.
Consider how Redis commands can help control access and timing of cache updates.
You got /4 concepts.
Practice
(1/5)
1. What is the main goal of cache stampede prevention in Redis?
easy
A. To allow multiple processes to update cache simultaneously
B. To increase the size of the cache automatically
C. To delete all cache entries periodically
D. To stop many requests from hitting the database at the same time
Solution
Step 1: Understand cache stampede problem
Cache stampede happens when many requests try to refresh expired cache simultaneously, causing database overload.
Step 2: Identify the goal of prevention
Prevention aims to allow only one process to refresh cache, reducing database hits.
Final Answer:
To stop many requests from hitting the database at the same time -> Option D
Quick Check:
Cache stampede prevention = stop many DB hits [OK]
Hint: Cache stampede means many requests hit DB; prevention stops this [OK]
2. Which Redis command syntax correctly sets a lock with expiration to prevent cache stampede?
easy
A. SET key value NX PX 3000
B. SET key value XX EX 3000
C. GET key NX PX 3000
D. DEL key NX PX 3000
Solution
Step 1: Recall Redis SET options for locking
SET with NX sets key only if it does not exist; PX sets expiration in milliseconds.
Step 2: Identify correct syntax
SET key value NX PX 3000 uses SET with NX and PX correctly to create a lock with 3000ms expiry.
Final Answer:
SET key value NX PX 3000 -> Option A
Quick Check:
SET with NX and PX = lock with expiry [OK]
Hint: Use SET with NX and PX for lock with expiry [OK]
Common Mistakes:
Using GET instead of SET for locking
Using XX instead of NX option
Using DEL command for locking
3. Given this Redis Lua script snippet for cache stampede prevention:
local lock = redis.call('SET', KEYS[1], '1', 'NX', 'PX', 5000) if not lock then return redis.error_reply('Lock not acquired') end -- refresh cache here redis.call('DEL', KEYS[1]) return 'Cache refreshed'
What will be the output if the lock key already exists?
medium
A. Lock not acquired
B. nil
C. Syntax error
D. 'Cache refreshed'
Solution
Step 1: Understand SET with NX behavior
SET with NX returns OK if key set, nil if key exists already.
Step 2: Check script logic when lock not acquired
If lock is nil (not acquired), script returns redis.error_reply('Lock not acquired').
Final Answer:
Lock not acquired -> Option A
Quick Check:
Lock exists -> error reply 'Lock not acquired' [OK]
Hint: If SET NX fails, script returns 'Lock not acquired' error [OK]
Common Mistakes:
Assuming script returns 'Cache refreshed' always
Expecting nil instead of error reply
Thinking script has syntax error
4. You wrote this Redis command to prevent cache stampede:
SET lock_key 1 PX NX 5000
But it does not work as expected. What is the problem?
medium
A. The value '1' is invalid for locking
B. The order of PX and NX options is incorrect
C. SET command does not support NX option
D. PX option requires seconds, not milliseconds
Solution
Step 1: Check Redis SET command option order
Redis requires SET options in order: NX before PX or PX before NX is accepted, but best practice is NX PX.
Step 2: Identify issue with given command
Given command uses PX before NX, which Redis does not accept; correct order is NX PX.
Final Answer:
The order of PX and NX options is incorrect -> Option B
Quick Check:
Correct option order is NX PX [OK]
Hint: Use NX before PX in SET command options [OK]
Common Mistakes:
Thinking value '1' is invalid
Believing SET does not support NX
Confusing PX milliseconds with seconds
5. You want to prevent cache stampede using Redis locks. Which approach correctly handles the scenario where the cache refresh takes longer than the lock expiration time?
hard
A. Do not use PX expiration and rely on manual DEL of lock key only
B. Set a very short PX expiration to release lock quickly and retry immediately
C. Set a longer PX expiration than the expected refresh time and renew the lock if needed
D. Use GETSET command to overwrite lock without expiration
Solution
Step 1: Understand lock expiration vs refresh time
If lock expires before refresh finishes, another process may acquire lock causing duplicate refresh.
Step 2: Choose approach to avoid premature lock expiration
Setting PX longer than refresh time and renewing lock prevents other processes from acquiring lock too early.
Final Answer:
Set a longer PX expiration than the expected refresh time and renew the lock if needed -> Option C
Quick Check:
Lock expiry > refresh time + renew if needed [OK]
Hint: Lock expiry must cover refresh duration; renew lock if needed [OK]
Common Mistakes:
Setting too short PX causing multiple refreshes
Not using PX expiration causing stale locks
Using GETSET which overwrites lock unsafely
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.