当前位置:网站首页>Redis' optimistic lock and pessimistic lock for solving transaction conflicts
Redis' optimistic lock and pessimistic lock for solving transaction conflicts
2022-07-04 15:36:00 【BugMaker-shen】
List of articles
One 、Redis The problem of transaction conflict
Example :
for instance ,3 Individuals have your account : Do you have 10000 element
A person asked to reduce the amount 8000
A person asked to reduce the amount 5000
A person asked to reduce the amount 1000
Two 、 Pessimistic locking
Pessimistic locking (Pessimistic Lock), seeing the name of a thing one thinks of its function , Is very pessimistic , Every time I go to get the data, I think others will modify it , therefore I lock it every time I get the data ( No other transaction operation is allowed after locking ), So that if people want to take this data, they will block Until it gets the lock . There are many lock mechanisms used in traditional relational databases , For example, line locks. , Table lock, etc. , Read the lock , Write locks, etc. , It's all locked before the operation
The disadvantage is low efficiency , Serial operation only
3、 ... and 、 Pessimistic locking
Optimism lock (Optimistic Lock), seeing the name of a thing one thinks of its function , Is very optimistic , Every time I go to get the data, I think other people won't modify it , So it won't lock , So everyone can get the data , But in the process of updating, we will judge whether other people have updated this data during this period , You can use mechanisms like version numbers .
When reading data, it is unobstructed , When updating data, you need to match the version number of the read data with the version number of the data in the database , If it is consistent, it can be modified , Write back to the database after modification ; If the data is updated , It is inconsistent with the version number of the data in the database , You are not allowed to update , You need to read the data of the new version number to continue the operation
Optimistic lock is suitable for multi read applications , This can improve throughput .Redis It's using this check-and-set The mechanism implements the .
Four 、 Optimistic lock use
In execution multi Before starting a transaction , Execute first watch key1 [key2], You can watch one ( Or more ) key , If before the transaction is executed ( Or these ) key Altered by other orders , Then the business will be interrupted
unwatch Cancel WATCH Command to all key Surveillance : If in execution WATCH After the command ,EXEC Order or DISCARD If the order is executed first , Then there's no need to execute UNWATCH 了
watch The monitoring function is realized through optimistic locking
Both terminals monitor balance And open the business
terminal 1 Yes balance Add 10
terminal 1 Yes balance Add 20
terminal 1 exec Successful execution of transaction
terminal 2 exec Failed to execute transaction
analysis : Optimistic lock leads to terminal 2 Transaction execution failed ;2 All terminals get balance This data , All monitor it , First, add 10, Then the version number is updated , The second terminal performs addition 20, Judge the read balance Version number and database balance Version number , It's different , You can't perform the operation
5、 ... and 、Redis Three characteristics of transaction
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
There is no concept of isolation level : After opening the transaction , No, exec Before submitting, the command is only stored in the queue , Will not actually be implemented
There is no guarantee of atomicity : If a command fails in a transaction , Subsequent orders will still be executed , No rollback ( This and MySQL The atomicity of is different )
边栏推荐
- 2022年九大CIO趨勢和優先事項
- Redis哨兵模式实现一主二从三哨兵
- [book club issue 13] packaging format and coding format of audio files
- 左右对齐!
- Detailed explanation of MySQL composite index (multi column index) use and optimization cases
- 怎么判断外盘期货平台正规,资金安全?
- They are all talking about Devops. Do you really understand it?
- [book club issue 13] coding format of video files
- web聊天室实现
- [differential privacy and data adaptability] differential privacy code implementation series (XIV)
猜你喜欢
flutter 报错 No MediaQuery widget ancestor found.
MySQL组合索引(多列索引)使用与优化案例详解
Neuf tendances et priorités du DPI en 2022
MySQL index optimization
What is the future of the booming intelligent Internet of things (aiot) in recent years?
Dialogue with ye Yanxiu, senior consultant of Longzhi and atlassian certification expert: where should Chinese users go when atlassian products enter the post server era?
压力、焦虑还是抑郁? 正确诊断再治疗
How did the beyond concert 31 years ago get super clean and repaired?
华为云数据库DDS产品深度赋能
In today's highly integrated chips, most of them are CMOS devices
随机推荐
怎么判断外盘期货平台正规,资金安全?
Unity animation day05
C implementation defines a set of intermediate SQL statements that can be executed across libraries
大神详解开源 BUFF 增益攻略丨直播
MySQL index optimization
.Net 应用考虑x64生成
一篇文章学会GO语言中的变量
JS tile data lookup leaf node
Unity脚本API—Component组件
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
Intelligent customer service track: Netease Qiyu and Weier technology play different ways
Redis shares four cache modes
Shell 编程基础
js平铺数据查找叶子节点
Unity script introduction day01
Unity动画Animation Day05
Flutter reports an error no mediaquery widget ancestor found
web聊天室实现
Decimal, exponential
How to rapidly deploy application software under SaaS