Challenge - 5 Problems
Redis WATCH Mastery
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 with WATCH?
Consider the following Redis commands executed in sequence:
1.
2.
3.
4.
5.
6.
Assuming no other client modifies
1.
SET balance 1002.
WATCH balance3.
GET balance (returns 100)4.
MULTI5.
DECRBY balance 506.
EXECAssuming 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
Attempts:
2 left
💡 Hint
WATCH monitors keys for changes; if no changes occur, EXEC applies queued commands.
✗ Incorrect
Since no other client changed 'balance' after WATCH, the transaction succeeds and DECRBY reduces balance from 100 to 50.
❓ query_result
intermediate2:00remaining
What happens if a key changes after WATCH but before EXEC?
Given these commands:
1.
2.
3. Another client executes
4.
5.
6.
What will EXEC return and what is the value of
1.
SET stock 102.
WATCH stock3. Another client executes
DECR stock4.
MULTI5.
DECR stock6.
EXECWhat will EXEC return and what is the value of
stock?Redis
SET stock 10
WATCH stock
-- another client DECR stock
MULTI
DECR stock
EXECAttempts:
2 left
💡 Hint
If a watched key changes, EXEC aborts and returns null.
✗ Incorrect
Because another client changed 'stock' after WATCH, EXEC aborts and returns null. The stock value is 9 after the other client's DECR.
📝 Syntax
advanced2: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
Attempts:
2 left
💡 Hint
WATCH must be called before MULTI to monitor keys.
✗ Incorrect
WATCH must be called before MULTI. Calling WATCH after MULTI is invalid and causes a syntax error.
❓ optimization
advanced2: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?
Attempts:
2 left
💡 Hint
Lua scripts run atomically without needing WATCH.
✗ Incorrect
Lua scripts execute atomically, avoiding transaction aborts caused by WATCH conflicts.
🧠 Conceptual
expert3:00remaining
Why does Redis WATCH provide optimistic locking?
Select the best explanation for how WATCH implements optimistic locking in Redis.
Attempts:
2 left
💡 Hint
Optimistic locking means detecting conflicts rather than preventing them.
✗ Incorrect
WATCH detects if keys change after being watched and aborts the transaction to avoid conflicts, enabling optimistic concurrency control.