当前位置:网站首页>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优化之慢日志查询

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;)

MySQL - locking mechanism

HCIP第二天

MySQL - based

HCIP9_BGP增加实验

如何将项目部署到服务器上(全套教程)

解决IDEA安装安装插件慢问题

3D激光slam:LeGO-LOAM---地面点提取方法及代码分析

2022-7-31 12点 程序爱生活 恒指底背离中,有1-2周反弹希望
随机推荐
设置 height: auto 却无法触发 transition 动画的解决方案
nodejs 简介
Buried development process
uniapp 禁止默认返回事件
IO进程线程->进程->day4
有点奇怪!访问目的网址,主机能容器却不行
优炫数据库的逻辑复制怎么样?
MGRE综合实验
小说里的编程 【连载之二十三】元宇宙里月亮弯弯
血气方刚的年轻小伙竟去做家政小哥,是怎样成功逆袭转行的
[ansible] playbook explains the execution steps in combination with the project
Appium swipe problem
Ansible 学习总结(11)—— task 并行执行之 forks 与 serial 参数详解
Biotin-EDA|CAS:111790-37-5| 乙二胺生物素
静态路由综合实验
HCIP第三天
prometheus监控mysql_galera集群
mysql去除重复数据
常用的云安全防护措施盘点
RIP综合实验