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

Example Analysis of distributed Lock and Source Code based on Zookeeper

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces you based on Zookeeper distributed lock and source code example analysis and analysis, the content is very detailed, interested friends can refer to, hope to be helpful to you.

About the implementation of distributed locks by Redis in a distributed environment, using Redis to achieve distributed locks, today I will lead you to familiarize yourself with the implementation of distributed locks with zookeeper.

Before we learn about distributed locks, let's think about what business scenarios will use distributed locks and what should we pay attention to when designing distributed locks?

Introduction to distributed Lock

1. In what business scenarios will distributed locks be used

When multiple clients access the same resource on the server, you need to ensure the consistency of the data, such as the second kill system. Take a chestnut:

The number of a commodity in the system is 5, when the second kill time comes, there will be a large number of users snapping up this product, instantly resulting in a very large concurrency. The normal purchase process is:

Step1, users place orders

Step2, judge whether the quantity of goods is sufficient.

Step3, if sufficient, inventory--

Step4, if the stock is not enough, the second kill fails.

Suppose there is only one item left at this time, user A places an order for the goods, the number of goods is enough, the order is successful, the system has not had time to reduce inventory, and user B also places an order for the same item, and the number of goods is still 1 at this time. Finally, the inventory of the system will be reduced twice, resulting in the phenomenon of oversold goods. At this point, it is necessary to lock the user's order-> inventory reduction step, so that the operation becomes an atomic operation. In a stand-alone or single-process environment, using ReentrantLcok or synchronized of JDK is sufficient, but due to the high concurrency of the second kill system, it is easy for a single machine to downtime because it cannot withstand such pressure. In this case, multiple servers and processes are needed to support this business. ReentrantLcok or synchronized under a single machine are useless here. In this case, you need a distributed lock to ensure that only one user accesses shared resources in a certain period of time.

2. Matters needing attention of distributed locks

A. efficient acquisition and release of locks

B. in the case of network instability, interruption or downtime, the lock should be released automatically to prevent self-locking.

C. it has the characteristic of blocking lock, even if the lock is not acquired, it will block waiting.

D. It has the feature of non-blocking lock, that is, if the lock is not acquired, it will directly return the failure to acquire the lock.

E. With reentrant lines, the same thread can acquire locks multiple times

Implementation of distributed Lock with zookeeper

Zookeeper will not be introduced here. We can use the feature of sequential temporary nodes of zk to implement distributed locking. The ideas are as follows:

1. When acquiring the lock, create a node under the zk directory to determine whether the node's need is the smallest among its sibling nodes. If it is the smallest, the lock is acquired successfully.

2. If the lock is not the smallest, the lock has been occupied. You need to register the listener compared to your own smaller node. If the lock is released, you can listen to the release lock event to determine whether the node is the smallest in its sibling node. If so, acquire the lock.

On the Zookeeper-based implementation of distributed locks and source code example analysis and analysis is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Internet Technology

Wechat

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

12
Report