Concise and practical redis distributed lock usage


Concise and practical redis distributed lock usage

This article is transferred from: Le byte

This article mainly explains the usage of redis distributed lock

For more Java related information, you can follow the official account Le byte. Send: 999

In many cases, the distributed lock function needs to be used in microservices. At present, the more common scheme is to realize the distributed lock through redis. There are many ways to realize the distributed lock on the Internet. In the early stage, it was mainly based on redisson and other clients, but in spring boot2 When redis is used in versions above x, its client library has used lettuce by default.
So this article will directly introduce spring boot2 X the method of quickly using redis distributed lock function in the above projects. I hope to 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 use the basic functions provided by redis and realize the distributed lock function through certain algorithm design. At present, many blog posts and code bases have described how to use redis to implement distributed locks, but many implementations are relatively simple and have low security.
An algorithm called redlock is recommended in the official documents of redis to realize the redis based distributed lock function. At present, there are redis client implementation libraries in multiple languages based on this algorithm. Among them, the redisson library is the most famous in the Java field. However, redisson not only implements the distributed lock function, but also implements a set of redis distributed data structure, so it will be more important. In addition, the latest spring boot based When redis is used in versions above 2. X, its client library has used the encapsulation of lettuce (a Java redis client library that is safer and lighter than redison and jedis threads) by default. Therefore, in order to better meet the use in micro service scenarios, it is often used to implement distributed locks based on redlock algorithm in practice.

This case will also demonstrate how to use the redlock algorithm to realize redis distributed lock function. However, before that, let’s take a look at how the redlock algorithm works. The schematic diagram is as follows:

Concise and practical redis distributed lock usage

The above is the logic of the redlock algorithm officially recommended for redis distributed lock. It is a multi node redis distributed lock algorithm, which can effectively prevent single node failure. The execution steps are as follows:

Concise and practical redis distributed lock usage

The redis client implementing the above algorithm can basically ensure the effectiveness and security of distributed locks. Several basic characteristic requirements:

Concise and practical redis distributed lock usage

Spring boot integration usage

Through the above description, I believe you should have a certain understanding and understanding of the basic algorithm for implementing redis distributed lock. In practice, the implementation can be customized according to the algorithm, but in fact, spring has long provided the implementation of redis distributed lock based on the algorithm. The specific use steps are as follows:

1) In project POM The spring integration dependency is introduced into the XML file. The code is as follows:

Concise and practical redis distributed lock usage

At present, the distributed lock related code provided by spring is migrated in the spring integration subproject, so its related dependencies are introduced here.

2) Write the redislock configuration class. The code is as follows:

Concise and practical redis distributed lock usage

The premise of loading the above configuration code is that the application has integrated redis service access link information. The specific spring boot project integrates redis access in a relatively simple way. You can refer to other resources.

3) The specific usage of distributed locks is as follows:

Concise and practical redis distributed lock usage

The above code is the example code of redis distributed lock used in order duplication prevention. The related operations of distributed lock are realized by dependency injection into redislockregistry instance, such as obtaining() method to create lock, trylock() to hold lock and unlock() to release lock.

Thank you for your recognition and support. Xiaobian will continue to forward the high-quality articles of Le byte

Recommended Today

Modify user information changeinfo

When judging the persistence layer: Problem: there is such a problem when modifying user information. For example: the user’s email is not required. It was not empty originally. At this time, the user deletes the mailbox information and submits it. At this time, if it is not empty to judge whether it needs to be […]