当前位置:网站首页>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
边栏推荐
- Redis配置文件详解
- (resolved) the following raise notimplementederror occurs when Minet tests
- Redis主从复制以及哨兵模式
- SQL Sever column name or number of supplied values does not match the table definition
- UE5神通--POI解决方案
- 正确的理解MySQL的MVCC
- JS to determine whether the number entered by the user is a prime number (multiple methods)
- Testing network connectivity with the blackbox exporter
- MSSQL how to export and delete multi table data using statements
- 2022 love analysis · panoramic report of it operation and maintenance manufacturers
猜你喜欢

索引+sql练习优化

洛谷刷题心得记录

认识O(NlogN)的排序

C how to call line and rows when updating the database

野风药业IPO被终止:曾拟募资5.4亿 实控人俞蘠曾进行P2P投资
![[c++ primer notes] Chapter 4 expression](/img/cb/d543dd1f461653e9adf399b42d1d26.png)
[c++ primer notes] Chapter 4 expression

JS to judge the odd and even function and find the function of circular area

大厂工作十年,年薪40万突然被裁员,公司想抛弃你,一分情面都不会留
![[12. maximum continuous non repeating subsequence]](/img/eb/230cd6062e28374c86863f2122e43b.png)
[12. maximum continuous non repeating subsequence]

【批处理DOS-CMD命令-汇总和小结】-输出/显示命令——echo
随机推荐
05 observer mode
Ue5 magic power - POI solution
How to bind SQL statements to web buttons
Eight misunderstandings, broken one by one (final): the cloud is difficult to expand, the customization is poor, and the administrator will lose control?
SPARQL basic introductory exercise
JS example print the number and sum of multiples of all 7 between 1-100
Lvgl description 3 about the use of lvgl Guide
win命令行中导入、导出数据库相关表
[paper reading] internally semi supervised methods
Closure problem
JS to judge the odd and even function and find the function of circular area
【原创】TypeScript字符串utf-8编码解码
ACM课程学期总结
MySQL索引详解
Publications under nature, science and cell
Filter filter
March into machine learning -- Preface
lvgl使用demo及说明2
What are the specialties of database system engineers?
关于放大器失真的原因你了解多少呢?