Challenge - 5 Problems
Redis Transaction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this Redis transaction?
Consider the following Redis commands executed as a transaction:
WATCH balance:alice balance:bob
MULTI
DECRBY balance:alice 50
INCRBY balance:bob 50
EXEC
Assuming initial balances are balance:alice = 100 and balance:bob = 200, what will be the balances after EXEC if no other client modifies these keys?
WATCH balance:alice balance:bob
MULTI
DECRBY balance:alice 50
INCRBY balance:bob 50
EXEC
Assuming initial balances are balance:alice = 100 and balance:bob = 200, what will be the balances after EXEC if no other client modifies these keys?
Redis
WATCH balance:alice balance:bob MULTI DECRBY balance:alice 50 INCRBY balance:bob 50 EXEC
Attempts:
2 left
💡 Hint
Think about what MULTI and EXEC do together with WATCH.
✗ Incorrect
The WATCH command monitors keys for changes. Since no other client modifies the keys, EXEC runs the queued commands atomically, decreasing Alice's balance by 50 and increasing Bob's by 50.
🧠 Conceptual
intermediate1:30remaining
Why use WATCH in Redis transactions?
What is the main purpose of the WATCH command in Redis transactions?
Attempts:
2 left
💡 Hint
Think about how Redis detects conflicts in transactions.
✗ Incorrect
WATCH monitors keys and if any change before EXEC, the transaction aborts to avoid inconsistent updates.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this Redis transaction script
Which option contains a syntax error that will cause the Redis transaction to fail?
Redis
WATCH key1 key2 MULTI INCR key1 DECR key2 EXEC
Attempts:
2 left
💡 Hint
Check the EXEC command syntax carefully.
✗ Incorrect
EXEC does not accept any arguments. Adding extra arguments causes a syntax error.
❓ optimization
advanced2:30remaining
How to optimize multi-key updates for consistency in Redis?
You want to atomically update multiple keys in Redis and ensure no other client modifies them during your update. Which approach is best?
Attempts:
2 left
💡 Hint
Consider how Redis handles optimistic locking and atomicity.
✗ Incorrect
WATCH monitors keys for changes, MULTI/EXEC queues commands atomically, and retrying on EXEC null ensures consistency.
🔧 Debug
expert3:00remaining
Why does this Redis transaction fail to update balances consistently?
Given this Redis transaction:
WATCH balance:alice balance:bob
MULTI
DECRBY balance:alice 50
INCRBY balance:bob 50
EXEC
Sometimes, the balances are not updated as expected. What is the most likely cause?
WATCH balance:alice balance:bob
MULTI
DECRBY balance:alice 50
INCRBY balance:bob 50
EXEC
Sometimes, the balances are not updated as expected. What is the most likely cause?
Attempts:
2 left
💡 Hint
Think about what happens if keys change after WATCH.
✗ Incorrect
If keys change after WATCH, EXEC returns null and the transaction does not apply updates, causing inconsistent balances.