Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize distributed Lock

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains how to achieve distributed lock, the content is clear, interested friends can learn, I believe it will be helpful after reading.

There are three ways to implement distributed locks:

1. Implementation of distributed Lock based on Database

two。 Implementation of distributed lock based on cache (Redis, etc.)

3. Implementation of distributed Lock based on Zookeeper

First, implement distributed lock based on database

1. Pessimistic lock

Take advantage of select... Where... For update exclusive lock

Note: other additional functions are basically the same as the implementation. What you need to note here is "where name=lock". The name field must be indexed, otherwise the table will be locked. In some cases, such as a small table, the mysql optimizer will not move the index, resulting in table locking problems.

two。 Optimistic lock

The biggest difference between the so-called optimistic lock and the previous one is that based on the idea of CAS, it is not mutually exclusive, it will not produce lock waiting and consume resources, and it is considered that there is no concurrency conflict in the process of operation, which can only be detected after the failure of update version. Our rush to buy, second kill is to use this kind of implementation to prevent overselling.

Optimistic locking is achieved by adding an incremental version number field

Second, implement distributed lock based on cache (Redis, etc.)

1. Use the command to introduce:

(1) SETNX

SETNX key val: if and only if key does not exist, set a string whose key is val and returns 1; if key exists, it does nothing and returns 0.

(2) expire

Expire key timeout: set a timeout for key in second, after which the lock will be released automatically to avoid deadlock.

(3) delete

Delete key: deleting key

These three commands are mainly used when using Redis to implement distributed locks.

two。 Realize the idea:

(1) when acquiring a lock, use setnx to add a lock, and use the expire command to add a timeout for the lock. After this time, the lock is automatically released. The value of the lock is a randomly generated UUID, which is used to determine when the lock is released.

(2) when acquiring the lock, it also sets an acquisition timeout, and if it exceeds this time, the acquisition lock is abandoned.

(3) when releasing the lock, it is judged by UUID whether it is the lock or not. If it is the lock, delete is executed to release the lock.

Third, realize distributed lock based on Zookeeper.

ZooKeeper is an open source component that provides consistency services for distributed applications. It has a hierarchical file system directory tree structure, which stipulates that there can be only one unique file name in the same directory. The steps to implement a distributed lock based on ZooKeeper are as follows:

(1) create a directory mylock

(2) if thread A wants to acquire the lock, create a temporary sequential node under the mylock directory.

(3) get all the child nodes in the mylock directory, and then get the sibling nodes smaller than yourself. If they do not exist, the sequence number of the current thread is the lowest and the lock is obtained.

(4) Thread B gets all the nodes, determines that it is not the smallest node, and sets the node that listens less than it does.

(5) Thread A finishes processing and deletes its own node. Thread B listens to the change event and determines whether it is the smallest node. If so, it gets the lock.

Here we recommend an open source library Curator of Apache, which is a ZooKeeper client, the InterProcessMutex provided by Curator is the implementation of distributed lock, the acquire method is used to acquire the lock, and the release method is used to release the lock.

Advantages: it has the characteristics of high availability, reentrant and blocking lock, which can solve the problem of failure deadlock.

Disadvantages: because nodes need to be created and deleted frequently, the performance is not as good as that of Redis.

Fourth, contrast

Implementation of Database distributed Lock

Disadvantages:

1.db has poor operation performance and the risk of locking the table.

two。 When a non-blocking operation fails, polling is required to consume cpu resources

3. Not commit for a long time or polling for a long time may take up more connection resources

Implementation of Redis (cache) distributed lock

Disadvantages:

1. Lock deletion failure expiration time is difficult to control

two。 Non-blocking. If the operation fails, polling is required, which takes up cpu resources.

Implementation of ZK distributed Lock

Cons: performance is not as good as the redis implementation, mainly because write operations (acquiring lock release locks) need to be performed on Leader and then synchronized to follower.

In short: ZooKeeper has good performance and reliability.

From the point of view of the difficulty of understanding (from low to high) database > cache > Zookeeper

From the perspective of implementation complexity (from low to high) Zookeeper > = cache > database

From a performance perspective (from high to low) caching > Zookeeper > = database

From the perspective of reliability (from high to low) Zookeeper > cache > database

After reading the above content, do you have a further understanding of how to achieve distributed locking? if you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report