Zookeeper Application scenario: Distributed lock implementation

Source: Internet
Author: User

In the Zookeeper application scenario there is a description of the distributed cluster configuration file synchronization problem, imagine if there are 100 machines at the same time a file on the same machine to modify, how to ensure that the text will not be confused, this is the simplest distributed lock, this article describes the use of ZK implementation of distributed locks. Here is the implementation step of the write lock

Distributed write lock
Create a persistent type of znode,/locks/write_lock

    • Client Creation sequence| The Znode of the ephemeral type, whose name is Lockid, creates the Znode is/locks/write_lock/lockid0000000001
    • Call GetChildren () do not set watcher get/locks/write_lock under Znode list
    • Judge yourself Step 2 create Znode is not the smallest one in the Znode list, if it is on behalf of the lock, if not go down
    • Call exists () to determine the node number that you created for step 2 is small 1 znode node (that is, the smallest znode in the Znode node list), and set Watcher, if exists () returns FALSE, perform step 3
    • if exists () returns True, wait for the ZK notification to return to step 3 in the return function


Releasing a lock is either deleting the Znode node or disconnecting it.

* Note: GetChildren () in step 2 above does not set watcher because the herd effect is prevented, and if the GetChildren () is set to Watcher, then a jitter of the cluster will be notified. In the entire distributed lock competition process, a large number of repeated operations, and most of the results of the operation is to determine that they are not the smallest number of nodes, so as to continue to wait for the next notification-This obviously does not seem very scientific. The client is unnecessarily accepting excessive and self-unrelated event notifications, which can have a significant performance impact on the server if the cluster is large, and if a client with multiple nodes at the same time disconnects, the server will send a large number of event notifications like the rest of the clients- This is called the herd effect.

Zookeeper applicable scenario: distributed lock implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.