In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Redis and zookeeper in how to achieve distributed locks, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
1. Implementation of distributed lock based on redis
1. Acquire lock
Redis is a NOSQL database in the form of key-value, which is often used as a cache for the server. Starting with redis v2.6.12, the set command begins to change to the following format:
SET key value [EX seconds] [PX milliseconds] [NX | XX]
Except for key and value, EX is the timeout. NX means that the value of key is set only if key does not exist, while XX means that the value of key is set only when the key exists. NX mechanism is the core of distributed lock based on redis. Be able to solve the following issues:
1) Node 1 gets the key, and after setting the timeout, it dies before it can be released. Here, the EX timeout will take effect, and the lock will be released automatically after the timeout.
2) No sooner had the lock been acquired than the timeout was set. Here, setting key and timeout are atomic operations. If this happens, 0 will be returned, that is, the lock will not be acquired.
two。 Release lock
In order to solve the problems caused by non-atomic operations, lua scripts are often used. The operation of a lua script is considered atomic, similar to a transaction. The pseudo code is as follows:
Second, the implementation of distributed lock based on zookeeper
Zookeeper is a distributed coordination service in which each node is called znode and has its own independent path. There are four types of znode:
Persistent node: the default node type. After the client that created the node is disconnected from the zookeeper, the node still exists.
Persistent node sequential node: when a node is created, Zookeeper numbers the node name according to the chronological order in which it is created:
Temporary nodes: in contrast to persistent nodes, temporary nodes are deleted when the client that created the node is disconnected from zookeeper:
Temporary sequential node: combines the characteristics of temporary node and sequential node: when creating a node, Zookeeper numbers the node name according to the chronological order in which it is created; when the client that created the node is disconnected from zookeeper, the temporary node will be deleted.
Let's take a look at how distributed locks are implemented based on the above four types of nodes.
1. Acquire lock
1) create a persistence section in Zookeeper, and when the first client Client1 wants to acquire the lock, you need to create a temporary sequential node under this node.
2) Client1 looks up all the temporary order nodes under the persistent nodes and sorts them to determine whether the nodes created by itself are the first in order. If it is the first node, the lock is successfully obtained.
3) if another client Client2 comes to acquire the lock, create a temporary sequential node Lock2 under the persistent node.
4) Client2 looks up all the temporary order nodes under the persistent nodes and sorts them to determine whether the node Lock2 created by itself is the highest in order. It is found that the node Lock2 is not the smallest.
Therefore, Client2 registers Watcher with the node Lock1, which is sorted only before it, to listen for the existence of the Lock1 node. This means that the Client2 failed to grab the lock and entered the waiting state.
5) if another client Client3 comes to acquire the lock, download and create a temporary sequential node Lock3 on the persistent node.
Client3 looks for all the temporary order nodes under the persistent nodes and sorts them to determine whether the node Lock3 created by itself is the highest in order. It is also found that the node Lock3 is not the smallest.
Therefore, Client3 registers Watcher with the node Lock2, which is sorted only before it, to listen for the existence of the Lock2 node. This means that Client3 also failed to grab the lock and entered the waiting state.
two。 Release lock
Releasing the lock is relatively simple, because of the temporary sequential node created earlier, the lock is automatically released when the following two situations occur:
1) when the task is completed, Client releases the lock.
2) Client crashes before the task is completed, and the lock is automatically released.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.