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

How to improve the availability of distributed Lock in Redis

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In view of how to improve the availability of distributed locks in Redis, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Brief introduction of Redis distributed Lock

Redis provides us with distributed locks. The instruction to open the lock is setnx (set if not exists). Release the lock instruction del. Let's try it.

> setnx lookname mango (integer) 1 > get lookname "mango" > del lookname (integer) 1 > get mango (nil) deadlock

Here we think about a problem, if we have an exception while executing the logic, then the lock will not be released, resulting in a deadlock, so we find a way to let it release itself, add an expiration time this instruction is expire key seconds, even if we do not del, it will be automatically released after this time.

> setnx lookname mango (integer) 1 > expire lookname 5 (integer) 1 > get lookname (nil)

One more question: what if I have an exception during the execution of setnx and expire? We quickly thought of using transactions, but we note here that expire relies on setnx,expire, which is executed only when setnx grabs the resources, otherwise the execution is unsuccessful and there is no if else statement in the transaction.

In order to solve this problem, the redis open source community has developed a lot of lib libraries for distributed locks, that is, every time we use distributed locks, we have to introduce this third-party library. The author adds a set extension to the Redis2.8 version to execute the two commands together.

> set lookname mango ex 5 nxOK > get lookname "mango" > get lookname # 5s (nil) timeout problem

If we haven't finished executing the business logic between the time when the lock is added and the time the lock is released, then a new problem arises at this time: timeout, for example, the logic I only want takes 30 seconds, while my lock expires only 10 seconds. At this point, before my logic is finished, the problem comes.

Do not use expiration time

The shortcut to solve the timeout problem is not to use expiration time. We need to add a more private random number after key, such as name1564835 and name6879425, to reduce the collision of key. We mark this key, and the program reclaims this memory after performing logic processing.

Delayed recovery

If 10 seconds is not enough, then we can extend the recovery time of the key. We can determine whether the client is still using the key before the key is recycled. If we do not use the key, we will do nothing. If we are using it, we will increase the recovery time. How do we do it? We can restart a thread on the calling side to monitor the key that is about to expire, and the client can send a Lua script to the redis service instance to see if the value of the key has changed. If there is no change, let the redis server extend the lock time.

RedLock algorithm

When we are in a cluster, when the master node dies, the slave node will take its place. the first client successfully applied for a lock in the master node, but before the lock was synchronized to the slave node, the master node suddenly died, and then the slave node became the master node. This new node does not have the lock, so when another client comes to request a lock, both clients hold the resource. The problem arises again.

But this problem is a very small probability event and is caused by a very short time, from a distributed system point of view, we can tolerate this problem. But we hope that redis will not be affected at all, and we can consider redlock. Redlock algorithm is invented by Antirez, its process is more complex, but there are a lot of open source library to do a good package, users can use it, such as redlock-py.

Redlock.Redlock ()

Principle of RedLock algorithm:

1. Gets the current time in milliseconds.

two。 Take turns requesting locks on N nodes with the same key and random values. In this step, when the client requests a lock on each master, there will be a much smaller timeout than the total lock release time. For example, if the lock automatic release time is 10 seconds, the timeout period for each node lock request may be in the range of 5-50 milliseconds, which can prevent a client from blocking on a down master node for too long. If a master node is unavailable, we should try the next master node as soon as possible.

3. The client calculates the time it takes to acquire the lock in the second step, and the lock is considered successful only if the client successfully acquires the lock on most of the master nodes ((NUnip 2) + 1), and the total time consumed does not exceed the lock release time.

4. If the lock acquisition is successful, the lock automatic release time is now the initial lock release time minus the time it took to acquire the lock.

5. If lock acquisition fails, whether it is because no more than half of the locks were successfully acquired, or because the total elapsed time exceeds the lock release time, the client releases locks on each master node, even those locks that he believes did not succeed.

Note: when locking, it sends set (key, value, nx=True, ex=xxx) instructions to more than half of the nodes. As long as the set of more than half of the nodes is successful, the locking is considered successful. When you release the lock, you need to send a del instruction to all nodes. However, the Redlock algorithm also needs to consider many details, such as error retry, clock drift and so on. at the same time, because Redlock needs to read and write to multiple nodes, it means that the performance of Redis is lower than that of single instance, and the code introduces additional third-party lib.

This is the answer to the question about how to improve the usability of distributed locks in Redis. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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

Internet Technology

Wechat

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

12
Report