This is the seventh day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Introduction to the

Redis transactions can execute more than one command at a time, with three important guarantees:

  • Batch operations are put into the queue cache before the EXEC command is sent.
  • After receiving the EXEC command, the transaction is executed. Any command in the transaction fails to execute, but the other commands are still executed.
  • Command requests submitted by other clients are not inserted into the sequence of transaction execution commands during transaction execution.

A transaction goes through three phases from inception to execution:

  • Start the transaction.
  • Order in.
  • Execute a transaction.

The command

DISCARD # UNWATCH # monitor all keys UNWATCH # Monitor all keys UNWATCH # Monitor all keys UNWATCH # Monitor one or more keys The transaction will be interrupted if this (or these) key is changed by another command before the transaction is executed.Copy the code

Example 1: EXEC

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:

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.

127.0.0.1:6379> set account:a 100 OK 127.0.0.1:6379> set account:b 22 OK 127.0.0.1:6379> multi OK 127.0.0.1:6379> get Account :a QUEUED 127.0.0.1:6379> get account:b QUEUED 127.0.0.1:6379> decrby Account :a 50 QUEUED 127.0.0.1:6379> incrby Account :b 50 QUEUED 127.0.0.1:6379> get account:a QUEUED 127.0.0.1:6379> get account:b QUEUED 127.0.0.1:6379> exec 1) "120" 2) "22" 3) (INTEGER) 70 4) (INTEGER) 72 5) "70" 6) "72" 127.0.0.1:6379>Copy the code

Example 2: DISCARD

Cancel the transaction, abandon execution of all commands in the transaction block.

multi

.

discard

All commands in the

127.0.0.1:6379[1]> set account:a 70 OK 127.0.0.1:6379[1]> set account:b 72 OK 127.0.0.1:6379[1]> keys * 1) "account:b" 2) "Account :a" 127.0.0.1:6379[1]> multi OK 127.0.0.1:6379[1]> set aa 234 QUEUED 127.0.0.1:6379[1]> get aa QUEUED 127.0.0.1:6379[1]> discard OK 127.0.0.1:6379[1]> keys * 1) Discard "127.0.0.1:6379[1]>Copy the code

Example 3: Transaction error handling method 1

If an error occurs when executing a command, only the command that reported the error is not executed, and all other commands are executed

Such as:

Key: the value of aa is ABC. It is a string and cannot be incrementing. When the incr command is executed, an error will be reported

 127.0.0.1:6379[1]> multi
 OK
 127.0.0.1:6379[1]> set aa abc
 QUEUED
 127.0.0.1:6379[1]> get aa
 QUEUED
 127.0.0.1:6379[1]> incr aa
 QUEUED
 127.0.0.1:6379[1]> get aa
 QUEUED
 127.0.0.1:6379[1]> exec
 1) OK
 2) "abc"
 3) (error) ERR value is not an integer or out of range
 4) "abc"
 127.0.0.1:6379[1]> 
 ​
Copy the code

Example 4: Transaction error handling 2

A command in the queue has a reporting error, and the entire queue will be cancelled when executed.

Such as:

If the inCR123 command does not exist in the queue and an error is reported, the entire command queue cannot be executed when the exec command is executed, and all commands are not executed.

127.0.0.1:6379[1]> keys * 1) "account:b" 2) "Account :a" 3) "aa" 127.0.0.1:6379[1]> multi OK 127.0.0.1:6379[1]> set bb 123 QUEUED 127.0.0.1:6379[1]> get bb QUEUED 127.0.0.1:6379[1]> incr123 (error) ERR unknown command 'incr123', with args beginning with: 127.0.0.1:6379[1]> exec (error) EXECABORT Transaction discarded because of previous errors. 127.0.0.1:6379[1]> keys * 1) "Account: b" 2) "account: a" 3) "aa" 127.0.0.1:6379 [1] >Copy the code

Example 5: WATCH

Monitor one (or more) keys, and if this (or these) keys are changed by another command before the transaction is executed (no changes are affected), then the transaction will be interrupted.