当前位置:网站首页>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 :
- 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 .
- 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
OKIn 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
OKWhen 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 .
边栏推荐
- 【深入理解TcaplusDB技术】TcaplusDB运维
- 510000 prize pool invites you to join the war! The second Alibaba cloud ECS cloudbuild developer competition is coming
- Shell编程基础(第七篇:分支语句-if)
- 【深入理解TcaplusDB技术】TcaplusDB机器如何初始化和上架
- Concordia University | volume product cycle network for reward generation in reinforcement learning
- Classic interview question: a page from entering URL to rendering process
- [deeply understand tcapulusdb technology] cluster management operation
- 【深入理解TcaplusDB技术】单据受理之创建游戏区
- 树莓派环境设置
- 【深入理解TcaplusDB技术】单据受理之建表审批
猜你喜欢
随机推荐
[compréhension approfondie de la base de connaissances tcaplusdb] déploiement de la version locale de tcaplusdb FAQ
NFT 中可能存在的安全漏洞
【深入理解TcaplusDB技术】创建游戏区
IDEA写jsp代码报错,但是正常运行解决
【深入理解TcaplusDB技术】TcaplusDB机器如何下架
An error is reported when idea writes JSP code, but it is solved by normal operation
I wrote a telnet command myself
漫话Redis源码之一百一二十
同花顺开户选哪家券商比较好?手机开户安全么?
康考迪亚大学|图卷积循环网络用于强化学习中的奖励生成
【深入理解TcaplusDB技术】运维平台中实现TcaplusDB事务管理
手把手教你IDEA创建SSM项目结构
University of Calgary | recommendation system based on Reinforcement Learning
Storage structure of graph (adjacency matrix)
Be careful with MySQL filesort
【深入理解TcaplusDB技术】入门Tcaplus SQL Driver
Possible security vulnerabilities in NFT
Fibonacci search (golden section)
【深入理解TcaplusDB技术】TcaplusDB机型管理
【深入理解TcaplusDB技术】TcaplusDB 表管理——修改表


![[deeply understand tcapulusdb technology] tcapulusdb table management - clean up table](/img/2b/3ab5e247ac103728b4d3579c3c5468.png)




