0
0
Redisquery~20 mins

Multi-key transactions for consistency in Redis - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Redis Transaction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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?
Redis
WATCH balance:alice balance:bob
MULTI
DECRBY balance:alice 50
INCRBY balance:bob 50
EXEC
Abalance:alice = 100, balance:bob = 200
Bbalance:alice = 50, balance:bob = 200
Cbalance:alice = 50, balance:bob = 250
Dbalance:alice = 100, balance:bob = 250
Attempts:
2 left
💡 Hint
Think about what MULTI and EXEC do together with WATCH.
🧠 Conceptual
intermediate
1:30remaining
Why use WATCH in Redis transactions?
What is the main purpose of the WATCH command in Redis transactions?
ATo monitor keys for changes and abort the transaction if they change
BTo automatically retry the transaction if it fails
CTo queue commands for atomic execution
DTo lock keys so no other client can read them during the transaction
Attempts:
2 left
💡 Hint
Think about how Redis detects conflicts in transactions.
📝 Syntax
advanced
2: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
A
WATCH key1 key2
MULTI
INCR key1
DECR key2
EXEC
DISCARD
B
WATCH key1 key2
MULTI
INCR key1
DECR key2
EXEC extra_arg
C
WATCH key1 key2
MULTI
INCR key1
DECR key2
EXEC
D
CEXE
2yek RCED
1yek RCNI
ITLUM
2yek 1yek HCTAW
Attempts:
2 left
💡 Hint
Check the EXEC command syntax carefully.
optimization
advanced
2: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?
AUpdate keys one by one without transactions
BUse MULTI/EXEC without WATCH and hope no conflicts occur
CUse Lua scripting to perform all updates atomically without WATCH
DUse WATCH on all keys, then MULTI/EXEC to queue updates, retry if EXEC returns null
Attempts:
2 left
💡 Hint
Consider how Redis handles optimistic locking and atomicity.
🔧 Debug
expert
3: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?
AAnother client modified balance:alice or balance:bob after WATCH but before EXEC, causing EXEC to return null
BWATCH locks the keys preventing any updates, so EXEC fails
CThe DECRBY and INCRBY commands are not atomic inside MULTI/EXEC
DRedis does not support multi-key transactions
Attempts:
2 left
💡 Hint
Think about what happens if keys change after WATCH.