当前位置:网站首页>Analysis of distributed lock redistribution principle
Analysis of distributed lock redistribution principle
2022-07-24 10:07:00 【Pupils learning programming】
1. be based on setnx The distributed lock implemented has the following problems
- Do not reenter : The same thread cannot get the same handle more than once
- Can't try again : Only one attempt to acquire a lock returns false, No retry mechanism
- Time out release : Lock timeout release can avoid deadlock , But if the business takes a long time to execute , It will also cause the lock to release , There are safety risks
- Master slave consistency : If Redis Provides a master-slave cluster , There is a delay in master-slave synchronization , When the Lord outage , If you slave and synchronize the lock data in the master , Lock implementation will appear
Redisson It's a Redis On the basis of implementation Java In memory data grid (In-Memory Data Grid). It not only provides a series of distributions Type Java Common objects , There are also many distributed services , It includes the implementation of various distributed locks . Use Redission Can solve the above mentioned 4 A question .
2. Code implementation
2.1 Import dependence

2.2 To configure Redisson client

2.3 Use Redisson Distributed locks for

3.Redisson Reentrant lock principle
3.1 structure
adopt hash structure :
When value yes 0 The description can be deleted
3.2 flow chart

Source code
Lock source code :
The source code parsing :
Release the lock :

The source code parsing :
Redisson The principle of distributed lock

Principle summary :
- Reentrant : utilize hash Structure record thread id And reentry times
- Can try again ( Try again when you have time left , That is, publish and subscribe to realize lock retry ): Use semaphores and PubSub Function implementation wait 、 Wake up the 、 Retry mechanism for failed lock acquisition
- Overtime renewal ( Renew the contract continuously through scheduled tasks ): utilize watchDog, Every once in a while (releaseTime/3), Reset timeout
边栏推荐
- How to solve command 'xxx GCC' not found, but can be installed with:??
- How to solve the problem of robot positioning and navigation in large indoor scenes with low-cost solutions?
- 聚集日志服务器
- Looting (leetcode-198)
- PHP caching system - PHP uses Memcache
- [STM32 learning] (17) STM32 realizes LCD12864 display serial implementation
- Home raiding III (leetcode-337)
- It's eleven again. Those jokes about nagging programmers going home for blind dates
- Tencent 5g innovation center was established, laying out key directions such as unmanned ports, smart mines and E-sports events
- [STM32 learning] (22) STM32 realizes 360 degree rotary encoder
猜你喜欢

Recursion - if the function calls itself internally, then the function is a recursive function & the effect is the same as that of the loop & the push condition return should be added, otherwise stack
![[STM32 learning] (12) STM32 realizes LCD1602 simple static reality](/img/78/954ebaae0cce5d9387e7032fa85e60.png)
[STM32 learning] (12) STM32 realizes LCD1602 simple static reality
![[STM32 learning] (13) STM32 realizes ultrasonic ranging (hc-sr04)](/img/6e/b7cf7a8e3296e29d61a0626e3793ea.png)
[STM32 learning] (13) STM32 realizes ultrasonic ranging (hc-sr04)

高精尖中心论文入选国际顶会ACL 2022,进一步拓展长安链隐私计算能力

Raspberry Pie: serial port login does not display print information

It's eleven again. Those jokes about nagging programmers going home for blind dates
![[STM32 learning] (10) stm32f1 general timer realizes pulse counter](/img/42/1f5104f923b1868dda23bcc425fc62.png)
[STM32 learning] (10) stm32f1 general timer realizes pulse counter

The most complete solution for distributed transactions

error: field ‘XXX’ declared as a function

Tencent 5g innovation center was established, laying out key directions such as unmanned ports, smart mines and E-sports events
随机推荐
Is it safe for Oriental Fortune futures to open an online account, and will it be cheated?
Embedded development: Tools - optimizing firmware using DRT
Ribbon's loadbalancerclient, zoneawareloadbalancer and zoneavoidancerule are three musketeers by default
给你的网站加一个爱发电角标
Exception: pyqtgraph requires Qt version >= 5.12 (your version is 5.9.5)
[STM32 learning] (5) press the key to control the flow light (interrupt Implementation)
Simple parsing JSON strings with regular expressions
LiteOS_ a - SYS_ The run() function is missing a header file.
[robot learning] mechanism kinematics analysis and MATLAB simulation (3D model +word report +matlab program)
An article takes you to understand the operation of C language files in simple terms
Boundless dialogue | participate in the live broadcast on July 25 and win the prize
SMTP automatic mail sending function code
CAS principle [concurrent programming]
How to solve the problem of robot positioning and navigation in large indoor scenes with low-cost solutions?
MySQL 数据库 JDBC编程
It is reported that the prices of some Intel FPGA chip products have increased by up to 20%
The optimal time to buy and sell stocks includes the freezing period (leetcode-309)
Spark Learning: build SQL to meet the specified optimization rules
【机器人学习】机构运动学分析与matlab仿真(三维模型+word报告+matlab程序)
Set scrolling for the box