当前位置:网站首页>Implementation of redis distributed lock
Implementation of redis distributed lock
2022-08-04 08:15:00 【Zero shear】
I. Introduction
When we modify the existing data in the system, we need to read it first, and then modify and save it. At this time, it is easy to encounter concurrency problems.Since modification and saving are not atomic operations, some operations on data may be lost in concurrent scenarios.In a single-server system, we often use local locks to avoid problems caused by concurrency. However, when services are deployed in a cluster, local locks cannot take effect between multiple servers. At this time, distributed locks are required to ensure data consistency.accomplish.

Second, the mainstream implementation scheme of distributed locks
Distributed lock based on database
Cache based (Redis, etc.)
Based on Zookeeper
Each distributed lock solution has its own advantages and disadvantages:
Performance: redis is the highest
Reliability: zookeeper highest
3. Implementing distributed locks based on redis
Redis locks mainly use Redis's setnx command
- Locking command: SETNX key value, when the key does not exist, set the key and return success, otherwise return failure.KEY is the unique identifier of the lock, which is generally named according to the business.
- Unlock command: DEL key, releases the lock by deleting the key-value pair, so that other threads can acquire the lock through the SETNX command.
- Lock timeout: EXPIRE key second, set the timeout time of the key to ensure that even if the lock is not explicitly released, the lock can be automatically released after a certain period of time to avoid the resource being locked forever.
There are some problems with the above lock implementation:
1-SETNX-and-EXPIRE-non-atomic
If SETNX is successful, after setting the lock timeout time, the server hangs up, restarts or has network problems, etc., resulting in the EXPIRE command not being executed, and the lock becoming deadlock if the lock timeout time is not set.
Solution:
1. Use Lua script to compose SETNX and EXPIRE into atomic operation
2. Use set key value ex second nx
2, lock removal
If thread A successfully acquires the lock and sets an expiration time of 30 seconds, but the execution time of thread A exceeds 30 seconds, the lock is automatically released after expiration, and thread B acquires the lock at this time; then the execution of A is completed, and thread A usesDEL command to release the lock, but the lock added by thread B has not been executed yet, and the lock added by thread B is actually released by thread A.
Solution:
When set acquires the lock, set a specified unique value (for example: uuid), and acquire this before releasingvalue, determine whether it is your own lock, and if so, release the lock

3. Timeout unlocking leads to concurrency (similar to the previous problem)
If thread A successfully acquires the lock and sets the expiration time to 30 seconds, but the execution time of thread A exceeds 30 seconds, the lock expires and is automatically released. At this time, thread B acquires the lock, and thread A and thread B execute concurrently.

Concurrency of two threads A and B is obviously not allowed. Generally, there are two ways to solve this problem:
- Set the expiration time long enough to ensure that the code logic can complete before the lock is released.
- Add a daemon thread for the thread that acquires the lock, and increase the valid time for the lock that will expire but not be released.
4. The deletion operation lacks atomicity
If thread A successfully acquires the lock and sets the expiration time to 30 seconds, but the execution time of thread A is exactly 30 seconds, the lock is automatically released when it expires. At this time, A just completes the business logic and makes a judgment to release the lock.But before the lock is released, thread B acquires the lock again, and then thread A uses the DEL command to release the lock, but the lock added by thread B has not been executed yet, and the lock added by thread B actually released by thread A.
Solution:
Use Lua scripts to ensure atomicity of locking and releasing locks.
5. How does Lua script ensure its atomicity
Redis embeds a lua environment (very small) to run lua scripts. When running lua scripts, other steps and commands will not be run. It is similar to adding locks to the code executing lua scripts, so its atomicity is guaranteed..
Four. Summary
To ensure that distributed locks are available, we at least make sureThe implementation of the lock also satisfies the following four conditions:
- Mutually exclusive.At any time, only one client can hold the lock.
- No deadlock occurs.Even if a client crashes while holding the lock and does not actively unlock it, it is guaranteed that other clients can lock in the future.
- The ringer must also be tied to the bell.Locking and unlocking must be done by the same client, and the client cannot unlock the locks added by others.
- Locking and unlocking must be atomic.
边栏推荐
- js - the first letter that appears twice
- RHCSA第五天
- 【电脑录制屏】如何使用bandicam录游戏 设置图文教程
- Interpretation of new features | MySQL 8.0 online adjustment REDO
- ShuffleNet v2 network structure reproduction (Pytorch version)
- 从底层看 Redis 的五种数据类型
- 最近的一些杂感-20220731
- 安装GBase 8c数据库集群时,报错误码:80000306,显示Dcs cluster not healthy。怎么处理错误呢?
- MMDetection finetune
- 研究性学习专题 3_LL(1)语法分析设计原理与实现
猜你喜欢

【JS 逆向百例】某网站加速乐 Cookie 混淆逆向详解

Thread类的基本使用。

How many assertion methods are commonly used in JMeter?

【虚幻引擎UE】UE5基于Gltf加载插件实现gltf格式骨骼动画在线/本地导入和切换

关于#sql#的问题:后面换了一个数据库里面的数据就不能跑了

25.时间序列预测实战

小程序如何使用订阅消息(PHP代码+小程序js代码)

线程的状态

Yolov5 replaces the backbone network of "Megvii Lightweight Convolutional Neural Network ShuffleNetv2"

Lightweight Backbone VGNetG Achieves "No Choice, All" Lightweight Backbone Network
随机推荐
C Language Lectures from Scratch Part 6: Structure
GIS数据与CAD数据间带属性字段互相转换还原工具,解决ArcGIS等软件进行GIS数据转CAD数据无法保留属性字段问题
Secondary network security competition C module MS17-010 batch scanning
【CNN基础】转置卷积学习笔记
高等代数_证明_两个矩阵乘积为0,则两个矩阵的秩之和小于等于n
【论文笔记】Delving into the Estimation Shift of Batch Normalization in a Network
MySQL 8.0.29 详细安装(windows zip版)
[STM32] STM32F103 series name and package, memory
图的基本概念
微信消息从发送到接收,经历了什么?如何防止丢包
高等代数_证明_对称矩阵属于不同特征值的特征向量正交
给Unity Behavior Designer(Unity行为树) 的Can See Object 画圆锥辅助图
[Paper Notes] - Low Illumination Image Enhancement - Supervised - RetinexNet - 2018-BMVC
经典动态规划问题的递归实现方法——LeetCode39 组合总和
研究性学习专题 3_LL(1)语法分析设计原理与实现
Cross-species regulatory sequence activity prediction
并查集介绍和基于并查集解决问题——LeetCode 952 按公因数计算最大组件大小
24.循环神经网络RNN
解决:Hbuilder工具点击发行打包,一直报尚未完成社区身份验证,请点击链接xxxxx,项目xxx发布H5失败的错误。
占位,稍后补上