当前位置:网站首页>Redis transactions
Redis transactions
2022-06-27 08:17:00 【Little moon 6】
Redis The business of
What is it? :
Official website :https://redis.io/topics/transactions
You can execute more than one command at a time , The essence is a set of commands . All commands in a transaction are serialized , Serializing execution in sequence without being inserted by other commands
Can be seen as , In a queue , Disposable 、 Sequence 、 To execute a series of orders exclusively
It should be noted that , Even if a command fails , All other commands in the queue are processed ,Redis Does not stop processing commands . To put it bluntly, it is not atomic ;
How do you play? :
| Serial number | Command and description |
|---|---|
| 1 | DISCARD Cancel the business , Give up executing all commands within the transaction block . |
| 2 | EXEC Execute commands within all transaction blocks . Once implemented exec All the previously added monitoring locks will be cancelled (watch Monitored key) |
| 3 | MULTI Mark the beginning of a transaction block . |
| 4 | UNWATCH Cancel WATCH Command to all key Surveillance . |
| 5 | WATCH key [key ...] Watch one ( Or more ) key , If before the transaction is executed ( Or these ) key Altered by other orders , Then the business will be interrupted . |
example 1: Normal execution
multi Start a transaction ,exec Execute all commands within the transaction , stay exec All previous operations have not been submitted , Cannot access when opening a new thread window
127.0.0.1:6379> keys *
1) "k2"
2) "k1"
127.0.0.1:6379> flushall
OK
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set k1 v1
QUEUED
127.0.0.1:6379> set k2 v2
QUEUED
127.0.0.1:6379> get k1
QUEUED
127.0.0.1:6379> exec
1) OK
2) OK
3) "v1"
127.0.0.1:6379> get k2
"v2"
example 2: Give up execution
multi Start a transaction ,discard Discard all commands within the transaction , All commands within a transaction are invalid
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set k2 xiaoming
QUEUED
127.0.0.1:6379> discard
OK
127.0.0.1:6379> get k2
"v2"
127.0.0.1:6379>
example 3: All sit together
multi Start a transaction , Just during compilation ( Grammatically ) There is one who reports an error , Then all commands in this transaction will fail to commit
127.0.0.1:6379> keys *
1) "k3"
2) "k2"
3) "k1"
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set k1 vvvvv
QUEUED
127.0.0.1:6379> set k4 v4
QUEUED
127.0.0.1:6379> set k5 v5
QUEUED
127.0.0.1:6379> setsetset k6 v6
(error) ERR unknown command 'setsetset'
127.0.0.1:6379> exec
(error) EXECABORT Transaction discarded because of previous errors.
127.0.0.1:6379> keys *
1) "k3"
2) "k2"
3) "k1"
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379>
example 4: Enemy creditor
multi Start a transaction , Just during compilation ( Grammatically ) No report error , Then the command in this transaction , The runtime error command will fail , Other commands will still succeed
127.0.0.1:6379> keys *
1) "k3"
2) "k2"
3) "k1"
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set k22 v22
QUEUED
127.0.0.1:6379> get k4
QUEUED
127.0.0.1:6379> incr k1
QUEUED
127.0.0.1:6379> set k33 v33
QUEUED
127.0.0.1:6379> exec
1) OK
2) (nil)
3) (error) ERR value is not an integer or out of range
4) OK
127.0.0.1:6379> keys *
1) "k2"
2) "k3"
3) "k22"
4) "k33"
5) "k1"
127.0.0.1:6379> get k22
"v22"
127.0.0.1:6379>
therefore , Strictly speaking ,redis It doesn't support transactions , Or some support transactions
watch monitor :
example 1: Normal execution
127.0.0.1:6379> get k22
"v22"
127.0.0.1:6379> set balance 100
OK
127.0.0.1:6379> set debt 0
OK
127.0.0.1:6379> WATCH balance
OK
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> decrby balance 20
QUEUED
127.0.0.1:6379> incrby debt 20
QUEUED
127.0.0.1:6379> EXEC
1) (integer) 80
2) (integer) 20
127.0.0.1:6379>
example 2: Abnormal modification
Is in the process of a pair watch balance when , Process II ( Or threads ) Yes balance It's been modified , Then the transaction will fail

