当前位置:网站首页>Multi transactions in redis

Multi transactions in redis

2022-06-22 20:20:00 Fenglibin

One 、 summary

Redis Medium Multi and Pipleline Can execute multiple commands at once , however Pipeline Just put more redis Instructions are sent out together ,redis There is no guarantee of the order in which these instructions are executed , And reduce the overhead of multiple network transmission , Therefore, its execution efficiency is very high ;Multi Equivalent to one redis Of transaction, Ensure the order of the whole operation , adopt watch these key, Can avoid these key It is modified by other commands during the execution of the transaction , As a result, the result is not what is expected .

The official introduction MULTI 、 EXEC 、 DISCARD and WATCH yes Redis Transaction related orders , Transactions can execute more than one command at a time , But it has to meet 2 Conditions :

  1.     A transaction is a separate isolation operation : All commands in the transaction are serialized 、 To execute in order . Transaction is in the process of execution , Will not be interrupted by command requests from other clients .
  2.     A transaction is an atomic operation : The commands in the transaction are either all executed , Or none of it . Execution and success are 2 A concept , It is not a failure or an error , Others fail .redis Yes for transactions Partial support . If there is a submission error at the beginning of syntax, etc , Equivalent to java None of our compilers can pass , Then it must not be implemented at all . If an error is reported during execution , It has been fully implemented , But whoever reports a mistake will find someone , Other normally executed releases . each takes what he needs ! Not all the transactions here are successful , All or nothing , All execution and all success ( Or they all failed ) yes 2 A concept .

Two 、 Command Introduction

  • MULTI: Open transaction , Always returns OK
  • EXEC: Commit transaction
  • DISCARD: Give up the business ( Abandon commit execution )
  • WATCH: monitor
  • QUEUED: Add the command to the execution queue

3、 ... and 、 Demonstration of examples

1、 Open transaction

You can see that even if the transaction is started , The correct commands in the transaction are also executed , Incorrect command not executed , Whoever makes mistakes is responsible .

2、 Add... To some commands watch

increase watch command , It can be ensured that watch Of key During the execution of the transaction , If it is modified by other connections , Then the current transaction will be executed exec Times wrong , The transaction was interrupted , All commands in the transaction will not be executed .

1) Right name is t1 Of key increase watch

127.0.0.1:6379> watch t1
OK
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set t1 t111
QUEUED
127.0.0.1:6379> set t2 v222
QUEUED
127.0.0.1:6379> set t3 v333
QUEUED
127.0.0.1:6379> exec
(nil)
127.0.0.1:6379> get t1
"v111"
127.0.0.1:6379> get t2
"v2"
127.0.0.1:6379> get t3
"t33"

At this time, in the execution interval of the transaction , That is, when multi Not implemented after exec Before , Other connections have been modified t1 The following operations of :

127.0.0.1:6379> set t1 v111
OK

In execution exec When you give an order, you will report (nil) Such a wrong result , Indicates that the current transaction failed to execute . Through subsequent get Command view t1、t2 and t3 Value , Only t1 The value of has changed , Is modified by other connections , None of the commands in the current transaction have been executed .

2) Right name is t2 Of key increase watch

127.0.0.1:6379> watch t2
OK
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set t1 t111
QUEUED
127.0.0.1:6379> set t2 v222
QUEUED
127.0.0.1:6379> set t3 v333
QUEUED
127.0.0.1:6379> exec
(nil)
127.0.0.1:6379> get t1
"v111"
127.0.0.1:6379> get t2
"v222"
127.0.0.1:6379> get t3
"t33"
127.0.0.1:6379> unwatch
OK

In the transaction interval , Other connection pairs t2 It's been modified :

127.0.0.1:6379> set t2 v222
OK

When executing the transaction commit command exec when , Findings were watch Of t2 It was modified , Current transaction execution failed , adopt get Command view , Only t2 The value of has changed , Is modified by other connections , All commands of the current transaction have not been executed .

These two increases are right key Conduct watch An example of , Demonstrated being watch Of key It can be any... In a transaction key, As long as it is watch Of key Be modified , The whole transaction will not be executed .

原网站

版权声明
本文为[Fenglibin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221835236343.html