当前位置:网站首页>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 )
边栏推荐
- Data Lake Governance: advantages, challenges and entry
- 夜天之书 #53 Apache 开源社群的“石头汤”
- 宽度与对齐
- 开源人张亮的 17 年成长路线,热爱才能坚持
- Intelligent customer service track: Netease Qiyu and Weier technology play different ways
- Optimization method of deep learning neural network
- 每周招聘|高级DBA年薪49+,机会越多,成功越近!
- Functional interface, method reference, list collection sorting gadget implemented by lambda
- Weekly recruitment | senior DBA annual salary 49+, the more opportunities, the closer success!
- Temperature control system based on max31865
猜你喜欢

大神详解开源 BUFF 增益攻略丨直播

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?

Intelligent customer service track: Netease Qiyu and Weier technology play different ways

Neuf tendances et priorités du DPI en 2022

案例分享|金融业数据运营运维一体化建设

Deep learning neural network case (handwritten digit recognition)
Redis shares four cache modes

Data Lake Governance: advantages, challenges and entry

LNX efficient search engine, fastdeploy reasoning deployment toolbox, AI frontier paper | showmeai information daily # 07.04

Guitar Pro 8win10 latest guitar learning / score / creation
随机推荐
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination
【读书会第十三期】 音频文件的封装格式和编码格式
Analysis of nearly 100 million dollars stolen and horizon cross chain bridge attacked
Unity脚本API—Transform 变换
Unity脚本API—Component组件
unity update 协程_Unity 协程的原理
LeetCode 58. Length of the last word
中国主要城市人均存款出炉,你达标了吗?
宽度精度
Understand the context in go language in an article
一篇文章学会GO语言中的变量
Redis哨兵模式实现一主二从三哨兵
MYSQL索引优化
LeetCode 35. Search the insertion position - vector traversal (O (logn) and O (n) - binary search)
Huawei cloud database DDS products are deeply enabled
Unity脚本常用API Day03
左右对齐!
Enter the width!
Redis 发布和订阅
宽度与对齐