redis transaction and watch mechanism

Chapter 1, redis transactions and watch mechanism

1.1) redis transaction, three major commands of transaction

What is a redis transaction
? A transaction in Redis is a single isolated operation, which ensures that two or more command sets are queued and executed sequentially as needed without being interrupted by any other operations.
Redis transactions actually refer to using the idea of ​​​​transactions to implement the execution of a set of set commands. There is no concept of rollback, and there is no transaction in the strict sense.

①multi-setAutoCommoit(false)-transaction is started

Syntax: Start transaction multi

Function: Mark the start of a transaction. Through multi, subsequent commands will be put into a queue in order. When the user types exec, these instructions will be executed in sequence.
Return value: always returns ok

②exec - execution

Syntax: execute transaction exec

Function: Execute all commands within the transaction block.
Return value: Contents of all execution statements within the transaction. If the transaction is interrupted (affected), nil is returned.


③discard -cancel

Syntax: cancel transaction discard

Function: Cancel the transaction. If you enter several commands after opening multi, and then type discard, all previous commands will be canceled.
Return value: always returns ok

# 开启事务
127.0.0.1:6379> MULTI
OK
# 输入两个或者两个以上命令
127.0.0.1:6379(TX)> set k1 v1
QUEUED
127.0.0.1:6379(TX)> set k2 v2
QUEUED
# 执行命令
127.0.0.1:6379(TX)> EXEC
1) OK
2) OK
127.0.0.1:6379> keys *
1) "k1"
2) "k2"

1.2) Redis transaction errors and rollback situations


① An error occurred during team formation. The error is known to redis and all instructions in the transaction will be invalid.
Insert image description here

127.0.0.1:6379> MULTI
OK
127.0.0.1:6379(TX)> set k1 v1
QUEUED
127.0.0.1:6379(TX)> set k2 v2
QUEUED
127.0.0.1:6379(TX)> set k33
(error) ERR wrong number of arguments for 'set' command
127.0.0.1:6379(TX)> set k4 v4
QUEUED
127.0.0.1:6379(TX)> exec
(error) EXECABORT Transaction discarded because of previous errors.
127.0.0.1:6379> keys *
(empty array)


② Runtime error: Because the error is unknown, redis must execute to know the error, and redis has no error rollback mechanism and will continue to execute subsequent instructions and be effective.
Insert image description here

127.0.0.1:6379> MULTI
OK
127.0.0.1:6379(TX)> set k1 v1
QUEUED
127.0.0.1:6379(TX)> INCR k1
QUEUED
127.0.0.1:6379(TX)> set k2 v2
QUEUED
127.0.0.1:6379(TX)> EXEC
1) OK
2) (error) ERR value is not an integer or out of range
3) OK
127.0.0.1:6379> keys *
1) "k1"
2) "k2"
127.0.0.1:6379>

1.3) watch mechanism

① watch monitors the value of one or several keys. If the value of any key is modified before transaction EXEC is executed, the transaction will be interrupted.
Return value: always returns ok

Syntax: watch key [key …]


②Cancel the monitoring of all keys by the WATCH command. If after executing the WATCH command, the EXEC command or the DISCARD command is executed first, then there is no need to execute UNWATCH again. Return
value: always returns ok

Syntax: unwatch


③Example:
Start the server redis-server, and then open two client connections. They are called A client (red) and B client (yellow).
Insert image description here
Insert image description here
1) Set key on client A: str.lp and the number of logins is 10.
2) Monitor key on client A: str.lp
3) Open transaction multi on client A
4) Modify the value of str.lp on client A as 11
5) Modify the value of str.lp on client B to 15
6) Execute transaction exec on client A
7) Check the value of str.lp on client A. The transaction executed by client A is not committed because WATCH's str. The value of lp has been modified, so the transaction is aborted.
Insert image description here

Guess you like

Origin blog.csdn.net/baomingshu/article/details/132180599