Transactions in redis

Novice tutorial: https://www.runoob.com/redis/redis-transactions.html
Shang Silicon Valley video tutorial: https://www.bilibili.com/video/BV1Rv41177Af?p=21

redis transaction

Redis transactions can execute multiple commands at once, and have the following three characteristics:
  1. There is no concept of isolation level: batch operations are put into the queue buffer before sending EXEC commands.
  2. Atomicity is not guaranteed: After receiving the EXEC command, it enters the transaction execution. Any command in the transaction fails, and the rest of the commands are still executed.
  3. Separate isolation operation: During the transaction execution process, the command requests submitted by other clients will not be inserted into the transaction execution command sequence.

A transaction will go through the following three stages from inception to execution:

  • Start business
  • Order to join
  • Execute transaction

Redis transaction commands Multi, Exec, discard

  1. Multi: similar to begin in mysql to open things, used to open redis transactions. Starting from the input of the Multi command, the input commands will be entered into the command queue in order to form a team, but will not be executed.
  2. Exec: After entering Exex, all commands in the command queue will be executed.
  3. discard: used to cancel the team.
Insert picture description here

Example 1: The team is successful and the submission is successful.

Insert picture description here

Example 2: Cancel the team halfway and cancel the transaction

Insert picture description here


Example 3: When the command is in the team, an error is reported, and the transaction will fail to submit

Insert picture description here

Example 4: The commands entered into the team are correct, but some commands are executed incorrectly. For example, the character pass type value does not support the +1 operation, but it will not affect the execution of other commands. This is the difference from the mysql transaction rollback.

Insert picture description here

Redis transaction error handling

If a report error occurs in a command in the team, all the queues will be cancelled when it is executed.

Insert picture description here

If a command reports an error during the execution phase, only the command that reports the error will not be executed, and other commands will be executed without rollback.

Insert picture description here