当前位置:网站首页>Knowledge points of common interview questions: distributed lock
Knowledge points of common interview questions: distributed lock
2022-07-29 10:01:00 【Programming thousand paper cranes】
1 Basic concept of distributed lock
1.1 Basic concepts
Lock the code of cache query database , There are two ways :
- Local lock Process lock Have limitations , It can only take effect in the same project , Cannot control the way in another project . Ensure that only one thread can access the shared resources at the same time for example : Lock the method of querying the database , At the same time, only one person can query the database .
- Distributed lock Distributed lock In a distributed environment , The system is deployed in multiple machines , A kind of lock to realize distributed mutual exclusion of multiple processes . To ensure that multiple processes can see the lock , Locks are stored in public storage ( such as Redis、Memcache、 Database and other three-party storage ), To achieve multiple processes concurrent access to the same critical resource , Only one process can access the shared resource at the same time , Ensure data consistency . for example : There are two servers , Among them, there are methods to query database classification data , At the same time , Only one thread in a project can query successfully .
2 redis Distributed lock
2.1 Implementation logic of distributed lock
Basic logic : All clients go to one place at the same time “ Occupy the pit ”, If the current client occupies a hole , Just execute the business logic , If not, you must wait , Until other clients release the lock .
2.2 Basic implementation
stay Redis How to occupy the pit ?【 Lock 】
set command :set nx


/**
* Distributed lock
*/
public List<Node> getDataByRedisLock(){
List<Node> nodeList = null;
//1. Get the lock Executing business code set nx (setIfAbsent)
Boolean lock = redisTemplate.opsForValue().setIfAbsent("redis-lock", "100");
if (lock){
// Locking succeeded in querying the database
nodeList = getNodesByMysql();
// Query is completed and unlocked
redisTemplate.delete("redis-lock");
}else {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
// retry
getDataByRedisLock();
}
return nodeList;
}
Problem analysis :
After lock up If the code is abnormal What if the final lock is not released and the deadlock is caused ? While locking Set expiration time
- Lock
- Set expiration time
If you want to lock Need assurance Locking and setting expiration time are atomic
2.3 Locking atomicity
stay Redis Atomic locking and expiration time settings are supported in


above java Method setIfAbsent Will be converted to redis Of set key value EX 10 NXredis Every single command in is an atomic operation , Or it all works , Or all failed , Will not be disturbed by other commands .redis Why is the command of atomic operation ?redis The bottom layer is single thread execution of commands . Commands from all clients to redis After that, it will be put into a queue and executed one by one , There is no other thread interference in the middle . therefore redis A command of is thread safe .
Why? redis Of QPS So is it high or single thread ?
redis The bottom layer adopts non blocking multiplexing io Model , You can use a single thread to handle high traffic .
2.4 Lock deletion optimization
problem :
- In practice , Threads 1 Lock up , However, it may take a long time to implement the business , So threads 1 Your lock will expire by itself
- in the meantime , Threads 2, Locking success
- Then the thread 1 After executing the business code , Execute the operation of deleting the lock , At this point, the thread 2 The lock of is deleted How to ensure that the wrong lock will not be deleted ?
Solution :
When it's locked , Set the value to UUID, For each user UUID Different , As key Corresponding value

Problem analysis :
The process of deleting locks is not atomic . Deleting a lock is a two-step process
- Get value UUID
- Delete lock
If after getting the value , Before deleting the lock , The lock of the current thread has expired , Other threads set distributed locks , Then there's a problem .
if Judgment can be made by , Execute the code to delete the lock ,Redis It will delete the locks of other threads
2.5 Delete lock atomicity
LUA Script

if redis.call("get",KEYS[1]) == ARGV[1] then return redis.call("del",KEYS[1]) else return 0 end
lua Script :Lua Is a lightweight and compact scripting language , Using standard C Language written and open in source

Problem analysis :
The above code realizes the atomicity of locking and unlocking , But there are problems : Before the business code is executed , Distributed locks should not expire .
The expiration time of the lock is longer + finally Guaranteed to unlock

