Redis transaction can execute multiple commands once, and with two important guarantees:
It is placed in a batch operation before sending queue buffer EXEC command.
After receiving EXEC command to enter the transaction execution, transaction arbitrary command execution fails, the rest of the command is still being executed.
During the execution of a transaction, other command request submitted by the client will not be inserted into the transaction execution command sequence.
From start to execute a transaction will go through the following three stages:
Begin a transaction.
Command into the team.
The enforcement branch.
Examples
The following is an example of a transaction, it is the first to MULTI begin a transaction, then multiple commands into teams to the transaction, the transaction is triggered by the last EXEC command, together with all the commands in the transaction:
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"
Redis execute a single command is atomic, but Redis without adding any mechanism for maintaining atomicity in transaction, so the implementation of Redis transaction is not atomic.
Transactions can be understood execute the script as a packaged volume, but the bulk of the instruction is not atomic operation, the middle will not lead to the failure of an instruction previously done instruction rollback, it will not cause subsequent instructions do.
这是官网上的说明 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.
such as:
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 it fails in the set b bbb place, set a success would not have been rolled back, set c will continue.
Redis transaction command
The following table lists the commands for redis matters:
Number and description of command
1DISCARD
Cancel the transaction, giving up all the commands in a transaction block.
2EXEC
Execute commands within blocks of all transactions.
3MULTI
Mark the beginning of a transaction block.
4UNWATCH
Cancel WATCH command to monitor all the key.
5WATCH key [key ...]
A monitor (or more) key, if this (or these) key changes to other orders are executed before the transaction, then the transaction will be interrupted.
(Editor: Leilin Peng Source: network intrusion deleted)