“This is the 14th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge.”

1. Transaction overview

Nature of the transaction

The execution of a single Redis command is atomic, but Redis does not add any mechanism to the transaction to maintain atomicity, so the execution of a Redis transaction is not atomic.

A transaction can be understood as a packaged batch execution script, but a batch instruction is not an atomized operation, and the failure of an intermediate instruction does not result in a rollback of previously done instructions, nor does it cause subsequent instructions to not be done.

In conclusion, a Redis transaction is the one-time, sequential, exclusive execution of a sequence of commands in a queue.

Transaction execution process

Redis provides transactions that package multiple commands and execute them once in a first-in, first-out (FIFO) order. There is no interruption during execution (during the execution of the transaction, the command request submitted by other clients is not inserted into the transaction execution command sequence), and the transaction will not end until all the commands in the transaction queue have been executed (whether successfully or failed).

Start a transaction with MULTI, then queue multiple commands into the transaction, and finally trigger the transaction with the EXEC command, executing all the commands in the transaction:

  1. Open transaction (MULTI)
  2. The command team
  3. Execute a transaction (EXEC)

Redis transaction command

The following table lists the commands associated with redis transactions:

The serial number Command and Description
1 DISCARD Cancellations a transaction or abandons all commands in a transaction block.
2 EXEC executes all commands within a transaction block.
3 MULTI marks the start of a transaction block.
4 UNWATCH Cancels the WATCH command to monitor all keys.
5 WATCH key key … Monitor one (or more) keys. If the key is changed by another command before the transaction is executed, the transaction will be interrupted.

2. Execute the transaction

Normal execution transaction

127.0.0.1:6379> set k2 v2 QUEUED 127.0.0.1:6379> k2 v2 QUEUED 127.0.0.1:6379> Get k2 QUEUED 127.0.0.1:6379> get k2 QUEUED 127.0.0.1:6379> set k3 v3 queue 127.0.0.1:6379> EXEC 3) "v1" 4) "v2" 5) OKCopy the code

All commands are not executed directly in a transaction; they are executed only when an EXEC is initiated.

Aborted transaction

The DISCARD command is used to cancel a transaction and DISCARD all commands in a transaction block.

DISCARD OK 127.0.0.1:6379> set K1 v1 QUEUED 127.0.0.1:6379> set k2 v2 QUEUED 127.0.0.1:6379> DISCARD OKCopy the code

3. Transaction exception

Compiled exception

Compilation exception (syntax error), compilation cannot pass, and all commands in the transaction are not executed.

127.0.0.1:6379> MULTI OK 127.0.0.1:6379> set k1 v1 QUEUED 127.0.0.1:6379> set k2 v2 QUEUED 127.0.0.1:6379> getSet k3 v33 QUEUED 127.0.0.1:6379> getset k2 # ERR wrong number of arguments for 'getset' command 127.0.0.1:6379> Set k4 v4 QUEUED 127.0.0.1:6379> EXEC (error) EXECABORT Transaction discarded because of previous errors. 127.0.0.1:6379 > get k1 (nil)Copy the code

Runtime exception

Runtime exceptions are command logic errors, but they pass at compile time

Runtime exception Logical error commands will not be executed, other commands are executed normally

127.0.0.1:6379> MULTI OK 127.0.0.1:6379> set k1 v1 QUEUED 127.0.0.1:6379> incr k1 # V2 QUEUED 127.0.0.1:6379> set k3 v3 QUEUED 127.0.0.1:6379> EXEC # 1) OK 2) (error) ERR value is not an integer or 3) OK 4) OK 127.0.0.1:6379> mget k1 k2 k3 1) "v1" 2) "v2" 3) "v3"Copy the code

4. Monitor transactions

The Redis Watch command is used to monitor one (or more) keys. If the key is changed by another command before the transaction is executed, the transaction will be interrupted.

Execute and monitor transactions normally

127.0.0.1:6379> Set money 100 # simulated account balance OK 127.0.0.1:6379> set out 0 # simulated expenditure amount OK 127.0.0.1:6379> Watch money # monitor money OK 127.0.0.1:6379> MULTI OK 127.0.0.1:6379> DECRBY money 20 # 127.0.0.1:6379> INCRBY out 20 # 127.0.0.1:6379> EXEC # execute transaction, transaction normal execute 1) (INTEGER) 80 2) (INTEGER) 20Copy the code

The key changed before the simulated transaction was executed

1, monitor money, put instructions in the transaction queue, do not execute first:

 127.0.0.1:6379> watch money
 OK
 127.0.0.1:6379> MULTI
 OK
 127.0.0.1:6379> DECRBY money 10
 QUEUED
 127.0.0.1:6379> INCRBY out 10
 QUEUED
Copy the code

Open a redis client and change the value of the key ‘money’ :

127.0.0.1:6379> get money "80" 127.0.0.1:6379> Set money 1000 OK 127.0.0.1:6379> get moneyCopy the code

3, execute transaction, transaction execution failure:

 127.0.0.1:6379> watch money
 OK
 127.0.0.1:6379> MULTI
 OK
 127.0.0.1:6379> DECRBY money 10
 QUEUED
 127.0.0.1:6379> INCRBY out 10
 QUEUED
 127.0.0.1:6379> EXEC
 (nil)
Copy the code

The Watch directive is similar to an optimistic lock. If the value of any of the multiple keys monitored by watch has been changed by another client when a transaction is committed, the transaction queue will not be executed when EXEC is used to execute the transaction, and a Nullmulti-Bulk reply is returned to notify the caller that the transaction has failed.

unwatch

The Unwatch command is used to disable the WATCH command to monitor all keys.

 127.0.0.1:6379> watch money
 OK
 127.0.0.1:6379> unwatch
 OK
Copy the code

\