当前位置:网站首页>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 )
边栏推荐
- Solve the error of JSON module in PHP compilation and installation under CentOS 6.3
- 直播预告 | PostgreSQL 内核解读系列第二讲:PostgreSQL 体系结构
- 在芯片高度集成的今天,绝大多数都是CMOS器件
- C1 certification learning notes 3 -- Web Foundation
- 这几年爆火的智能物联网(AIoT),到底前景如何?
- web聊天室实现
- MySQL - MySQL adds self incrementing IDs to existing data tables
- Understand the context in go language in an article
- Redis publier et s'abonner
- Redis sentinel mode realizes one master, two slave and three Sentinels
猜你喜欢
Unity脚本生命周期 Day02
C1 certification learning notes 3 -- Web Foundation
Quelles sont les perspectives de l'Internet intelligent des objets (aiot) qui a explosé ces dernières années?
flutter 报错 No MediaQuery widget ancestor found.
Stress, anxiety or depression? Correct diagnosis and retreatment
MySQL index optimization
Optimization method of deep learning neural network
Detailed explanation of MySQL composite index (multi column index) use and optimization cases
Redis shares four cache modes
How did the beyond concert 31 years ago get super clean and repaired?
随机推荐
LeetCode 1184. Distance between bus stops -- vector clockwise and counterclockwise
Unity script API - component component
Shell programming basics
Redis 发布和订阅
%S format character
Flutter reports an error no mediaquery widget ancestor found
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?
[book club issue 13] ffmpeg common methods for viewing media information and processing audio and video files
怎么判断外盘期货平台正规,资金安全?
谈SaaS下如何迅速部署应用软件
Unity prefab day04
Nine CIO trends and priorities in 2022
Unity script API - transform transform
【读书会第十三期】视频文件的编码格式
31年前的Beyond演唱会,是如何超清修复的?
宽度精度
PXE network
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
每周招聘|高级DBA年薪49+,机会越多,成功越近!
Scientific research cartoon | what else to do after connecting with the subjects?