Novice tutorial: https://www.runoob.com/redis/redis-transactions.html
Shang Silicon Valley video tutorial: https://www.bilibili.com/video/BV1Rv41177Af?p=21
Redis transactions can execute multiple commands at once, and have the following three characteristics:
- There is no concept of isolation level: batch operations are put into the queue buffer before sending EXEC commands.
- 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.
- 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
- 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.
- Exec: After entering Exex, all commands in the command queue will be executed.
- discard: used to cancel the team.
Example 1: The team is successful and the submission is successful.
Example 2: Cancel the team halfway and cancel the transaction
Example 3: When the command is in the team, an error is reported, and the transaction will fail to submit
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.
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.
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.