当前位置:网站首页>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)
边栏推荐
- Gateway service gateway
- 2022-2028 global carbon fiber room scraper system industry research and trend analysis report
- Solving the weird problem that the query conditions affect the value of query fields in MySQL query
- [NLP] [textcnn] text classification
- Is it safe to open a stock account of Huatai Securities online?
- 20220216 misc buuctf another world WinHex, ASCII conversion flag zip file extraction and repair if you give me three days of brightness zip to rar, Morse code waveform conversion mysterious tornado br
- lvm-snapshot:基于LVM快照的备份之准备工作
- 2022-2028 global single travel industry research and trend analysis report
- How does the VR cloud exhibition hall bring vitality to offline entities? What are the functions?
- Tide - rust web framework based on async STD
猜你喜欢

2022-2028 global capsule shell industry research and trend analysis report

Wechat official account development (1) introduction to wechat official account

Quick start of wechat applet -- project introduction

2022-2028 global rotary transmission system industry research and trend analysis report

Why should VR panoramic shooting join us? Leverage resources to achieve win-win results

6-1 exploit -ftp exploit

Detailed explanation of conv2d -- use in arrays and images

To tell you the truth, ThreadLocal is really not an advanced thing

Basic knowledge of Embedded Network - introduction of mqtt

Redis - cache penetration, cache breakdown, cache avalanche
随机推荐
Pycharm useful shortcut keys
Red Hat将在Project Atomic上运用容器负载服务器
Why should VR panoramic shooting join us? Leverage resources to achieve win-win results
Explain kubernetes backup and recovery tools velero | learn more about carina series phase III
在指南针上买基金安全吗?
2022-2028 global ultra high purity electrolytic iron sheet industry research and trend analysis report
Maxpool2d explanation -- Application in arrays and images
2022-2028 global weight loss ginger tea industry research and trend analysis report
Software supply chain security risk pointing North for enterprise digitalization and it executives
Bridge emqx cloud data to AWS IOT through the public network
$watch will not trigger data change - $watch not firing on data change
The girlfriend said: if you want to understand the three MySQL logs, I will let you heiheihei!
[NLP] [textcnn] text classification
Dataloader source code_ DataLoader
On the application of cluster analysis in work
Mysql database query optimization
Ditto set global paste only text shortcuts
Thoughts on the future of data analysis in "miscellaneous talk"
[UML] UML class diagram
[designmode] singleton pattern