当前位置:网站首页>Redis transaction and optimistic lock
Redis transaction and optimistic lock
2022-07-28 00:42:00 【Although the sunset is not as beautiful as you】
Preface
Business
① Atomicity (atomicity). A transaction is an indivisible unit of work , The operations included in the transaction either do , Either not .
② Uniformity (consistency). The transaction must be to change the database from one consistency state to another . Consistency is closely related to atomicity .
③ Isolation, (isolation). The execution of one transaction cannot be interfered by other transactions . That is, the operations and data used within a transaction are isolated from other concurrent transactions , Transactions that execute concurrently cannot interfere with each other .
④ persistence (durability). Permanence is also called permanence (permanence), Once a transaction is committed , Its changes to the data in the database should be permanent . Subsequent operations or failures should not have any impact on it .
stay Redis There is no concept of transactions without isolation levels !
stay Redis A single imperative guarantees atomicity , But transactions don't guarantee atomicity !
Optimism lock
① When concurrency may occur in a program , It is necessary to ensure the accuracy of data in the case of concurrency , This ensures that when the current user operates with other users , The result obtained is the same as when he operated alone .
② No concurrency control , It may cause dirty reading 、 Unreal reading and non repeatable reading .
stay Redis It can be realized Optimism lock Of !
One 、Redis How to implement transactions ?
① Normal execution of business
127.0.0.1:6379> multi # Open transaction
OK
127.0.0.1:6379> set name dingyongjun # Add data
QUEUED
127.0.0.1:6379> set age 26 # Add data
QUEUED
127.0.0.1:6379> set high 172 # Add data
QUEUED
127.0.0.1:6379> exec Perform transactions
1) OK
2) OK
3) OK
127.0.0.1:6379> get name # Data acquisition success , Prove the successful execution of the transaction
"dingyongjun"
127.0.0.1:6379> get age
"26"
② Give up the business
127.0.0.1:6379> multi # Open transaction
OK
127.0.0.1:6379> set name dingyongjun # Add data
QUEUED
127.0.0.1:6379> set age 26 # Add data
QUEUED
127.0.0.1:6379> discard # Give up the business
OK
127.0.0.1:6379> get name # Will not perform the add operation in the transaction
(nil)
③ Compile time exception , Something is wrong with the code , Or there's a problem with the command , All orders will not be executed
127.0.0.1:6379> multi # Open transaction
OK
127.0.0.1:6379> set name dingyongjun # Add data
QUEUED
127.0.0.1:6379> set age 23 # Add data
QUEUED
127.0.0.1:6379> getset name # Enter a wrong command , At this time, it has been reported wrong , But this is still in the queue of transactions
(error) ERR wrong number of arguments for 'getset' command
127.0.0.1:6379> set high 173 # Add data
QUEUED
127.0.0.1:6379> exec # Perform transactions , Report errors , And all commands will not be executed
(error) EXECABORT Transaction discarded because of previous errors.
127.0.0.1:6379> get name # Get data is empty , Prove that it has not been implemented
(nil)
④ Runtime exception , Except that syntax errors are not executed and exceptions are thrown , Other correct commands can be executed normally
127.0.0.1:6379> multi # Open transaction
OK
127.0.0.1:6379> set name dingyongjun # Add string data
QUEUED
127.0.0.1:6379> incr name # Perform auto increment operation on string data
QUEUED
127.0.0.1:6379> set age 23 # Add data
QUEUED
127.0.0.1:6379> get age # get data
QUEUED
127.0.0.1:6379> exec # Perform transactions . Although an error is reported when the string data is auto incremented , But other commands can be executed normally
1) OK
2) (error) ERR value is not an integer or out of range
3) OK
4) "23"
127.0.0.1:6379> get age # Data acquisition success
"23"
⑤ summary : It can be concluded from the above ,Redis It supports single command transactions , But transactions do not guarantee atomicity !
Two 、Redis How to achieve optimistic lock ?
①watch( monitor )
127.0.0.1:6379> set money 100 # Add money 100
OK
127.0.0.1:6379> set cost 0 # Add expenses 0
OK
127.0.0.1:6379> watch money # Monitor money
OK
127.0.0.1:6379> multi # Open transaction
OK
127.0.0.1:6379> DECRBY money 30 # money -30
QUEUED
127.0.0.1:6379> incrby cost 30 # cost +30
QUEUED
127.0.0.1:6379> exec # Perform transactions , success ! At this time, the data can be successful only if there is no change
1) (integer) 70
2) (integer) 30
② Multithreaded testing watch
# Threads 1
# Threads 1
127.0.0.1:6379> set money 100 # Add money 100
OK
127.0.0.1:6379> set cost 0 # Add expenses 0
OK
127.0.0.1:6379> watch money # Turn on Monitoring ( Optimism lock )
OK
127.0.0.1:6379> multi # Open transaction
OK
127.0.0.1:6379> DECRBY money 20 # money -20
QUEUED
127.0.0.1:6379> INCRBY cost 20 # cost +20
QUEUED
# Don't execute here , First execution thread 2 To modify the monitored value
127.0.0.1:6379> exec # Error report in execution , Because we watched money This value , If the transaction wants to operate on this value
# The monitor will judge whether this value is normal , If there is a change , Transaction execution failed !
(nil)
# Threads 2
# Threads 2, This operation is executed before the transaction is executed
127.0.0.1:6379> INCRBY money 20 # money +20
(integer) 120
③ summary : The difference between optimistic lock and pessimistic lock .
Pessimistic locking : There's always a problem , So keep watching , Before the current step is completed , Don't let any thread execute , It's a waste of performance ! Generally not used !
Optimism lock : Only judge when updating the data , Has anyone modified the monitored data during this period , If not, carry out the business normally , Otherwise, the execution fails !
边栏推荐
- JS event propagation capture stage bubbling stage onclick addeventlistener
- Leetcode 415. string addition and 43. string multiplication
- Jmeter 如何解决乱码问题?
- Precautions for site selection of Yongzhou clean animal laboratory
- Application scenario Display of metauniverse
- 592. 分数加减运算 : 表达式计算入门题
- JVM memory model
- Matlab | those matlab tips you have to know (2)
- 相应通道无电压但ADC的值却在大幅变化且不等于0的可能原因
- [meetup preview] openmldb + ONEFLOW: link feature engineering to model training to accelerate machine learning model development
猜你喜欢

