Mastering Redis Transactions: Commands, CAS Locks, and Common Pitfalls
This article explains why Redis offers transactions, introduces the five core transaction commands, demonstrates practical examples including WATCH and CAS optimistic locking, and discusses error handling and the reasons Redis does not support rollback.
Why Use Transactions
Redis commands like GET, SET, MGET, and MSET are atomic individually, but when multiple operations must be executed as an indivisible unit, a transaction is needed. For example, transferring money between two accounts requires both debit and credit to succeed together.
Basic Transaction Commands
Redis provides five core commands: MULTI, EXEC, DISCARD, WATCH, and UNWATCH.
Practical Example
Scenario: two accounts each have 100 units; one transfers 10 units to the other.
# Initialize accounts
127.0.0.1:6379> set tuoluo 100
OK
127.0.0.1:6379> set zhaocai 100
OK
# Start transaction
127.0.0.1:6379> MULTI
OK
# Increment and decrement
127.0.0.1:6379(TX)> INCRBY tuoluo 10
QUEUED
127.0.0.1:6379(TX)> DECRBY zhaocai 10
QUEUED
# Execute
127.0.0.1:6379(TX)> EXEC
1) (integer) 110
2) (integer) 90Nested Transactions
Redis does not support nested MULTI; attempting a second MULTI inside an active transaction returns an error.
# First transaction
127.0.0.1:6379> MULTI
OK
# Attempt nested
127.0.0.1:6379(TX)> MULTI
(error) ERR MULTI calls can not be nestedDiscarding a Transaction
Calling DISCARD clears the queued commands and exits the transaction.
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379(TX)> DISCARD
OKWATCH and Optimistic Locking (CAS)
If another client modifies a watched key before EXEC, the transaction aborts. WATCH implements a compare‑and‑swap (CAS) mechanism.
Usage: WATCH key1 key2 …; MULTI; …; EXEC. If any watched key changed, EXEC returns nil. UNWATCH cancels the watch.
Error Handling
Errors Before EXEC
Syntax or argument errors during queuing cause the whole transaction to be aborted with EXECABORT.
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379(TX)> SET tuoluo
(error) ERR wrong number of arguments for 'set' command
127.0.0.1:6379(TX)> EXEC
(error) EXECABORT Transaction discarded because of previous errors.Errors After EXEC
Runtime type errors (e.g., applying a list command to a string) cause only the offending command to fail; other commands may still succeed, breaking atomicity.
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379(TX)> SET tuoluo 100
QUEUED
127.0.0.1:6379(TX)> LPOP tuoluo
QUEUED
127.0.0.1:6379(TX)> EXEC
1) OK
2) (error) WRONGTYPE Operation against a key holding the wrong kind of valueWhy Redis Does Not Support Rollback
According to the official documentation, Redis only aborts a transaction on syntax or type errors, which are assumed to be rare in production. Skipping rollback simplifies the implementation and keeps Redis fast.
“It’s the programmer’s fault, not Redis’s.”
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
