Command Introduction
The Transactions command family is designed to encapsulate multiple commands into an atomic execution unit. It is suitable for scenarios that require atomicity for batch operations, such as inventory deduction and account balance transfers. Based on their features, its core commands can be categorized into the following two types:
Transaction Lifecycle Control
MULTI: Marks the start of a transaction, and subsequent commands enter a queued state.
EXEC: Submits and executes all commands in the transaction queue at once and in order (the execution is not interrupted by requests from other clients during this process).
DISCARD: Aborts the transaction and clears all queued commands.
Optimistic Locking Concurrency Control
WATCH: Monitors one or more keys before a transaction starts. If a monitored key is modified by another client before EXEC, the transaction is automatically aborted (returns nil), thereby implementing the CAS (Compare-And-Swap) optimistic locking mechanism.
UNWATCH: Cancels the monitoring of all keys on the current connection.
Note:
Unlike relational databases, Redis/Valkey transactions do not provide rollback capability. Their atomicity is only reflected in the execution phase: commands within a transaction are packaged and executed at once, and the execution is not interrupted by requests from other clients. If an error occurs while executing a command, subsequent commands will still be executed, and operations that have already been executed will not be rolled back.
Command Support Overview
All five commands (discard, exec, multi, unwatch, watch) are fully supported in the standard architecture of Redis versions 2.8, 4.0, 5.0, 6.2, and 7.0, as well as in the standard and cluster architectures of Valkey versions 8.0 and 9.0.
Cross-Slot Restriction: Transactions family commands do not involve cross-slot access scenarios.
Command Compatibility Matrix
The support status of Transactions family commands across different versions and architectures is shown in the table below (✓ indicates supported, ✗ indicates not supported, and — indicates that the command does not involve a cross-slot access scenario).
|
discard | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
exec | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
multi | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
unwatch | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
watch | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |