当前位置:网站首页>Learn about redlock
Learn about redlock
2022-06-24 23:03:00 【Comma 8080】
What is? RedLock
RedLock yes Redis The official proposal is based on Redis How to implement distributed lock .
Can guarantee
- Security : Exclusive access , That is, there will always be only one client Can get the lock
- Avoid deadlock : The final client Can get the lock , There will be no deadlock
- Fault tolerance : Important parts redis If the node survives, it can provide services normally
Implement distributed locking on a single node
SET key_name my_random_value NX PX 30000
NX Express if not exist Just set it up and go back True, Otherwise do not set and return False ( Only when the key Set when not present ) PX Indicates the expiration time in milliseconds , 30000 After these milliseconds key Be overdue
Delete own Lock set
Delete reason : Ensure the high efficiency of server resources , You don't have to wait for the lock to automatically expire before deleting
Delete method : Best use Lua Script delete (redis Ensure that no other actions are taken when executing this script , Ensure the atomicity of the operation ), The code is as follows ; Logic is First get key, Delete this if it exists and the value is set by itself key; Otherwise, skip ;
if redis.call("get",KEYS[1]) == ARGV[1] then
return redis.call("del",KEYS[1])
else
return 0
end
Realization RedLock Algorithm
Algorithm flow , Open five nodes , To claim the lock ,client Operations performed
- Get the current time in milliseconds
- client Try using the same key,value Get all redis Service lock , In the process of acquiring a lock, the acquisition time is much shorter than the lock expiration time , Avoid long waits for a closed redis service , And try to get the next instance
- client By subtracting the time of the first step after obtaining all the locks that can be obtained , This time difference is less than TTL Time and at least three redis Instance successfully acquired lock , Only in this way can we really get the lock successfully
- If the lock is acquired successfully , Then the effective time of the lock is TTL Subtract the time difference from step 3
- If the lock acquisition fails , Then it will start to unlock all redis example , Because we have obtained less than 3 A lock , Must be released to prevent impact on other client Get the lock

Failure to retry
When one client When getting lock failed , You should try again later to avoid multiple client At the same time, apply for lock . At best, there is a client At the same time, lock applications are initiated to all services
Lock acquisition failure and lock acquisition failure client Release the lock in time after completing the task
Release the lock
To release a lock, just issue the command to release the lock to all instances , It's not necessary to consider whether the lock can be released successfully ;
Reference article
Redlock(redis Distributed lock ) Principle analysis - RGC - Blog Garden (cnblogs.com)
边栏推荐
- 开发规范~参数校验异常、异常返回提示切面
- High level application of SQL statements in MySQL database (II)
- 2022 simulated 100 questions and simulated examination of high-altitude installation, maintenance and demolition
- 【ROS玩转Turtlesim小海龟】
- Development specification - parameter verification exception, exception return prompt section
- Recommended course: workplace writing training
- 剑指 Offer 42. 连续子数组的最大和
- Spark 离线开发框架设计与实现
- 2022年高压电工考试模拟100题及在线模拟考试
- Canvas to add watermark to pictures
猜你喜欢
随机推荐
O (n) complexity hand tear sorting interview questions | an article will help you understand counting sorting
Servlet
China smallpox vaccine market trend report, technical innovation and market forecast
Research Report on solar battery charger industry - market status analysis and development prospect forecast
New, Huawei cloud Kaitian apaas
机器学习编译入门课程学习笔记第一讲 机器学习编译概述
Win10 or win11 printer cannot print
[WSL] SSH Remote Connection and host port forwarding configuration
Leetcode: calculate the number of elements less than the current element on the right (sortedlist+bisect\u left)
Dynamic memory management (1)
Analyze the implementation process of oauth2 distributed authentication and authorization based on the source code
Learning bit segment (1)
LeetCode Algorithm 剑指 Offer II 027. 回文链表
How to integrate Huawei cloud function services in fluent
canvas 实现图片新增水印
剑指 Offer 13. 机器人的运动范围
vulnhub Vegeta: 1
Leetcode: push domino (domino simulation)
Memory alignment of structures
Solution to the login error of tangdou people









