Databases 9 min read

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.

ITPUB
ITPUB
ITPUB
Mastering Redis Transactions: Commands, CAS Locks, and Common Pitfalls

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.

Redis transaction basic commands
Redis transaction basic commands

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) 90

Nested 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 nested

Discarding 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
OK

WATCH and Optimistic Locking (CAS)

If another client modifies a watched key before EXEC, the transaction aborts. WATCH implements a compare‑and‑swap (CAS) mechanism.

Redis WATCH example
Redis WATCH example

Usage: WATCH key1 key2 …; MULTI; …; EXEC. If any watched key changed, EXEC returns nil. UNWATCH cancels the watch.

CAS principle
CAS principle

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 value

Why 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.”
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

databaseredisCASTransactionswatch
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.