当前位置:网站首页>Three implementation methods of distributed lock
Three implementation methods of distributed lock
2022-06-23 09:40:00 【Linging_ twenty-four】
github:https://github.com/Linging24/lock.git
Requirements for distributed lock implementation :
1、 In a distributed environment , Only one client can hold the lock at any time
2、 A deadlock will not occur
3、 Lock your own client , It can only be unlocked by itself
1、 Data based implementation
- Create database lock table :
CREATE TABLE `lock_record` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT ' Primary key ',
`lock_name` varchar(50) DEFAULT NULL COMMENT ' Lock name ',
PRIMARY KEY (`id`),
UNIQUE KEY `lock_name` (`lock_name`)
) ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=utf8;
Ideas : When a thread enters shared code , Insert a record into the database , When threads of other distributed nodes want to access the shared code , You will first query whether such a lock record exists in the database , If it exists, wait for the lock , Otherwise, get the lock , Execute shared code .
2、 be based on redis Realization
- Use setnx Instructions , But there is if after locking , If the program throws an exception , It will cause the lock to never be released , It is possible to set a timeout , If the time comes, the lock will be released , However, it is necessary to ensure that the locking operation and setting the timeout time are atomic operations .
- because redis Generally, it is deployed in a cluster , The first method is only applicable to the single node method , For cluster locking, use redission.
Be careful : Why does the first method only apply to a single node redis, If it is applied to the cluster, what problems will it cause ?
Under Cluster , All write operations are on the primary node , So the locks are all on the primary node , If the master node hangs up , Then the slave node will preempt the lock , At this time, if the primary node has not been down yet , Some clients have acquired locks , And operate resources , And from The client that obtains the lock from the node is also operating on the resource , It is possible that multiple clients are operating unified resources , In this way, resources cannot be locked .
Seven ways to implement :
https://www.cnblogs.com/wangyingshuo/p/14510524.html
3、 be based on zookpeer Realization 
边栏推荐
猜你喜欢

JSP getting started summary

Wechat applet: click the button to switch frequently, overlap the custom markers, but the value does not change
Redis learning notes - slow query analysis

Aiming at the overseas pet market, "grasshand" has developed an intelligent tracking product independent of mobile phones | early project

UEFI source code learning 3.7 - norflashdxe

Web -- Information Disclosure

安装typescript环境并开启VSCode自动监视编译ts文件为js文件

ionic5錶單輸入框和單選按鈕

A 32KB cache with direct mapping Memory exercises after class

Use Base64 to show pictures
随机推荐
Typora set up image upload service
学习SCI论文绘制技巧(F)
Gstore weekly gstore source code analysis (IV): black and white list configuration analysis of security mechanism
[GXYCTF2019]BabySQli
One of the 12 balls is different from the others. Provide a balance and find it three times
Gorm 高级查询
Structure binary tree from inorder and postorder traversal for leetcode topic analysis
A 32KB cache with direct mapping Memory exercises after class
启明星辰华典大数据量子安全创新实验室揭牌,发布两款黑科技产品
High performance computing center nvme / nvme of nvme of overview
【CTF】bjdctf_ 2020_ babyrop
[極客大挑戰 2019]HardSQL
RBtree
Opening, creating and storing files
Learn SCI thesis drawing skills (E)
map的下标操作符
Servlet-02 lifecycle
ionic5表单输入框和单选按钮
[geek Challenge 2019] hardsql
Redis learning notes - redis and Lua