当前位置:网站首页>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 )
边栏推荐
- 重排数组
- flutter 报错 No MediaQuery widget ancestor found.
- 数据湖治理:优势、挑战和入门
- 每周招聘|高级DBA年薪49+,机会越多,成功越近!
- Unity脚本介绍 Day01
- In today's highly integrated chips, most of them are CMOS devices
- 31年前的Beyond演唱会,是如何超清修复的?
- %f格式符
- Stress, anxiety or depression? Correct diagnosis and retreatment
- Detailed explanation of MySQL composite index (multi column index) use and optimization cases
猜你喜欢
Neuf tendances et priorités du DPI en 2022
go-zero微服务实战系列(九、极致优化秒杀性能)
Dry goods | fMRI standard reporting guidelines are fresh, come and increase your knowledge
lnx 高效搜索引擎、FastDeploy 推理部署工具箱、AI前沿论文 | ShowMeAI资讯日报 #07.04
函数式接口,方法引用,Lambda实现的List集合排序小工具
MYSQL索引优化
AI做题水平已超过CS博士?
每周招聘|高级DBA年薪49+,机会越多,成功越近!
Functional interface, method reference, list collection sorting gadget implemented by lambda
Redis 发布和订阅
随机推荐
An article learns variables in go language
Helix swarm Chinese package is released, and perforce further improves the user experience in China
Blood cases caused by Lombok use
c# 实现定义一套中间SQL可以跨库执行的SQL语句
Decimal, exponential
【读书会第十三期】 音频文件的封装格式和编码格式
MySQL index optimization
Unity script lifecycle day02
%S format character
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination
怎么判断外盘期货平台正规,资金安全?
Unity脚本常用API Day03
基于MAX31865的温度控制系统
lnx 高效搜索引擎、FastDeploy 推理部署工具箱、AI前沿论文 | ShowMeAI资讯日报 #07.04
[book club issue 13] ffmpeg common methods for viewing media information and processing audio and video files
%f格式符
Redis 发布和订阅
js平铺数据查找叶子节点
宽度与对齐
In today's highly integrated chips, most of them are CMOS devices