当前位置:网站首页>Redis based distributed lock
Redis based distributed lock
2022-07-01 00:23:00 【ordinaryBlog】
Distributed lock
The reason for using distributed locks
Distributed lock is a way to ensure synchronous access to shared resources between distributed systems
matters needing attention
When acquiring the lock , Use SET name value NX EX Time Ensure atomicity when acquiring locks , And the lock is automatically released in case of downtime
When releasing the lock , Run the following script , Avoid releasing locks on other threads
// Release the lock Compare value Whether it is equal or not Avoid accidental release
if redis.call("get",KEYS[1]) == ARGV[1] then
return redis.call("del",KEYS[1])
else
return 0
end
Redlock Algorithm
Redlock yes Redis The author of Antirez The proposed cluster mode is distributed lock , be based on N Completely independent Redis Nodes realize high availability of distributed locks
Suppose there is N Completely independent Redis node , stay N individual Redis Use and on instances Redis In single instance, the same method is used to acquire and release the lock .
In the process of acquiring and releasing locks, the client will perform the following operations :
Get current system time , In Milliseconds
Use the same... In turn Key And random values in N Lock requested on nodes
When Redis When requesting a lock , The client will set a timeout for network connection and response , This timeout should be less than the lock expiration time , This prevents the client from waiting .
The client uses the current time minus the time to acquire the lock to get the time to acquire the lock , If and only if more than half of Redis Node gets lock , And the use time is less than the lock aging time , Lock is success
If you get the lock ,key The real effective time of is equal to The effective time minus the time taken to acquire the lock
. If for some reason , Lock acquisition failed ( The lock is not obtained in more than half of the instances, or the lock removal time has exceeded the effective time ), The client should be in all Redis Unlock the instance , No matter what Redis Whether the instance is locked successfully , Because the server response message may have been lost, but it actually succeeded , After all, one more release will not be a problem
( Reference resources https://www.cnblogs.com/backnullptr/p/12155812.html)
边栏推荐
- LVM snapshot: preparation of backup based on LVM snapshot
- CentOS installation starts redis
- [leetcode] [SQL] notes
- Operation record of reinitialization instance of Dameng database
- 1175. prime number arrangement / Sword finger offer II 104 Number of permutations
- Random ball size, random motion collision
- The difference between union and union all in MySQL
- 6-1 exploit -ftp exploit
- Arthas debugging problem determination Toolkit
- SSM integration process (integration configuration, function module development, interface test)
猜你喜欢

Solving the weird problem that the query conditions affect the value of query fields in MySQL query

20220215-ctf-misc-buuctf-einstein-binwalk analyze picture-dd command separate zip file -- look for password in picture attribute

2022-2028 global rampant travel industry research and trend analysis report

The girlfriend said: if you want to understand the three MySQL logs, I will let you heiheihei!

Redis - sentinel mode

Vmware16 installing win11 virtual machine (the most complete step + stepping on the pit)

2022-2028 global ethylene oxide scrubber industry research and trend analysis report
![[untitled]](/img/96/7f26614bbdcce71006e38ee34ab216.jpg)
[untitled]

ABAQUS 2022 software installation package and installation tutorial
![[designmode] factory pattern](/img/62/9be808b3e1c2139d564caa307fcd30.png)
[designmode] factory pattern
随机推荐
A detailed explanation of the implementation principle of go Distributed Link Tracking
The difference between union and union all in MySQL
Why should VR panoramic shooting join us? Leverage resources to achieve win-win results
Excuse me, does Flink support synchronizing data to sqlserver
Error 2059 when Navicat connects to MySQL
SSM integration process (integration configuration, function module development, interface test)
女朋友说:你要搞懂了MySQL三大日志,我就让你嘿嘿嘿!
Repetition is the mother of skill
[leetcode] [SQL] notes
Makefile notes (Yiwen Institute makefile)
2022-2028 global herbal diet tea industry research and trend analysis report
Redis - understand the master-slave replication mechanism
2022-2028 global ultra high purity electrolytic iron powder industry research and trend analysis report
1175. prime number arrangement / Sword finger offer II 104 Number of permutations
5g smart building solution 2021
Wechat official account development (1) introduction to wechat official account
2022-06-30: what does the following golang code output? A:0; B:2; C: Running error. package main import “fmt“ func main()
Software supply chain security risk pointing North for enterprise digitalization and it executives
The college entrance examination in 2022 is over. Does anyone really think programmers don't need to study after work?
Cloud security daily 220630: the IBM data protection platform has found an arbitrary code execution vulnerability, which needs to be upgraded as soon as possible