Redis Combat 14-Basic Principles of Distributed Locks and Comparison of Different Implementation Methods

In the previous article, we know that in a cluster environment, the JVM-level lock implemented by the synchronized keyword will be invalid. So how to solve this problem? We can use distributed locks to solve it. In this article, we will introduce the basic principles of distributed locks and the comparison of different implementation methods.

Let's first review the timing diagram of JVM-level locks. As shown below:

Under the cluster, there are multiple lock monitors for different jvms. So, what should it be like to use a distributed lock? Can we propose the lock monitor and put it into a three-party component, so that all JVM processes in the cluster can share access to this common lock monitor? As shown below:

 

The figure above is a schematic diagram of a distributed lock. So what is a distributed lock?

What is a distributed lock?

Distributed locks need to meet the needs of distributed systems or locks that are visible and mutually exclusive to multiple processes in cluster mode.

What conditions should distributed locks meet?

visibility

Multiple threads can see the same result,

Note : The visibility mentioned here is not the visibility of memory that we refer to in concurrent programming. It just means that changes can be perceived between multiple processes.

mutually exclusive

Mutual exclusion is the most basic condition of distributed locks, making programs execute serially

high availability

The program is not easy to crash, and high availability is guaranteed all the time

high performance

Since locking itself reduces performance, the distributed lock itself needs to have higher locking performance and lock release performance

safety

Security is also an integral element of our procedures

There are three common distributed locks

Mysql implements distributed locks

mysql itself has a lock mechanism, but due to the general performance of mysql, it is rare to use MySQL as a distributed lock at the beginning when using distributed locks;

Redis implements distributed locks

Redis as a distributed lock is a very common way to use it. Now enterprise-level development basically uses Redis or zookeeper as a distributed lock. Redis implements distributed locks by using the setnx method. If the key is inserted successfully, it means that the lock has been obtained. If someone inserts successfully and others fail, it indicates that the lock cannot be obtained. This logic is used to implement distributed locks. of.

Zookeeper implements distributed locks

Zookeeper is also a better solution for implementing distributed locks in enterprise-level development. It is mainly realized by using temporary ordered nodes and watch mechanism. This series of tutorials mainly explains Redis, so I won't elaborate too much here.

Let's take a look at the comparison of the three implementations

We compared the three implementation methods from different dimensions. As shown below:

 

 Preview of the next section: Implementation ideas for distributed locks based on Redis

 

Guess you like

Origin blog.csdn.net/kaizi_1992/article/details/131029739