当前位置:网站首页>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
边栏推荐
- What happens from the input URL to the page load
- Where is the bitbucket clone address
- Calculate CPU utilization [Prometheus]
- Trie tree template 2
- 多表查询之子查询_单行单列情况
- SMTP automatic mail sending function code
- Spark Learning: compile spark source code in win10
- PHP caching system - PHP uses Memcache
- Anti shake and throttling
- [STM32 learning] (7) use of serial port 2 (usart2)
猜你喜欢
![[STM32 learning] (16) STM32 realizes LCD1602 display (74HC595 drive) - 4-bit bus](/img/8f/19b0eb959d2b3f896c8e99f8e673d1.png)
[STM32 learning] (16) STM32 realizes LCD1602 display (74HC595 drive) - 4-bit bus

Dark king | analysis of zego low illumination image enhancement technology

Deployment and analysis of coredns

Detailed explanation of uninstalling MySQL completely under Linux

Web page opening speed is very slow, how to solve it?

Hucang integrated e-commerce project (I): introduction to the project background and structure

2022 trusted cloud authoritative assessment released: Tianyi cloud has obtained ten certifications and five best practices

Embedded development: Tools - optimizing firmware using DRT

Spark Learning: implement compact table command

The best time to buy and sell stocks (leetcode-121)
随机推荐
[200 opencv routines] 236. Principal component analysis of feature extraction (openCV)
Boundless dialogue | participate in the live broadcast on July 25 and win the prize
Uniapp calendar component
error: field ‘XXX’ declared as a function
Exception: pyqtgraph requires Qt version >= 5.12 (your version is 5.9.5)
ES6 question
It is reported that the prices of some Intel FPGA chip products have increased by up to 20%
2022: I feel like sleeping for the first time during the day
【LeeCode】获取2个字符串的最长公共子串
[STM32 learning] (14) two 74HC595 controls four nixie tube displays
Simple parsing JSON strings with regular expressions
给你的网站加一个爱发电角标
Financial digital transformation
Do you really understand the concept of buffer? Take you to uncover the buffer zone~
07 Jason module
Arduino drive lcd1602a
2022, our small goal
Get the historical quotation data of all stocks
The optimal time to buy and sell stocks includes the freezing period (leetcode-309)
Countdownlatch and join [concurrent programming]