Redis Transactions
# Redis Transactions
Redis transactions allow multiple commands to be executed at once, with the following three important guarantees:
* Batch operations are queued in a buffer before the EXEC command is sent.
* After receiving the EXEC command, the transaction is executed. If any command within the transaction fails, the remaining commands are still executed.
* During transaction execution, command requests submitted by other clients are not inserted into the transaction's command sequence.
A transaction goes through the following three stages from start to execution:
* Starting the transaction.
* Queuing commands.
* Executing the transaction.
* * *
## Example
Here is an example of a transaction. It starts with the **MULTI** command to begin a transaction, then queues multiple commands into the transaction, and finally uses the **EXEC** command to trigger the transaction, executing all commands within it together:
redis 127.0.0.1:6379> MULTI
OK
redis 127.0.0.1:6379> SET book-name "Mastering C++ in 21 days"
QUEUED
redis 127.0.0.1:6379> GET book-name
QUEUED
redis 127.0.0.1:6379> SADD tag "C++" "Programming" "Mastering Series"
QUEUED
redis 127.0.0.1:6379> SMEMBERS tag
QUEUED
redis 127.0.0.1:6379> EXEC
1) OK
2) "Mastering C++ in 21 days"
3) (integer) 3
4) 1) "Mastering Series"
2) "C++"
3) "Programming"
The execution of a single Redis command is atomic, but Redis does not add any mechanism to maintain atomicity for transactions. Therefore, the execution of a Redis transaction is not atomic.
A transaction can be understood as a packaged batch execution script, but the batch commands are not atomic operations. The failure of a command in the middle will not cause a rollback of the commands already executed, nor will it prevent subsequent commands from being executed.
> **This is the explanation from the official Redis documentation on (http://redis.io/topics/transactions):**
>
>
> It's important to note that even when a command fails, all the other commands in the queue are processed β Redis will not stop the processing of commands.
For example:
redis 127.0.0.1:7000> multi
OK
redis 127.0.0.1:7000> set a aaa
QUEUED
redis 127.0.0.1:7000> set b bbb
QUEUED
redis 127.0.0.1:7000> set c ccc
QUEUED
redis 127.0.0.1:7000> exec
1) OK
2) OK
3) OK
If the `set b bbb` command fails, the already successful `set a` will not be rolled back, and `set c` will still continue to execute.
* * *
## Redis Transaction Commands
The following table lists the commands related to Redis transactions:
| No. | Command & Description |
| --- | --- |
| 1 | (#) Cancels the transaction, abandoning the execution of all commands within the transaction block. |
| 2 | (#) Executes all commands within the transaction block. |
| 3 | (#) Marks the beginning of a transaction block. |
| 4 | (#) Cancels the WATCH command's monitoring of all keys. |
| 5 | [WATCH key [key ...]](#) Monitors one (or more) keys. If this (or these) key(s) are modified by other commands before the transaction is executed, the transaction will be aborted. |
YouTip