Getting Started with Redis - Transactions

The address of the original text is updated, and the reading effect is better!

Getting Started with Redis - Transactions | CoderMast Programming Mast icon-default.png?t=N5F7https://www.codermast.com/database/redis/redis-transaction.html

Redis transactions can execute multiple commands at once, with the following three important guarantees:

  • Bulk operations are queued before sending the EXEC command.
  • After receiving the EXEC command, it enters into transaction execution. Any command in the transaction fails to execute, and the rest of the commands are still executed.
  • During transaction execution, command requests submitted by other clients will not be inserted into the transaction execution command sequence.

A transaction goes through the following three stages from start to execution:

  • Start a business.
  • The command is enqueued.
  • Execute the transaction.

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

A transaction can be understood as a packaged batch execution script, but batch instructions are not atomic operations, and the failure of a certain instruction in the middle will not cause the rollback of previous instructions, nor will it cause subsequent instructions to not be executed.

Redis transaction commands

  • Start transaction:multi
  • Execute the transaction:exec
  • Cancel transaction:discard
  • Monitor one (or more) keys:watch key [key ...]

Monitor one (or more) keys. If the key (or keys) is changed by other commands before the transaction is executed, the transaction will be interrupted.

  • Unwatch:unwatch

Cancel the monitoring of all keys by WATCH command.

Notice

Redis transactions are different from MySQL transactions. MySQL transactions are either all executed or not executed at all. And Redis is just a set of sequential execution of a set of commands, the transaction will not be rolled back, nor will it stop with an error.

Guess you like

Origin blog.csdn.net/qq_33685334/article/details/131252572