当前位置:网站首页>Redis transaction mechanism
Redis transaction mechanism
2022-06-29 14:14:00 【Full stack programmer webmaster】
One 、Redis Orders related to the transaction :
1、MULTI:
Used to mark the opening of the transaction block .MULTI After performing ,Redis The subsequent commands will be put into a cache queue one by one , When EXEC When the command is called , All the commands in the queue will be executed atomically .
2、EXEC:
Execute all previously queued commands in a transaction , Then restore the normal connection state . When using WATCH On command , Only if the monitored key has not been modified ,EXEC Command will execute the command in the transaction .
3、DISCARD:
Give up the business , Clear the command in the transaction queue , Then restore the normal connection state . If used UNWATCH command , that DISCARD The command will cancel all keys currently connected to the monitor .
4、WATCH:
When a transaction needs to be executed conditionally , You will use this command to key Set as monitored . If this is before the transaction is executed key Altered by other orders , Then the whole business will be interrupted .WATCH The command can be used to provide CAS function .
5、UNWATCH:
Clear all monitored keys in the transaction . If called EXEC or DISCARD command , So you don't have to call it manually UNWATCH command .
Two 、Redis How things work :
1、 Definition of transaction :
Redis The essence of a transaction is a set of commands , All commands in a transaction are either executed , Either not . The principle of transaction is to send the command belonging to a transaction to Redis, Put in a queue , let Redis Execute these orders in turn . If it's sending EXEC The client is disconnected before the command , be Redis The transaction queue will be cleared , None of the commands in the transaction will execute . And once the client sends EXEC command , All orders will be executed , Even after that, it doesn't matter if the client is disconnected , because Redis All commands to be executed have been recorded in .
besides ,Redis The transaction can also ensure that the commands in a transaction are executed in turn without being inserted by other commands . in other words , During the execution of the transaction , The server will not interrupt the transaction and execute the command requests of other clients , That is, it will not be inserted by other commands , Don't jam , Wait until all commands in the transaction have been executed before processing command requests from other clients . It's one-off 、 Sequence 、 To execute a series of orders exclusively .
2、Redis The nature of transactions :
(3) Atomicity :Redis The atomicity of can only guarantee the one-time execution of batch operations , And traditional mysql The difference is ,Redis Rollback is not supported , In execution EXEC On command , If Redis The execution of a command in the transaction failed , Subsequent orders will still be executed , No rollback .
Redis Why rollback is not supported rollback?
- Redis The reason for the operation failure can only be syntax error or wrong data type operation , These are problems that can be found during development , Will not enter the production environment , Therefore, no rollback is required .
- Redis The interior design advocates simplicity and high performance , Supporting the ability to roll back transactions can lead to design complexity , This is related to Redis Against the original intention of , Therefore, rollback capability is not required .
- Redis The application scenario is obviously not designed for high reliability and strong consistency of data storage , It is designed for high performance of data access , Designers have partially abandoned atomicity for simplicity and high performance .
(2) Isolation, : A transaction is a separate isolation operation , There is no concept of isolation level , The commands in the transaction queue will not be actually executed before they are committed . In business , All commands are serialized , To execute in order . Transaction is in the process of execution , Command requests sent by other clients will not be inserted into the transaction execution command sequence .
(3) persistence : If Redis When running in a particular persistence mode , Transactions are also persistent .
3、Redis Wrong handling of transactions :
If there is an error in the execution of a command in a transaction ,Redis How will it be handled ? Answer that question , First, you need to know what causes the command execution error .
(1) Grammar mistakes :
Syntax error means that the command does not exist or the number of command parameters is incorrect . such as :
redis>MULTI
OK
redis>SET key value
QUEUED
redis>SET key
(error)ERR wrong number of arguments for 'set' command
redis> errorCOMMAND key
(error) ERR unknown command 'errorCOMMAND'
redis> EXEC
(error) EXECABORT Transaction discarded because of previous errors.With the MULTI The command was executed 3 An order : One is the right command , Successfully joined the transaction queue ; The other two commands have syntax errors . And as long as there is a syntax error in one command , perform EXEC After the command Redis It will directly return an error , Can't even execute commands with correct syntax .
Here's a little bit of attention : Redis 2.6.5 Previous versions ignored commands with syntax errors , Then execute other syntactically correct commands in the transaction . In this case ,SET key value Will be performed ,EXEC The command will return a result :1) OK.
(2) Running error :
Run error refers to an error that occurs when a command is executed , For example, using hash type commands to manipulate keys of collection type , This error occurs before actual execution Redis It's impossible to discover , So in a transaction, such an order will be Redis Accept and execute . If a command in the transaction appears Running error , Other commands in the transaction will continue to be executed ( Including the command after the error command ), Examples are as follows :
redis>MULTI
OK
redis>SET key 1
QUEUED
redis>SADD key 2
QUEUED
redis>SET key 3
QUEUED
redis>EXEC
1) OK
2) (error) ERR Operation against a key holding the wrong kind of value
3) OK
redis>GET key
"3"It can be seen that although SADD key 2 There was a mistake , however SET key 3 Still implemented .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/100029.html Link to the original text :https://javaforall.cn
边栏推荐
- Wechat applet: new and exclusive cloud development wechat group contacts
- Introduction to esp8266: three programming methods "suggestions collection"
- Cicd introduction [easy to understand]
- Introduction to reverse commissioning -pe file section table and block 03/07
- 【VEUX开发者工具的使用-getters使用】
- 微信小程序:装B神器P图修改微信流量主小程序源码下载趣味恶搞图制作免服务器域名
- Introduction to veeambackup & replication
- golang7_TCP编程
- Applet Wechat: un nouveau réseau exclusif de microgroupes de développement de Cloud
- 单端口RAM实现FIFO
猜你喜欢

微信小程序:修复采集接口版云开发表情包

Tiktok's global short video dominance may be reversed by YouTube
![[use of veux developer tools - use of getters]](/img/85/7a8d0f9d0c86eb3963db280da70049.png)
[use of veux developer tools - use of getters]

Uncover the practice of Baidu intelligent test in the field of automatic test execution

leetcode:226. Flip binary tree

吐血整理:一份不可多得的架构师图谱!
Redis fragment cluster setup and use tutorial

纳人才,谋发展 | 人大金仓喜获“最佳雇主校招案例奖”

台式机主板上保护cpu的盖子安装和拆卸

Thinkpad VMware 安装虚拟机出现此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态(问题解决方法)
随机推荐
Introduction to reverse commissioning -pe file section table and block 03/07
【VEUX开发者工具的使用-getters使用】
浅析 Istio——可观测性
内网穿透(nc)
纳人才,谋发展 | 人大金仓喜获“最佳雇主校招案例奖”
Redis的数据过期清除策略 与 内存淘汰策略
goby如何导出扫描结果
28000 word summary of callable and future interview knowledge points. After reading it, I went directly to ByteDance. Forgive me for being a little floating (Part 2)
传输层 用户数据报协议(UDP)
ES6 array method
[high concurrency] cache idea
STM32 watchdog study
现场快递柜状态采集与控制系统
Redis fragment cluster setup and use tutorial
vmware虚拟机的作用
瑞达期货可以开户吗?安全可靠吗?
布隆过滤器Bloom Filter简介
微信小程序:修复采集接口版云开发表情包
[network bandwidth] Mbps & Mbps
Leetcode question brushing: String 07 (repeated substring)