当前位置:网站首页>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
边栏推荐
- MySQL - low level settings
- WebForm DropDownList bind year and month respectively
- 小说里的编程 【连载之二十三】元宇宙里月亮弯弯
- MGRE综合实验
- 如何将项目部署到服务器上(全套教程)
- postgres groupby merge strings
- 设置工作模式与环境(中):建造二级引导器
- 牛客2022 暑期多校4 D Jobs (Easy Version)(递推优化策略)
- 传递泛型给JSX元素
- Figure robot software digital twin station oil and gas pipelines, oil and gas transportation control platform
猜你喜欢
随机推荐
WebForm DropDownList bind year and month respectively
mysql启动报错The server quit without updating PID file几种解决办法
IO进程线程->进程->day4
HCIP第二天
5分钟搞懂MySQL - 行转列
UG NX二次开发(C#)-外部模式-导出dwg格式的文件
HCIP 第十三天
I.MX6U-ALPHA开发板(EPIT定时器实验)
学习笔记(7)Funtion
Seleniu截图代码以及给图片赋值名字
prometheus监控mysql_galera集群
WebGPU 导入[2] - 核心概念与重要机制解读
研发过程中的文档管理与工具
传递泛型给JSX元素
[ansible] playbook explains the execution steps in combination with the project
flutter 自己写一个组件
如何开启mysql慢查询日志?
【Unity3D】初学加密技巧(反破解)
读入、输出优化
OSPF 综合实验