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

What are the differences between zk and redis distributed locks

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

Share

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

This article is about the differences between zk and redis distributed locks. Xiao Bian thinks it is quite practical, so share it for everyone to make a reference. Let's follow the editor and have a look.

Redis implements distributed locks

Set nx (set not exist, if the key value is empty, normal setting, return 1, otherwise will not be set and return 0) operation according to lockKey area. If the setting is successful, it means that the lock has been obtained, otherwise, the lock has not been obtained.

2. If the lock is not acquired, go to Redis to get the corresponding value of the key. We store a timestamp (expressed in milliseconds, t1) on the key. In order to avoid deadlock and other clients occupying the lock for more than a certain time (5 seconds), we use the current timestamp of the client to compare with the stored timestamp.

3. If the key usage time limit is not exceeded, false is returned, indicating that others are occupying the key and cannot be forced to use it; if the time limit has expired, we can unlock it and use our timestamp instead of the value of the field.

4. However, if after setnx fails, the value cannot be obtained, indicating that the lock has been released before the operation, in which case, the best way is to execute setnx method again to obtain its value to obtain the lock.

Release lock: delete key in redis

Zookeeper implements distributed locks

Based on temporary order nodes:

1. The client calls the create() method to create a node named "locknode/guid-lock-". Note that the creation type of the node needs to be set to EPHEMERAL_SEQUENTIAL.

2. The client calls the getChildren("locknode") method to get all the child nodes that have been created.

3. After the client obtains all child node paths, if it finds that the node it created in step 1 has the lowest sequence number among all nodes, it is considered that the client has obtained the lock.

4. If the node created is not the node with the smallest sequence number among all nodes, then monitor the node with the largest sequence number smaller than the node created by yourself and enter waiting. Until the next time the monitored child node changes, the child node acquisition is performed to determine whether to acquire the lock.

The process of releasing the lock is relatively simple, just delete the child node you created.

Difference:

redis distributed lock, in fact, you need to constantly try to acquire the lock, compare the consumption performance

zk distributed lock, can not get the lock, register a listener, do not need to constantly actively try to get the lock, performance overhead is small

Another point is that if the client that redis acquires the lock bugs or hangs, then it can only wait for the timeout period before releasing the lock; and zk, because it is a temporary znode, as long as the client hangs, znode is gone, and the lock is automatically released at this time.

Thank you for reading! What are the differences between zk and redis distributed locks? I hope the above content can be of some help to everyone, so that everyone can learn more knowledge. If you think the article is good, you can share it so that more people can see it!

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