Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Implementation of redis distributed Lock Optimization

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "the specific implementation of redis distributed lock optimization", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "the specific implementation of redis distributed lock optimization"!

For stand-alone applications, we can directly use the synchronized keyword or the Lock tool class to add locks, but for distributed applications, we need to rely on some tools to achieve locking.

The popular explanation of the locking process is:

1. Occupy the pit

two。 Execution logic

3. Fill the pit

We can use redis to complete the occupation pit operation.

Basic version lock

/ / acquire the lock boolean lock = redis.setIfAbsent (key, value); if (lock) {/ / business logic / / fill the pit redis.delete (lock)} if the lock is not released due to application downtime after the lock is acquired, it will cause deadlock

When acquiring a lock, you need to add an expiration time to the lock.

Redis.setIfAbsent (key, value); redis.expire (key, value)

If the expiration time is not set successfully when acquiring the lock, it will also lead to deadlock

Merge set and expire into atomic operations through lua script to ensure that the expiration time can be set successfully.

If the lock expires but the current task is not finished, the lock may be acquired by other applications and the lock's key will be updated. If the lock is released at the end of the current task, someone else's lock will be released.

Release the lock:

Judge whether the value of the current lock is consistent with the value of your own lock.

If it doesn't match, it won't be released.

If the values match, delete the key release lock

When releasing the lock, if you judge that the key value matches, but at this time the key expires and the lock is acquired by someone else, and delete the key at this time, you will release someone else's lock.

To ensure that the query, judgment, and deletion logic is atomic, use the lua script

How to ensure the reentrant of the lock

If there is locking logic or other calls to recursive methods, there are

Lock.lock (); / / logical lock.lock (); / / logical lock.unLock (); lock.unLock ()

To achieve a reentrant lock, you can write on the value of the lock, record the number of reentrants on the value, add one for each reentrant, and subtract one for each unlock until zero, then delete the key release lock

Key: {"value": 1}

What if the business logic time is too long and the lock is released in advance?

Set the expiration time a little longer

The renewal function needs to be added to the lock.

The disadvantage of setting a long expiration time is that it takes a long time for the lock to be acquired by others after the application goes down, which affects the business.

If there is a renewal function, if there is a downtime, the lock will be brushed out with a shorter expiration time, which is a more elegant solution.

For example, after acquiring a lock in redission, a watchdog thread will be started to monitor whether the current thread still holds the lock, and if it still holds the lock, renew it.

The specific operation is to check whether the lock is held every ten seconds, and if the lock is not released, reset the expiration time of the lock to achieve renewal.

If the application successfully acquires the lock on the master node of redis, the master node goes down and the lock data is not synchronized to the slave node. After the master-slave switch, other applications also take the opportunity to acquire distributed locks.

RedLock

Isn't there something wrong with the master-slave structure? redlock will use multiple unrelated redisMaster nodes without master-slave relationship to ensure that they will not go down at the same time, preferably an odd number.

By acquiring locks on multiple nodes at the same time, redLock is considered successful only if more than half of the nodes have successfully acquired locks; otherwise, rollback removes locks on all nodes.

At this point, I believe that everyone on the "redis distributed lock optimization of the specific implementation" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report