当前位置:网站首页>Redisson distributed lock source code analysis for high-level use of redis
Redisson distributed lock source code analysis for high-level use of redis
2022-08-02 08:23:00 【Practice makes perfect - LWP】
1. What is a distributed lock
1. Lock synchronized lock Stand-alone lock
2. Some of our mutually exclusive resources cannot be executed in parallel. We need something to ensure that the lock executed in serial is synchronized
3、Distributed lock: This lock storage must be independent of our business service
2. Design a lock by yourself, how to achieve
Requirements:
1. Only one thread can execute mutual exclusion at the same timeresources
2. When other threads are executing, if there is a thread executing, either wait or report an errorimplementation steps:
1. There is a mark to mark whether there is a thread executing the toilet pit door lock state=1 Someone is doing it=0 No one is doing it
2. This mark must be visible to ensure mutualThis mark is volatile
that can get the latest result. 3. Obtaining this mark cannot simultaneously preempt the successful safe cas3. How to do distributed locks in Redis
1. Mark the k-v structure key as a mark. If the key exists, it means that someone is doing it. If it does not exist, it means that no one is doing it.
2. Visibility listThe thread must be set before you can get
3. Ensure atomic setnx single-line execution
if(exists("pay:11") == 0) {
set("pay:111",1);
}
4. We hope that no other instructions can be inserted between exists and set2 instructions. To ensure the atomicity of multiple instructionsSexual mulit open transaction - exec commit discard rollbackFour. Transaction
1. The command is atomic, but the subsequent logic cannot be determined according to the intermediate commandFive, lua script
1. The host redis must support lua script perfectly
2. eval "return 'huihui'" 0
3. eval "if KEYS[1]=='1' then ARGV[1] end return ARGV[2]" 1 1 'huihui' 'xiaohuihui' The first parameter is the number of KEYs, the second is the value of KEY, the third is the value of ARGV1, and the fourth is the value of ARGV2Value
4. How to do duplicate locks?The same thread can be locked and added multiple times
4.1 Mutually exclusive key Big key
4.2 Know the thread information field
4.3 Save the number of flushes value hincrby thread-safe
4.
5.If the 30s lock will expire, but the business will fail if the lock is not executed.Six, Redission lock principle source code analysis
1.lua + hash to do it
if (redis.call('exists', KEYS[1]) == 0)
then " +
"redis.call('hincrby', KEYS[1], ARGV[2], 1); " +
"redis.call('pexpire', KEYS[1], ARGV[1]); " +
"return nil; " +
"end; " +
"if (redis.call('hexists', KEYS[1], ARGV[2]) == 1) then " +
"redis.call('hincrby', KEYS[1], ARGV[2], 1); " +
"redis.call('pexpire', KEYS[1], ARGV[1]); " +
"return nil; " +
"end; " +
"return redis.call('pttl', KEYS[1]);",If there is an expiration date?
3. Check this thing regularly, if it is, I will reset the expiration time for it! If it is not, I will not set it
4. The hashwheelTimer is also a scheduled task7. Source code analysis of the time wheel algorithm principle of watchDog watchdog mechanism
1. Interlocking: Give you multiple master nodes + the same lock at the same time
When Redis is doing distributed locks, there will be multiple losses ap model
Reduce lossesInterview questions:
1. There is a foot bath shop and their technicians are arranged in this way. There are 8 technicians in total.
Technician 1: Responsible for guests 0-3
Technician 2: Responsible for 3-6 guests
Technician 3: responsible for 6-9 guests
Technician 4: responsible for 9-12 guests
Technician 5: responsible for 12-15 guests
Technician 6: responsible for 15-18 guestsGuest
Technician 7: In charge of guests 18-21
Technician 8: In charge of guests from 21-24
If there is a day off at 6:00 in the morning, I want to book a massage for 9 hours later, who will I do?Technician for me?
6 o'clock 6+9=15 hours to go to massage 6 after 15 o'clock, there are already people in line, you have to line up tooIf the current time is 21:00, I would like to make an appointment for which technician will come to service after 6 hours
21+6 27 hours 2
边栏推荐
猜你喜欢
Understand Chisel language. 31. Chisel advanced communication state machine (3) - Ready-Valid interface: definition, timing and implementation in Chisel
PanGu-Coder:函数级的代码生成模型
Shell becomes canonical and variable
MySQL - based
Visual Analysis of DeadLock
MySQL - Detailed Explanation of Database Transactions
DeadLock的可视化分析
解决IDEA安装安装插件慢问题
OSPF 综合实验
MySQL事务隔离级别详解
随机推荐
Control 'ContentPlaceHolder1_ddlDepartment' of type 'DropDownList' must be placed inside a form tag with runat=server.
Conditional constructor ~wapper
@RequestBody使用
WebGPU 导入[2] - 核心概念与重要机制解读
Seleniu截图代码以及给图片赋值名字
Hack The Box - File Transfers Module详细讲解中文教程
59: Chapter 5: Develop admin management services: 12: MongoDB usage scenarios; (non-core data, non-core data with a relatively large amount of data, small private files such as face photos;)
UG NX二次开发(C#)-外部模式-导出dwg格式的文件
flutter在导航栏处实现对两个列表的点击事件
DeadLock的可视化分析
Inverter Phase Locking Principle and DSP Implementation
Buried development process
原型模式
常用的云安全防护措施盘点
Understand the Chisel language. 30. Chisel advanced communication state machine (2) - FSMD: Take Popcount as an example
mysql如何从某一行检索到最后
Biotin hydrazide HCl|CAS:66640-86-6|生物素-酰肼盐酸盐
五款优秀免费的在线抠图工具
设置工作模式与环境(中):建造二级引导器
有点奇怪!访问目的网址,主机能容器却不行