Guest tutor: Teaching director of well-known institutions under BObo ** Front, expert of CSDN blog, used to work in Yonyou Software Power Division. ** Projects: PMIS project of China Huadian, ERP system of China Huadian Group, PMIS project of China Guodian, etc

Distributed locking often comes up in the interview process, usually the interviewer will start from the service framework (Spring Cloud, Dubbo), along the way to the knowledge of distributed transactions, distributed locking, ZooKeeper, etc. So this article will take you to understand the knowledge of distributed locking from the perspective of interview.

01What are the solutions for distributed locking

Distributed locks, as the name implies, are locks used in distributed environments, focusing on locks. Its role is to protect data security.

With the continuous development of Internet technology, increasing the amount of data, increasingly complex business logic, in this context, the traditional centralized system has been unable to meet our business needs, distributed system has been applied in more scenarios, and access to a Shared resource in a distributed system requires a mutex mechanism, to prevent the mutual interference between each other, to ensure the consistency, In this case, we need to use distributed locks. Distributed lock solutions include MySQL, Redis, Zookeeper, and so on.

MYSQL

MYSQL can be used as follows:

01 Use the unique index of mysql

Each record in the database is a lock, taking advantage of the exclusivity of MySQL’s unique index. If multiple requests are submitted to the database at the same time, the database guarantees that only one operation will succeed, so we can assume that the thread that succeeded in the operation has acquired the lock of the method. When the method completes, we can delete the database record to release the lock.

02 Pessimistic lock/row lock

Pessimistic locks use the lock method, using for UPDATE with an explicit row lock, so that you can use the row-level exclusive lock to implement distributed locks.

03 Optimistic Lock version

Optimistic locking has higher performance than pessimistic locking. Optimistic locking is not a lock, but a design idea. It is implemented using the Version logging mechanism.

Redis

Redis is selected to achieve distributed lock for several reasons

(1) Redis has high performance; (2) The Redis command supports this well and is convenient to implement.

Redis usage scenarios are particularly high in concurrency.

**Zookeeper**

Zookeeper is a distributed coordination framework. Common components fail to coordinate. The scenarios used by Zookeeper include registry, configuration center, dynamic up-down awareness, and distributed lock.
** Cluster mechanism of Zookeeper ** is the key assessment content. It is ** half alive mechanism **, more than half of the nodes in the whole cluster survive.
Role of a cluster node

It is divided into Leader and follower.
The Leader is at the heart of a cluster’s work. The main job is to read and write all the data.
Followers are the followers of a cluster. It can store data, and each node can store data. The main job is to read all the data.

Ephemeral nodes are one of the types of Zookeeper nodes that have the -e attribute. The effect of that is

Everyone can see who created the node, and the node will disappear when the creator of the node goes offline. Features: ** Ordered, short, event listening mechanism **.

02How to implement distributed lock in Zookeeper

Zookeeper Distributed lock mechanism

  1. Convention: obtain the lock with the smallest serial number;

  2. The serial number is not the smallest, so it listens for the previous node with a smaller serial number.

  3. If a node is awakened, check whether the node is awakened with the smallest serial number. If it is the smallest acquisition lock; If it is not the smallest, then listen on nodes smaller than it

03How is Redis distributed

Redis distributed lock implementation idea

(1) When obtaining the lock, use setnx to add the lock, and use the expire command to add a timeout period for the lock. When the timeout period expires, the lock will be automatically released. The value of the lock is a randomly generated UUID. (2) set a timeout time for lock acquisition. If this time is exceeded, lock acquisition will be abandoned; (3) When releasing the lock, determine whether it is the lock according to the UUID. If it is the lock, execute delete to release the lock.

Distributed lock is often used in daily Coding, but the knowledge of distributed lock is still very profound. Hopefully this article will give you a clear idea of the distributed lock solution and further improve your ability to view the source code.