当前位置:网站首页>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.
边栏推荐
猜你喜欢
form表单提交到数据库储存
【STM32】STM32F103系列名称与封装、内存
redis---分布式锁存在的问题及解决方案(Redisson)
经典递归回溯问题之——解数独(LeetCode 37)
【虚幻引擎UE】UE5实现WEB和UE通讯思路
Secondary network security competition C module MS17-010 batch scanning
华为设备配置VRRP与NQA联动监视上行链路
Distributed Computing Experiment 4 Random Signal Analysis System
最近的一些杂感-20220731
DeLighT:深度和轻量化的Transformer
随机推荐
【论文笔记】Understanding Long Programming Languages with Structure-Aware Sparse Attention
微信消息从发送到接收,经历了什么?如何防止丢包
24.循环神经网络RNN
Detailed explanation of TCP protocol
金仓数据库 KDTS 迁移工具使用指南 (4. BS 版使用说明)
华为设备配置VRRP与路由联动监视上行链路
leetcode 22.8.1 二进制加法
2022爱分析· 银行数字化厂商全景报告
Convert callback function to Flow
1161. Maximum Level Sum of a Binary Tree
leetcode 22.7.31(1)两数之和 (2)整数除法
GBase 8c数据库集群中,怎么替换节点呢?比如设置A节点为gtm,换到B节点上。
玩转TypeScript对象、对象作为参数进行函数传递、接口和内置对象[无敌态]
关于#sql#的问题:后面换了一个数据库里面的数据就不能跑了
智汇华云 | 华云软件定义网络 DCI介绍
C# 实用的第三方库
「PHP基础知识」转换数据类型
data:image/jpg; base64 format data is converted to image
新特性解读 | MySQL 8.0 在线调整 REDO
一天搞定JDBC02:开启事务