Understanding Redis Transactions and Their ACID Properties
This article explains the fundamentals of ACID, how Redis implements transactions with MULTI, EXEC, DISCARD and WATCH commands, provides code examples, and analyzes whether Redis transactions satisfy atomicity, consistency, isolation, and durability.
The article starts with a mock interview scenario about Redis transaction mechanisms and proceeds to a systematic analysis of the topic.
What is ACID?
ACID stands for Atomicity, Consistency, Isolation, and Durability, which are the four essential properties of a reliable transaction.
Atomicity : All operations in a transaction succeed or none do.
Consistency : The database remains in a valid state after the transaction.
Isolation : Concurrent transactions do not interfere with each other.
Durability : Once committed, changes survive crashes.
How Redis Implements Transactions
Redis provides a lightweight transaction model based on four commands: MULTI , EXEC , DISCARD , and WATCH . The process consists of three steps: start the transaction, queue commands, and finally execute or discard the transaction.
Explicitly Start a Transaction
The client sends MULTI to begin queuing commands without executing them immediately.
Command Queueing
Subsequent commands are stored in a queue and return QUEUED , indicating they are pending execution.
Execute or Discard
Sending EXEC runs all queued commands atomically; DISCARD clears the queue, abandoning the transaction.
Redis Transaction Examples
Normal execution:
# 开启事务
> MULTI
OK
# 定义指令
> SET "公众号:码哥字节" "粉丝 100 万"
QUEUED
> SET "order" "30"
QUEUED
> SET "文章数" 666
QUEUED
> GET "文章数"
QUEUED
# 执行事务
> EXEC
1) OK
2) OK
3) OK
4) "666"Discarding a transaction:
# 初始化订单数
> SET "order:mobile" 100
OK
# 开启事务
> MULTI
OK
# 队列命令
> DECR "order:mobile"
QUEUED
# 丢弃
> DISCARD
OK
# 验证未修改
> GET "order:mobile"
"100"Does Redis Satisfy ACID?
Atomicity
Redis aborts the entire transaction if an error occurs before EXEC , guaranteeing atomicity. However, if errors happen during EXEC , Redis still executes the valid commands, breaking atomicity.
Consistency
Consistency is maintained when the transaction is aborted before execution or when only valid commands succeed. The impact of instance failures depends on persistence settings (RDB/AOF).
Isolation
Isolation before EXEC relies on the WATCH mechanism, which aborts the transaction if watched keys change. After EXEC , Redis’s single‑threaded execution naturally preserves isolation.
Durability
Redis cannot guarantee durability for transactions regardless of the persistence mode (RDB, AOF, or none), because data may be lost on crashes or not fully logged.
Summary
Redis provides atomicity for error‑free transactions but lacks rollback.
It offers consistency concepts but durability is not guaranteed.
Isolation is ensured via WATCH before execution and single‑threaded execution after.
Overall, Redis transactions satisfy atomicity, consistency, and isolation, but not durability.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
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.