This article is from: Le Byte

The article mainly explains: Redis distributed lock usage

For more information related to JAVA can pay attention to the public number “Le Byte” send: 999

Distributed locking is needed in many cases in micro-services, and the current common solution is to realize distributed locking through Redis. There are many ways to realize distributed locking on the Internet. In the early stage, it was mainly based on clients such as Redisson, but when Redis is used in Spring Boot2. The client library already uses lettuce by default. So this article is going to show you how to quickly use Redis’s distributed locking functionality in Spring Boot2.x projects and hopefully update your knowledge base!

Overview of Redis Distributed Lock Principle

In fact, Redis service itself does not provide such a mechanism as distributed lock, but as a global key-value storage system, the client can make use of the basic functions provided by Redis and realize the distributed lock function through certain algorithm design. There are a number of blog posts and code bases that describe how Redis can be used to implement distributed locking, but many implementations are relatively simple and less secure. In the official document of Redis, an algorithm called Redlock is recommended to realize the Redis-based distributed lock function. At present, there are several language versions of Redis client implementation libraries based on this algorithm. One of the best-known in the Java space is the Redisson library. However, since Reddisson not only implements the distributed locking function, but also implements an additional set of Redis distributed data structure, so it can be quite heavy, plus the latest Redis based on Spring Boot.2.x and above versions of Redis, The lettuce client library has already used the encapsulation of Java Redis client library by default, so in order to be more in line with the use of micro-service scenarios, in practice, Redlock algorithm is often chosen to implement the distributed lock by itself.

This example will also show how the Redlock algorithm implements Redis distributed locking, but before we do so, let’s take a look at how the Redlock algorithm works, as shown below:

The above is the Redlock algorithm logic to achieve the official recommendation of Redis distributed lock, it is a multi-node Redis distributed lock algorithm, can effectively prevent single node failure problem. The execution steps are as follows:

Redis client implementing the above algorithm can basically guarantee the effectiveness and security of the distributed lock in several basic characteristics:

Spring Boot integration usage

Through the description of the previous content, I believe you should have a certain understanding of the basic algorithm to achieve Redis distributed lock. In practice, you can customize the implementation according to this algorithm, but in fact Spring has long provided the implementation of Redis distributed lock based on this algorithm. The specific use steps are as follows:

1) Introduce the Spring Integration dependency in the project pom.xml file as follows:

The distributed lock related code that Spring provides is currently migrated into the Spring Integration subproject, so its dependencies are introduced here.

2) Write the configuration class of Redislock as follows:

The above configuration code is loaded on the premise that the application has integrated the Redis service access link information. The details of the Spring Boot project integration Redis access way is relatively simple, please refer to other materials.

3) The specific use mode of distributed lock is shown in the following code snippet:

The above code is an example of the Redis distributed lock used in order reduplication prevention. The operations related to the distributed lock are implemented through dependency injection of a RedisLockRegistry instance, such as obtain() method to create the lock, tryLock() to hold the lock, and unlock() to release the lock.

Thank you for your recognition and support, Xiaobian will continue to forward “Le Byte” quality articles