require、loadfile、dofile、load、loadstring

服务器中毒了——菜是原罪
![[meetup preview] openmldb + ONEFLOW: link feature engineering to model training to accelerate machine learning model development](/img/17/15c759aadafc335028d37380903ee7.jpg)
[meetup preview] openmldb + ONEFLOW: link feature engineering to model training to accelerate machine learning model development

点分治解析

Is it amazing to extract text from pictures? Try three steps to realize OCR!

数据可视化-《白蛇2:青蛇劫起》(3)

code review 工具

Set 数据构造函数

Is there a general formula for tens of millions of players? B station is underestimated as a hot money opportunity!

英特尔携手汉朔、微软,释放“AI + 零售”大招!
随机推荐
Yongzhou water quality testing laboratory construction: Furniture description
英特尔AI实践日第56期 | 探讨行业发展新趋势
小程序助力智能家居生态平台
How to realize fast recognition of oversized images
程序员工作中的理性与感性活动及所需的技能素养
这种动态规划你见过吗——状态机动态规划之股票问题(中)
What foundation does Yolo need? How to learn Yolo?
49 times faster, NVIDIA releases quantum hybrid programming platform qoda
threejs个人笔记
Current situation of semiconductor testing equipment Market: the localization rate is still less than 10%!
mysql数据库的基本操作(一)-——基于数据库
JS 事件传播 捕获阶段 冒泡阶段 onclick addEventListener
阿里二面:为什么要分库分表?
mysql数据库的基本操作(三)-——基于字段
【打新必读】魅视科技估值分析,分布式视听产品及解决方案
网络设备硬核技术内幕 防火墙与安全网关篇 (小结)
Jmeter 如何解决乱码问题?
细数国产接口协作平台的六把武器!
Precautions for site selection of Yongzhou clean animal laboratory
LeetCode 415. 字符串相加 和 43. 字符串相乘