0
0
Redisquery~20 mins

WATCH for optimistic locking in Redis - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Redis WATCH Mastery
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 with WATCH?
Consider the following Redis commands executed in sequence:

1. SET balance 100
2. WATCH balance
3. GET balance (returns 100)
4. MULTI
5. DECRBY balance 50
6. EXEC

Assuming no other client modifies balance between WATCH and EXEC, what will be the value of balance after EXEC?
Redis
SET balance 100
WATCH balance
GET balance
MULTI
DECRBY balance 50
EXEC
A50
B100
CEXEC returns null and balance remains 100
DSyntax error due to wrong command order
Attempts:
2 left
💡 Hint
WATCH monitors keys for changes; if no changes occur, EXEC applies queued commands.
query_result
intermediate
2:00remaining
What happens if a key changes after WATCH but before EXEC?
Given these commands:

1. SET stock 10
2. WATCH stock
3. Another client executes DECR stock
4. MULTI
5. DECR stock
6. EXEC

What will EXEC return and what is the value of stock?
Redis
SET stock 10
WATCH stock
-- another client DECR stock
MULTI
DECR stock
EXEC
AEXEC returns null and stock is 9
BEXEC returns 8 and stock is 8
CEXEC returns 9 and stock is 9
DEXEC returns error due to key change
Attempts:
2 left
💡 Hint
If a watched key changes, EXEC aborts and returns null.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this Redis transaction using WATCH
Which option contains a syntax error that will cause the Redis transaction to fail?
Redis
WATCH key1
MULTI
INCR key1
EXEC
AWATCH followed by MULTI and then EXEC is correct
BCalling EXEC before MULTI
CUsing INCR inside MULTI without WATCH
DUsing WATCH after MULTI
Attempts:
2 left
💡 Hint
WATCH must be called before MULTI to monitor keys.
optimization
advanced
2:00remaining
How to minimize transaction aborts with WATCH in Redis?
You have a high contention key 'counter' updated by many clients using WATCH and MULTI. Which approach reduces transaction aborts best?
AUse longer transactions with multiple commands inside MULTI
BIncrease the number of keys watched to detect conflicts earlier
CUse Lua scripts to perform atomic increments instead of WATCH
DRemove WATCH and rely on optimistic concurrency without monitoring
Attempts:
2 left
💡 Hint
Lua scripts run atomically without needing WATCH.
🧠 Conceptual
expert
3:00remaining
Why does Redis WATCH provide optimistic locking?
Select the best explanation for how WATCH implements optimistic locking in Redis.
AWATCH queues commands and executes them atomically without conflicts
BWATCH monitors keys and aborts transactions if keys change, allowing safe retries
CWATCH locks keys exclusively preventing other clients from modifying them
DWATCH duplicates keys to a temporary store for isolated transactions
Attempts:
2 left
💡 Hint
Optimistic locking means detecting conflicts rather than preventing them.