How to achieve Zookeeper distributed lock

How to achieve Zookeeper distributed lock

Tags: Zookeeper distributed


Important issues to consider implementation of distributed lock

1. The three core elements

Lock, unlock, lock timeout

2. The three questions

  • To ensure atomicity operations, and lock out the locking operation to be performed is finished disposable
  • Lock to prevent accidental deletion
  • On the basis of accidentally deleted on the addition of a daemon thread, continued life for the lock.

What is a temporary order of nodes


Zookeeper data storage structure is like a tree, the tree consists of nodes, this node is called Znode. Znode divided into four types.

Persistent node (Persistent)

The default node type, create a node client and Zookeeper after disconnected, the node still exists.

2. Persistence order of nodes (Persistent Sequential)

The so-called order of nodes, that is, when creating a node, Zookeeper numbered according to the name of the node node creation time of order.

Order node lasting

3. Temporary node (Ephemeral)

And lasting node On the contrary, when creating a node client and Zookeeper disconnected temporarily node will be deleted.

Create a temporary node
Disconnects the client
Delete the temporary node

4. The temporal order node (Ephemeral Sequential)

When you create a node, Zookeeper numbered according to the time order of creation to the node name; a node is created when the client and Zookeeper disconnected temporarily node will be deleted.


Zookeeper distributed lock principle to achieve the

It has been said above four types of Znode, which last type temporary order of nodes is the most conducive to achieving Zookeeper distributed lock.


1. Obtain lock

  • First, create a lasting node ParentLock in Zookeeper them. When the first client wants to acquire a lock of an operational data, you need to resume a temporary order of nodes under the persistent node Lock1.

Client 1 tries to obtain the

  • After that, 客户端1look for ParentLockall the interim order of the nodes below and in accordance with the size of the sort, they created their own judgment node Lock1is not a highest order. If it is the first node, the successful lock.

The client successfully 1

  • 此时, 客户端2 前来获取该项数据的锁, 则在ParentLock下再创建一个临时顺序节点Lock2.

Client 2 attempts to obtain a lock

  • 客户端2 查找ParentLock下面所有的临时顺序节点并排序,发现自己的Lock2节点并不是最靠前的. 于是客户端2向排序仅仅比它靠前的Lock1注册Watcher, 用于监听Lock1动态. 这意味着 Lock2抢锁失败, 进入等待状态.

Lock2 grab lock failure

  • 此时, 如果有一个客户端3前来获取锁, 则在ParentLock下在创建一个临时顺序节点Lock3.

Lock3 attempt to obtain a lock

  • 客户端3查找ParentLock下面所有的临时顺序节点并排序, 判断自己所创建的节点Lock3是不是顺序最靠前的一个, 结果发现Lock3不是最靠前的. 于是客户端3同样抢锁失败, 进入了等待状态.

Lock grab lock failure

  • 这个时候客户端1得到了锁, 客户端2监听了客户端1, 客户端3监听了客户端2. 这样刚好形成一个等待的队列.

2. 释放锁

释放锁有两种情况

2.1 任务完成, 客户端显示释放

  • 当任务完成时, 客户端1会显示的调用删除节点Lock1的指令.

Client 1 releases the lock

2.2 任务执行过程中, 客户端崩溃

  • 获得锁的客户端1在执行任务的过程中, 如果崩溃, 则会断开和Zookeeper服务器的链接, 根据临时节点的特性, 相关联的Lock1会随之自动删除.

1 client crashes, an automatic lock release

3. 获得锁

  • Since 客户端2've been listening Lock1state, this time found Lock1written off, 客户端2will immediately receive notification. This time 客户端2will query again ParentLockall the nodes under the confirmed node created yourself is not the smallest node, if it is the smallest of the successful lock.

The client successfully locks 2

The same can be pushed to客户端3


to sum up

Compare Zookeeper distributed lock and Redis

Distributed Lock advantage Shortcoming
Zookeeper 1. There encapsulated frame, easy to implement.
2. Wait lock mechanism ( Watcher), can increase the efficiency of the grab locks, many benefits
Performance adding and removing nodes is relatively low
Redis SetAnd Delperformance is relatively high (after all, the key database, Hash) 1. To achieve complex and require consideration of the atom, etc. mistakenly deleted.
Mechanism 2 without waiting for the lock, the lock can only be through the client and the like spin, inefficient.

Guess you like

Origin www.cnblogs.com/A-FM/p/11440656.html