Thread one
127.0.0.1:6379> WATCH balance
OK
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> decrby balance 20
QUEUED
127.0.0.1:6379> incrby debt 20
QUEUED
127.0.0.1:6379> EXEC
(nil)
127.0.0.1:6379> clear
127.0.0.1:6379> get balance
"800"
127.0.0.1:6379>
Thread two
127.0.0.1:6379> get balance
"80"
127.0.0.1:6379> set balance 800
OK
127.0.0.1:6379>
unwatch Cancel
As mentioned above Watch one ( Or more ) key , If before the transaction is executed ( Or these ) key Altered by other orders , Then the business will be interrupted
127.0.0.1:6379> watch balance
OK
127.0.0.1:6379> set balance 500
OK
127.0.0.1:6379> unwatch
OK
127.0.0.1:6379> get balance
"500"
127.0.0.1:6379> watch balance
OK
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set balance 80
QUEUED
127.0.0.1:6379> set debt 20
QUEUED
127.0.0.1:6379> exec
1) OK
2) OK
127.0.0.1:6379> get balance
"80"
127.0.0.1:6379>
Watch summary :
- Watch Instructions , Like optimistic lock , Transaction commit , If Key The value of has been changed by another client , For example, a certain list Has been used by another client push/pop After that , The entire transaction queue will not be executed
- adopt WATCH The command monitors multiple... Before the transaction executes Keys, If in WATCH And then there's anything Key The value of has changed ,EXEC All transactions executed by the command will be discarded , At the same time return to Nullmulti- bulk Reply to notify the caller that the transaction failed
summary :
In general, the transaction is divided into three phases :
Turn on : With MULTI Start a transaction
The team : Queue multiple orders into a transaction , These orders will not be executed immediately , It is put in the transaction queue waiting for execution
perform : from EXEC Command triggers transaction
Three characteristics :
1、 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、 There is no concept of isolation level : The commands in the queue are not actually executed until they are submitted , Because no instruction is actually executed before the transaction is committed , There is no such thing as ” The query in a transaction needs to see the update in the transaction , Query outside the transaction cannot see " This is a big headache .
3、 There is no guarantee of atomicity : redis The same thing If a command fails to execute in the service , Subsequent orders will still be executed , No rollback
边栏推荐
猜你喜欢

All tutor information on one page

并发编程JUC的AQS底层源码

What is a magnetic separator?
![[batch dos-cmd command - summary and summary] - how to distinguish the internal command and external command of CMD, and the difference between CMD command and run (win+r) command,](/img/d1/b7e70e6f0be8e128f38afbe7ac927c.png)
[batch dos-cmd command - summary and summary] - how to distinguish the internal command and external command of CMD, and the difference between CMD command and run (win+r) command,

SIG associé: toutes les routes mènent à ue5

Linux下Redis的安装

(笔记)Anaconda-Navigator闪退解决方法

二叉树结构以及堆结构基础
![[13. number and bit operation of 1 in binary]](/img/53/024f9742d1936fe6f96f4676cea00e.png)
[13. number and bit operation of 1 in binary]

lvgl 说明3关于lvgl guider的使用
随机推荐
Redis配置文件详解
Install Jenkins
What are the specialties of database system engineers?
After working in a large factory for ten years with an annual salary of 400000 yuan, I was suddenly laid off. If the company wanted to abandon you, it wouldn't leave any kindness
100%弄明白5种IO模型
无论LCD和OLED显示技术有多好,都无法替代这个古老的显示数码管
MySQL环境变量配置的教程
[batch dos-cmd command - summary and summary] - environment variables, path variables, search file location related instructions - set, path, where, what if there are spaces in the path parameters of
Zabbix部署说明(Server+Win客户端+交换机(H3C))
关于放大器失真的原因你了解多少呢?
JS to determine whether the number entered by the user is a prime number (multiple methods)
win命令行中导入、导出数据库相关表
How can I import data from Oracle into fastdfs?
c#的初步认识
Linux下Redis的安装
八大误区,逐个击破(终篇):云难以扩展、定制性差,还会让管理员失去控制权?
Pin details in rust
ZABBIX deployment instructions (server+win client + switch (H3C))
js用switch输出成绩是否合格
UE5神通--POI解决方案