In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
What is the reason why redis can do distributed locks? I believe there are many people do not understand, today, in order to let you know more about the reasons why redis can do distributed locks, so give you a summary of the following content, let's look down together.
Redis is a single-process single-thread mode, using queue mode to turn concurrent access into serial access, and there is no competition for multi-client connections to Redis.
The code is mainly aimed at locking the serial number of a certain amount of data to prevent multiple threads from writing this data. (mutually exclusive)
Now the most popular redis distributed lock is Redisson. Let's take a look at its underlying principle to understand how redis uses distributed locks.
Principle analysis
Distributed lock is to solve the lock function of parallel same code in distributed environment. Anyone who has known redis distributed lock must know that at first redis uses setnx as a distributed lock, and then set a timing expiration time on this basis, but what's wrong with this approach?
In fact, those who understand the above picture will understand what the problem is, first of all, the problem of atomicity, setnx+ expiration time these two operations must be atomic, so this can be solved with lua script
And then how to set the time to release the lock?
No matter how much expiration time we set, there is no guarantee that the locked code will be executed during this time, so it is not a good time.
If there is no fixed time, and the lock is released after the execution is completed, the problem is that if the machine goes down in the middle of the execution, the lock will never be released.
So how does Redisson solve the above problems?
It simplifies the code, and our use is so simple that we don't even have to actively set the expiration time.
It designs a watch dog watchdog to check whether it still holds the lock every 10 seconds, and if it does, give him an update expiration time of 30 seconds. Through this design, he can keep the lock until the lock is released, and even if the lock is down, the lock can be released automatically.
On the other hand, the client that cannot get the lock is constantly trying to add the lock in a loop.
Through the client-side id of the record lock, it can be designed as a reentrant lock
About the reason why redis can do distributed lock is shared here, I hope the above content can have a certain reference value for everyone, you can learn to apply. If you like this article, you might as well share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.