Automatic renewal
redis Distributed lock final code
/**
* Distributed lock
*/
public List<Node> getDataByRedisLock(){
List<Node> nodeList = null;
// Generate UUID
String uuid = UUID.randomUUID().toString();
//1. Get the lock Executing business code set nx (setIfAbsent)
//setIfAbsent If the expiration time is set for this method The bottom is set Ex nx Locking and expiration time settings are atomic
Boolean lock = redisTemplate.opsForValue().setIfAbsent("redis-lock", uuid,100,TimeUnit.SECONDS);
if (lock){
System.out.println(" Get the lock ");
// Locking succeeded in querying the database
try {
nodeList = getNodesByMysql();
}finally {
// Query is completed and unlocked
String script = "if redis.call(\"get\",KEYS[1]) == ARGV[1] then return redis.call(\"del\",KEYS[1]) else return 0 end";
/**
* Parameters 1 Script
* Parameters 2 To delete key
* Parameters 3 uuid
*/
redisTemplate.execute(
new DefaultRedisScript<Long>(script,Long.class),
Arrays.asList("redis-lock"),
uuid
);
}
}else {
try {
System.out.println(" Try again without obtaining the lock ");
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
// retry
getDataByRedisLock();
}
return nodeList;
}
redisson There is a watchdog in it watchDog The lock duration can be automatically renewed .
The underlying principle of the watchdog : The client to redis After adding the lock , Will start a new thread ( watchdog ), This thread is a timed thread , The length of each meeting /3 Clearance of redis Issue a renewal order , When renewing, it will check whether the lock is the lock of the current thread , If so, renew , Don't just terminate the watchdog thread . When the main thread cannot be contacted redis Or after downtime , The watchdog thread will also be out of contact redis, Therefore, deadlock caused by infinite renewal can be avoided .
2.6 redisson
Redisson yes Redis Officially recommended Java Version of Redis client . It provides a lot of functions , It's also very powerful . It has built-in distributed lock tools and methods, which are very convenient to use, just like using local locks . The above content is redission The underlying principle of distributed locks .
Project use redisson Implement distributed locks :
- Introduce dependencies
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.15.6</version>
</dependency>
- To configure bean object
@Configuration
public class RedissonConfig {
@Bean
public RedissonClient redissonClient(){
Config config = new Config();
config.setTransportMode(TransportMode.NIO);
SingleServerConfig singleServerConfig = config.useSingleServer();
// It can be used "rediss://" To enable SSL Connect
singleServerConfig.setAddress("redis://linux100:6379");
RedissonClient redisson = Redisson.create(config);
return redisson;
}
}
- Use redisson
@RestController
public class UserController {
@Autowired
private RedissonClient redissonClient;
@GetMapping("/test")
public String test() {
RLock lock = redissonClient.getLock("test-lock");
try {
System.out.println(" Locking success !!!");
Thread.sleep(10000);
} catch (Exception e) {
e.printStackTrace();
}finally {
System.out.println(" Lock release successful !!!");
lock.unlock();
}
return "success";
}
}
边栏推荐
- Shell笔记(超级完整)
- 英特尔联合Datawhale,发布学习项目!
- [Apple Developer account]06 after transferring the developer account, the annual fee of the developer is automatically renewed
- Manually build ABP framework from 0 -abp official complete solution and manually build simplified solution practice
- 2021年CS保研经历(四):西交软院预推免、信工所三室预推免
- Skiasharp's WPF self drawn bouncing ball (case version)
- Encyclopedia of introduction to machine learning - 2018 "machine learning beginners" official account article summary
- Logistic regression of machine learning
- What is Cartland number? What are the applications?
- Anfulai embedded weekly report no. 273: 2022.07.04--2022.07.10
猜你喜欢

Harmonyos 3.0 release!

Comprehensively design an oppe home page -- the bottom of the page

The latest translated official pytorch easy introduction tutorial (pytorch version 1.0)

Talk about multithreaded concurrent programming from a different perspective without heap concept

这是一份不完整的数据竞赛年鉴!

What kind of framework is friendly to developers?
![[ts]typescript learning record pit collection](/img/4c/14991ea612de8d5c94b758174a1c26.png)
[ts]typescript learning record pit collection

手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践

i. Mx6ull driver development | 32 - manually write a virtual network card device

综合设计一个OPPE主页--页面的底部
随机推荐
div 水平排列
最新翻译的官方PyTorch简易入门教程(PyTorch1.0版本)
机器学习之线性回归(最小二乘法手写+sklearn实现)
Unity3d空包打apk报错汇总
综合设计一个OPPE主页--页面的底部
JS to achieve full screen effect
TMS320C6000_ Tms320f28035 Chinese data manual
RTMP supports h265 streaming
What kind of framework is friendly to developers?
The function of that sentence
Dimensionality reduction and mathematical modeling after reading blog!
CS assurance and research experience in 2021 (IV): pre promotion and exemption of Xijiao soft Research Institute and the third room of Information Technology Institute
2021年CS保研经历(六):系统填报 + 一些感想
【C语言】扫雷(递归展开 + 标记功能)
Network security (6)
这是一份不完整的数据竞赛年鉴!
[Apple Developer account]06 after transferring the developer account, the annual fee of the developer is automatically renewed
Basic operations of OpenCV image processing
Summary of JD internship written examination questions
熊市下PLATO如何通过Elephant Swap,获得溢价收益?