当前位置:网站首页>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 .
边栏推荐
- NEFU118 n! How many zeros are there after [basic theorem of arithmetic]
- USB interface electromagnetic compatibility (EMC) solution
- Unity性能优化------渲染优化(GPU)之Occlusion culling(遮挡剔除)
- LeetCode 240. 搜索二维矩阵 II medium
- generic paradigm
- Reading notes of lifelong growth (I)
- IJCAI 2022杰出论文公布,大陆作者中稿298篇拿下两项第一
- The reverse order pairs in the "sword finger offer" array
- adb命令 (安装apk包格式:adb install 电脑上apk地址包名)
- cap理论和base理论
猜你喜欢

Selenium 报错:session not created: This version of ChromeDriver only supports Chrome version 81

基于FIFO IDT7202-12的数字存储示波器

STM32 can communication filter setting problem

Design scheme of digital oscilloscope based on stm32

修改frameworks资源文件如何单编

MySQL 面试40连问,面试官你再问下去我可要翻脸了

See "sense of security" in uncertainty Volvo asked in 2022

移动端使用vantUI的list组件,多个tab项来回切换时,列表加载多次导致数据无法正常展示

Unity performance optimization ----- occlusion culling of rendering optimization (GPU)

谷歌团队推出新Transformer,优化全景分割方案|CVPR 2022
随机推荐
《剑指Offer》 合并两个排序的链表
积分运算电路的设计方法详细介绍
Internship: compilation of other configuration classes
Unity mouse controls the first person camera perspective
Jmeter录制接口自动化
USB2.0接口的EMC设计方案
Nefu117 number of prime numbers [prime number theorem]
Unity performance optimization ----- LOD (level of detail) of rendering optimization (GPU)
Selenium 报错:session not created: This version of ChromeDriver only supports Chrome version 81
网络设备硬核技术内幕 路由器篇 (10) CISCO ASR9900拆解 (四)
EMC design scheme of RS485 interface
3.3-5v conversion
Unity最简洁的对象池实现
《剑指Offer》数组中的逆序对
STM32之CAN ---CAN ID过滤器分析
关于印发《深圳市工业和信息化局绿色制造试点示范管理暂行办法》的通知
Passive income: return to the original and safe two ways to earn
微信小程序实现音乐搜索页面
Notice of Shenzhen Municipal Bureau of human resources and social security on the issuance of employment related subsidies for people out of poverty
基于stm32的数字示波器设计方案