当前位置:网站首页>Distributed lock
Distributed lock
2022-07-27 15:21:00 【Hua Weiyun】
Distributed lock
Distributed locking is an important topic in distributed projects , When we use the traditional java When the shared resources cannot be locked , We need to consider distributed locks . So what are the technical solutions of distributed locks ? We can go through redis Distributed locks for , such as redis Of setxnx command , You can also use redisson, The second technical solution is to use zookeeper, What we use is zookeeper Temporary order node for , The third technical solution is to use the primary key or unique index of the database to unlock the lock
redis Distributed lock
Lock command :setnx key value nx ex 10s
key You can set it according to your business ,value It can be a random value , Make sure the whole situation is unique ,nx Express this key Return success when it does not exist , Otherwise, the execution fails , This ensures that the lock will not be locked again when it exists ,ex Used to set key The expiration time of , Prevent the deadlock caused by the failure to release the lock after the program hangs during operation , With the expiration time , Even if the program does not actively release the lock , It's time to expire ,redis Will also let this key Fail to release the lock . If the current business is locked 10s I haven't finished my business ,key It's due , Other threads may lock successfully , It's time to be right key Renew , What you use is watch dog, When deleting a lock, make sure you delete your own lock , It's also in the process of deleting locks key Of value Compare , It is generally used lua Script to release the lock , Guaranteed atomicity
To make a long story short , Both locking and unlocking should ensure atomicity
Use redis Generally speaking, distributed locks redis Both are read-write separated architectures , Data replication is asynchronous , Some data may be lost during master-slave switching , because redis What is guaranteed is the final consistency
zookeeper Distributed lock
zookeeper Distributed locks are used zookeeper Characteristics of temporary sequential nodes , The client executes business logic before zookeeper Create a temporary sequence node in , And then to get zookeeper All the nodes in , If the current node is the smallest, locking succeeds , Otherwise, we have to wait . Releasing the lock is to delete the temporary order node you created , The reason why temporary contact is needed , Yes, ensure connection zookeeper After the service of hangs up, delete the node in time to release the lock .
zookeeper Distributed locks use temporary sequential nodes , If the client and zookeeper A network condition may cause the wrong release of the lock , And if there are too many nodes ,zookeeper Data synchronization between clusters also takes time
Database distributed lock
The distributed locking of database is to use primary key or unique index , Ensure that after the current business adds a piece of data, other businesses cannot add the same data, so as to ensure the success of locking , To release a lock is to delete data
summary
This article introduces the implementation of distributed locks , It's based on redis Distributed lock 、 be based on zookeeper And the distributed lock based on database , It is rarely used in production , If it is used, it will use the framework encapsulated based on these technologies, such as Curator、Seate wait .
边栏推荐
- DIY ultra detailed tutorial on making oscilloscope: (1) I'm not trying to make an oscilloscope
- Wechat applet realizes music search page
- Notice of Nanshan District Civil Affairs Bureau on carrying out the grade evaluation of social organizations in Nanshan District in 2022
- Finally, someone finished all the dynamic planning, linked list, binary tree and string required for the interview
- Leetcode 244周赛-赛后补题题解【西兰花选手】
- Kubernetes CNI 分类/运行机制
- 网络设备硬核技术内幕 路由器篇 (10) CISCO ASR9900拆解 (四)
- 网络设备硬核技术内幕 路由器篇 21 可重构的路由器
- 华为鸿蒙模拟器去除顶部导航栏方法
- STM32之CAN ---CAN ID过滤器分析
猜你喜欢
随机推荐
Leetcode 244周赛-赛后补题题解【西兰花选手】
多线程环境下CountDownLatch的用法
TL431-2.5v基准电压芯片几种基本用法
The mobile terminal uses the list component of vantui. When multiple tab items are switched back and forth, the list is loaded many times, resulting in the failure of normal display of data
深圳市人力资源和社会保障局关于发放脱贫人口就业有关补贴的通知
LeetCode 1143. 最长公共子序列 动态规划/medium
Discussion on STM32 power down reset PDR
多表查询_练习1&练习2&练习3
STM32学习之CAN控制器简介
STM32F10x_硬件I2C读写EEPROM(标准外设库版本)
什么是Tor?Tor浏览器更新有什么用?
Leetcode-1737-满足三条件之一需改变的最少字符数
Lua study notes
Data warehouse project is never a technical project
USB接口电磁兼容(EMC)解决方案
Unity性能优化------DrawCall
移动端使用vantUI的list组件,多个tab项来回切换时,列表加载多次导致数据无法正常展示
LeetCode 面试题 17.21. 直方图的水量 双指针,单调栈/hard
EMC design scheme of USB2.0 Interface
STM32 CAN 通信 滤波设置问题







