In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
The editor will share with you the implementation principle of redis distributed lock. I hope you will gain a lot after reading this article. Let's discuss it together.
In fact, distributed locking can be understood as controlling the distributed system to operate on shared resources in an orderly manner and maintain consistency through mutual exclusion.
To give an inappropriate example:
Suppose the shared resource is a house with all kinds of books, and the distributed system is the person who wants to enter the house to read. The distributed lock is to ensure that the house has only one door and only one person can enter at a time, and the door has only one key. Then many people want to read, yes, stand in line, the first person takes the key to open the door to read and lock the door, and then the second person does not have the key, then wait for the first one to come out, and then you go in with the key, and so on.
Realization principle
Mutual repulsion
Ensure that only one client can get the lock at the same time, that is, it can operate on the shared resource.
Security.
Only locked services can have unlock permissions, that is, bcd can unlock all locks that cannot be added by a, and distributed locks are meaningless if they can all be unlocked.
What may happen is that a goes to query and finds that the lock held by an is ready to be unlocked, and suddenly the lock held by an expires, and then b goes to get the lock, because the lock an expires and b gets the lock. At this time, a continues to perform the second step of unlocking. If it is not checked, the lock held by b will be deleted.
Avoid deadlock
The occurrence of a deadlock will result in no subsequent services getting the lock and no more operations on the shared resources.
Ensure that locking and unlocking operations are atomic
This actually belongs to the problem of implementing distributed locks. Suppose a uses redis to implement distributed locks.
Assuming the locking operation, the operation steps are divided into two steps:
1. Set key set (key,value) 2 and set the expiration time for key.
Suppose that now a just after the implementation of set, the program crashes, resulting in a deadlock due to the existence of key without setting an expiration time for key.
How to realize distributed Lock
There are many ways to implement distributed locks. Distributed locks can be implemented as long as they meet the above conditions, such as database and redis,zookeeper. Here we will first talk about how to use redis to implement distributed locks.
The key to the implementation of distributed lock is to build a storage server outside the distributed application server to store lock information. At this time, we can easily think of Redis. First of all, we need to build a Redis server and use the Redis server to store lock information.
Using redis to realize distributed Lock
Locking using the redis command set key value NX EX max-lock-time
Use the redis command EVAL to unlock
There are several key points to pay attention to when implementing:
1. Lock information must be expired and timed out, and a thread cannot be allowed to hold a lock for a long time, resulting in a deadlock.
2. Only one thread can acquire the lock at a time.
Several redis commands to be used:
Setnx (key, value): "set if not exits". If the key-value does not exist, the cache is successfully added and 1 is returned, otherwise 0 is returned.
Get (key): gets the value corresponding to key, and returns nil if it does not exist.
Getset (key, value): first get the value corresponding to key, return nil if it does not exist, and then update the old value to the new value.
Expire (key, seconds): sets the validity period of key-value to seconds seconds.
After reading this article, I believe you have a certain understanding of the implementation principle of redis distributed lock, want to know more related knowledge, welcome to follow the industry information channel, thank you for reading!
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.