English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Redis Transactions

Redis transactions can execute multiple commands at once and come with the following three important guarantees:

  • Batch operations are placed in the queue cache before sending the EXEC command.

  • After receiving the EXEC command, enter the transaction execution phase; if any command in the transaction fails to execute, the other commands will still be executed.

  • During the transaction execution process, commands submitted by other clients will not be inserted into the sequence of transaction execution commands.

A transaction goes through the following three stages from start to execution:

  • Start the transaction.

  • Command queuing.

  • Execute the transaction.

Online Example

The following is an example of a transaction, which first queues commands with MULTI Start a transaction, then enqueue multiple commands into the transaction, and finally executed by EXEC Command triggers a transaction, and all commands in the transaction are executed 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 on transactions, so the execution of Redis transactions is not atomic.

Transactions can be understood as a packaged batch execution script, but batch instructions are not atomic operations; the failure of a single instruction in the middle will not cause the rollback of the instructions executed before it, nor will it cause the subsequent instructions not to be executed.

This is the description on the official website  From redis docs on 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 operation fails, set a has already been successfully executed and will not be rolled back, and set c will continue to execute.

Redis Transaction Commands

The following table lists the commands related to Redis transactions:

Serial NumberCommand and Description
1DISCARD
Cancel the transaction, and abandon the execution of all commands within the transaction block.
2EXEC
Execute all commands within the transaction block.
3MULTI
Mark the beginning of a transaction block.
4UNWATCH
Cancel the WATCH command's monitoring of all keys.
5WATCH key [key ...]
If a key (or multiple keys) watched is changed by other commands before the transaction is executed, the transaction will be interrupted.