In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to use Redis to realize distributed lock in distributed environment. Aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
In some business scenarios with high concurrency, such as second kill, course selection and other systems, in order to avoid the problems of oversold goods and course selections exceeding the required number of courses, locking operations need to be carried out when reading and writing the database to ensure that there is already a user operating at a certain time. In Java stand-alone applications, there is nothing wrong with using synchronized keywords directly, but not in distributed systems, so it is necessary to introduce distributed locks to solve the problem. Distributed locks can be implemented in Zookeeper or Redis, focusing on the use of Redis to implement distributed locks.
There is a SETNX command in Redis, which returns 0 if key exists in Redis, otherwise 1, and executes successfully. The following is a wrong way to implement distributed locks. Finally, I will write a correct code to implement distributed locks. Please refer to
Wrong idea of distributed locking
If customer 1 executes SETNX ("key", "value") when locking returns 1 successfully, and the rest of customers execute SETNX returns 0 without acquiring the lock, locking failure will listen on key and cycle through SETNX ("key", "value") until 1 is returned. When customer 1 needs to release the lock, DEL ("key") needs to be performed to release the lock. But there is a problem. If the process of customer 1 dies, the key will never be deleted, and the rest of the customers will always return 0 when they execute SETNX ("key", "value"), resulting in a deadlock. This distributed locking idea is wrong.
The right way of thinking
If the client process acquires the lock and sets an action to remove the timeout, such as the expire command, the timeout lock is automatically released after the client process dies, and other customers can still acquire the lock. Next, I use multithreading to simulate the acquisition and release of distributed locks with high concurrency.
Client acquires lock:
Save k v to Redis through the SETNX command. If 1 is returned, the Redis does not have this data, and the operation successfully obtains the lock. If 0 is returned, it indicates that there is this data in the Redis. Other clients occupy the lock, fail to acquire the lock, and enter the waiting state.
RedisThread.java
Impersonate a client
If the getLock () method returns true, it means that the lock is acquired, the subsequent business logic is executed, and finally the lock is released through the DEL (k) command.
If the getLock () method returns false, the loop continues to listen (of course, you can also control the listening frequency through Thread.sleep ()) until the lock is acquired and the business logic is finally released.
RedisThread.java
Open multiple threads to simulate high concurrency
RedisThread.java
@ Override
Public void run () {
Try {
/ / call the client
Client ()
} catch (Exception e) {
E.printStackTrace ()
}
}
RedisLock.java test
Open 50 threads to simulate 50 customers (cluster of 50 services)
The output is as follows:
Lock-- > Business start-> Business end-> unlock--lock-- >.
This is the answer to the question about how to use Redis to realize distributed lock in distributed environment. 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 to learn more about it.
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.