Redis transaction operations

affairs

Transaction: A complete logical process (instruction set) consisting of a series of database operations

Four characteristics of transactions in Mysql 1.
Atomicity (atomicity): All operations in a transaction (transaction) are either completed or not completed, and will not end in a certain link in the middle. If an error occurs during the execution of the transaction, it will be restored (Rollback) to the state before the transaction started, as if the transaction was never executed.
2. Consistency: Before the transaction starts and after the transaction ends, the integrity of the database is not destroyed. This means that the written data must fully comply with all the preset rules, including the accuracy of the data, the concatenation and the subsequent database can spontaneously complete the predetermined work.
3.Isolation: The ability of the database to allow multiple concurrent transactions to read, write and modify its data at the same time. Isolation can prevent data inconsistency due to cross execution when multiple transactions are executed concurrently. Transaction isolation is divided into different levels, including read uncommitted (Read uncommitted), read committed (read committed), repeatable read (repeatable read) and serializable (Serializable).
4.Durability: After the transaction is completed, the modification of the data is permanent, even if the system fails, it will not be lost.

Three major characteristics of redis transactions
1. One-time: refers to the fact that the transaction in Redis is executed only once. After the execution of the transaction ends, the life cycle of the transaction ends, and the transaction needs to be reopened when the transaction is executed again.
2. Sequential: refers to all the commands in the Redis transaction, which are executed in sequence.
3. Exclusivity: When a transaction operates on a certain data, other transactions or other commands cannot operate on this data.

execute transaction

normal execution

start a transaction

multi

command enqueue

这一步可以任意输入命令,命令不会立即执行,而是入队

commit transaction

exec

Cancel transaction execution

cancel transaction

discard

Transaction compile time exception

如果提交的是错误指令,会报错但是也会入队,之后整个事务都不会执行

runtime exception

运行时异常的命令任然可以入队,执行事务时没有异常的指令正常执行,该指令执行失败返回错误

redis optimistic locking

Locks in redis:
pessimistic locks: think that errors can occur at any time, so
locks are locked at any time It is enough to judge whether anyone has modified this data during this period.

Implement optimistic locking

watch key

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324140876&siteId=291194637