Redis supports transactions in a distributed environment, where a transaction can execute multiple commands at once, and all commands in the transaction are executed sequentially. The transaction is not interrupted by command requests from other clients. The server does not proceed to other commands from other clients until all commands in the transaction have been executed. Redis transaction operation is divided into three stages: transaction start, command queue and transaction execution. The transaction execution flow of Redis is as follows: see figure.
- Enable transaction: The client runs Multi command to enable the transaction
- Submit request: The client submits the command to the transaction
- Task queuing: Redis places client requests in a transaction queue for execution.
- Enqueued status feedback: The server returns a QURUD indicating that the command has been queued
- Execute commands: The client executes transactions through Exec
- Transaction execution error: In a Redis transaction, if a command is executed incorrectly, other commands continue to execute without rollback. You can use Watch to monitor the status of transaction execution and handle the exception of command execution errors.
- Execution result feedback: The server returns the result of the transaction execution to the client.
Redis transaction commands are Multi, Exec, Discard, Watch, and unWatch, as shown below: