当前位置:网站首页>Implementation of redis distributed lock
Implementation of redis distributed lock
2022-07-25 15:00:00 【Hua Weiyun】
Redis The implementation of distributed lock understands the basic concepts
Three core elements of distributed lock implementation :
Lock
The easiest way is to use setnx command . key It's the only sign of the lock , Name it by business . For example, if you want to give a commodity Lock the seckill activity , You can give key Name it “lock_sale_ goods ID” .
When a thread executes setnx return 1 , explain key It didn't exist , The thread successfully got the lock ; When a thread executes setnx return 0 , explain key Already exist , The thread failed to grab the lock .
Unlock
If you lock it, you have to unlock it . When the thread that gets the lock finishes executing the task , Need to release lock , So that other threads can enter . The simplest way to release the lock The single way is to execute del Instructions .
After releasing the lock , Other threads can continue to execute setnx Command to get the lock .
Lock timeout
What do you mean by lock timeout ? If a locked thread hangs up during the execution of a task , There's no time to explicitly release the lock , This capital The source will be locked forever ( Deadlock ), Other threads don't want to come in any more . therefore , setnx Of key A timeout must be set , To ensure that even if not explicitly released , This lock should also be released automatically after a certain time . setnx Timeout parameter is not supported , So you need the amount External instructions expire .
The problem is
setnx and expire Non atomicity of
When a thread executes setnx , Success got the lock , setnx Just executed successfully , It hasn't been implemented yet expire Instructions , node 1 hang It fell off .
have access to set (key, value, time, NX), This can replace setnx Instructions .
del Cause false deletion
If a thread succeeds in getting a lock , And the set timeout is 30 second . For some reason, threads A It's very slow , After that 30 Seconds didn't finish , At this time, the lock will be released automatically after expiration , Threads B Got the lock. . And then , Threads A Finished the mission , Threads A Pick up Try to execute del Command to release the lock . But this time the thread B We're not done yet , Threads A actually Delete the thread B Lock added .
How to avoid this situation ?
Can be in del Make a decision before releasing the lock , Verify whether the current lock is a lock added by itself . As for the concrete realization , It can be locked It's time to put the current thread ID treat as value , And verify... Before deleting key Corresponding value Is it my own thread ID.
The above method is two independent operations , It's not atomic .
We can let the thread obtaining the lock open a guard thread , Used for locks that are about to expire “ life ”.
When it's past 29 second , Threads A We're not done yet , At this time, the daemons will execute expire Instructions , For this lock “ The lives ”20 second . keep Thread protector from 29 Start execution in seconds , Every time 20 Once per second .
When a thread A Finish the task , Will explicitly turn off the daemons .
If node 1 A sudden power failure , Because of the thread A In the same process as the daemons , The daemons will also stop . This lock has expired Hou , No one gave it a life , It's automatically released .
边栏推荐
- Niuke multi school E G J L
- "Ask every day" briefly talk about JMM / talk about your understanding of JMM
- "How to use" decorator mode
- LeetCode_字符串_中等_151.颠倒字符串中的单词
- Content type corresponding to office file
- Several methods of spark parameter configuration
- LeetCode_ Factorization_ Simple_ 263. Ugly number
- LeetCode_ String_ Medium_ 151. Reverse the words in the string
- 解决asp.net上传文件时文件太大导致的错误
- awk从入门到入土(23)awk内置变量ARGC、ARGC--命令行参数传递
猜你喜欢

39 简洁版小米侧边栏练习

Gonzalez Digital Image Processing Chapter 1 Introduction

Melody + realsense d435i configuration and error resolution
![[MySQL series] - how much do you know about the index](/img/d7/5045a846580be106e2bf16d7b30581.png)
[MySQL series] - how much do you know about the index

Overview of cloud security technology development

"Ask every day" reentrantlock locks and unlocks

GameFramework制作游戏(一)

【MySQL系列】-索引知多少

Wechat official account official environment online deployment, third-party public platform access
![[Nacos] what does nacosclient do during service registration](/img/76/3c2e8f9ba19e36d9581f34fda65923.png)
[Nacos] what does nacosclient do during service registration
随机推荐
PHP implements non blocking (concurrent) request mode through native curl
39 简洁版小米侧边栏练习
转载----如何阅读代码?
sql server强行断开连接
Live classroom system 05 background management system
awk从入门到入土(20)awk解析命令行参数
39 simple version of millet sidebar exercise
Is it safe for Guolian securities to buy shares and open an account?
GameFramework制作游戏(一)
Spark parameter adjustment and tuning
[thread knowledge points] - spin lock
How many ways can you assign initial values to a two-dimensional array?
Jmeter的随机数函数怎么用
阿里云技术专家邓青琳:云上跨可用区容灾和异地多活最佳实践
"Ask every day" how locksupport realizes thread waiting and wakeup
[C topic] Li Kou 206. reverse the linked list
Splice a field of the list set into a single string
Leetcode-198- house raiding
Yes, UDP protocol can also be used to request DNS server
L1 and L2 regularization