May 16, 2021 Redis
Redis transactions can execute multiple commands at once with two important guarantees:
A transaction goes through three stages from start to execution:
The following is an example of a transaction that starts with MULTIPLE, then teams multiple commands into the transaction, and finally triggers the transaction with the EXEC command, executing 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"
The following table lists the commands related to the redis transaction:
Serial number | Commands and descriptions |
---|---|
1 |
DISCARD
Cancel the transaction and discard all commands within the transaction block. |
2 |
Exec
Executes commands within all transaction blocks. |
3 |
MULTI
Marks the beginning of a transaction block. |
4 |
UNWATCH
Cancel the watch command's monitoring of all keys. |
5 |
WATCH key [key ...]
Monitor one (or more) keys, and if this (or these) keys are altered by other commands before the transaction executes, the transaction is interrupted. |