Transactions (MULTI/EXEC)Intermediate
1Question 1 of 1
bashHow do transactions work in Redis using MULTI, EXEC, and WATCH?
Explanation & Concept
Redis transactions are sequential and isolated. All commands inside a `MULTI` block are queued and executed atomically in a single batch upon `EXEC`.
`WATCH` provides Optimistic Concurrency Control (Check-and-Set). If any watched key is modified by another client before `EXEC` runs, the transaction aborts and returns a nil multi-bulk response.
Code Example
bash
# Optimistic locking balance transfer
WATCH account:101
val = GET account:101
# Begin atomic transaction block
MULTI
DECRBY account:101 50
INCRBY account:202 50
EXEC
# If another client modified account:101 after WATCH, EXEC returns nil and cancels!How It Works & Key Takeaways
`WATCH` avoids pessimistic locking overhead. If a collision occurs, the application simply re-reads the value and retries.
Mock Test · 1 Questions · Optional
Ready for Replication?
Take a quick test to evaluate yourself, or move straight to the next module.
Intermediate1 questions