In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Preface
This article mainly introduces several methods of locking on redis, which can be shared for your reference and study. I won't say much below, let's take a look at the detailed introduction.
1. Redis lock classification
The subtables of lock commands that can be used by redis are INCR, SETNX, SET
two。 The first lock command INCR
The idea of this locking is that if key does not exist, the value of key will be initialized to 0 before performing an INCR operation to add one.
Then other users are performing INCR operations to add a period of time, and if the number returned is greater than 1, the lock is in use.
1. Client A requests the server to acquire the key. A value of 1 indicates that the lock has been acquired.
2. Client B also requests the server to obtain the key. A value of 2 indicates that the acquisition of the lock failed.
3. Client An executes the code and deletes the lock
4. Client B waits for a period of time to obtain a value of 1 for key when it requests, indicating that the lock has been acquired successfully.
5. Client B executes the code and deletes the lock
$redis- > incr ($key); $redis- > expire ($key, $ttl); / / set the generation time to 1 second
3. The second lock SETNX
The idea of locking is to set key to value if key does not exist
If key already exists, SETNX does not take any action
1. Client A requests the server to set the value of key. If the setting is successful, it means that the lock is successful.
2. Client B also requests the server to set the value of key. If it fails, it means that locking failed.
3. Client An executes the code and deletes the lock
4. Client B requests to set the value of key after waiting for a period of time. The setting is successful.
5. Client B executes the code and deletes the lock
$redis- > setNX ($key, $value); $redis- > expire ($key, $ttl)
4. The third lock SET
There is a problem with both of the above methods, and you will find that you need to set key expiration. So why set key expiration? If the request execution exits unexpectedly for some reason, resulting in a lock created but not deleted, the lock will always exist so that the cache will never be updated in the future. So we need to add an expiration time to the lock in case something happens.
But setting it with Expire is not an atomic operation. So atomicity can also be ensured through transactions, but there are still some problems, so officials have cited another one, and the use of the SET command itself has included the ability to set the expiration time since version 2.6.12.
1. Client A requests the server to set the value of key. If the setting is successful, it means that the lock is successful.
2. Client B also requests the server to set the value of key. If it fails, it means that locking failed.
3. Client An executes the code and deletes the lock
4. Client B requests to set the value of key after waiting for a period of time. The setting is successful.
5. Client B executes the code and deletes the lock
$redis- > set ($key, $value, array ('nx',' ex' = > $ttl)); / / ex represents seconds
5. Other questions
Although the above step has met our needs, we still have to consider other issues.
1. What if redis finds that the lock has failed? Interrupt request or loop request?
2. If there is a circular request, if one acquires the lock, is it easy to grab the lock when the others go to acquire the lock?
3. After the lock expires ahead of time, client A has not finished executing it, and then client B acquires the lock. When client A finishes executing the lock, will client A delete the lock of B when deleting the lock?
6. Solution.
For problem 1: use a loop request, a loop request to acquire the lock
For problem 2: for the second problem, when the loop requests to acquire the lock, add the sleep function and wait for a few milliseconds to execute the loop.
For problem 3: the key stored when the lock is added is random. In this way, every time you delete a key, you can judge whether the value saved in the key is the same as your own.
Do {/ / for problem 1, use the loop $timeout = 10; $roomid = 10001; $key = 'room_lock'; $value =' room_'.$roomid; / / assign a random value for problem 3$ isLock = Redis::set ($key, $value, 'ex', $timeout,' nx') / / ex seconds if ($isLock) {if (Redis::get ($key) = = $value) {/ / prevent premature expiration, mistakenly delete locks created by other requests / / execute internal code Redis::del ($key); continue;// execution successfully deleted key and jumped out of the loop}} else {usleep (5000) / / Sleep, reduce lock frequency, relieve redis pressure, for problem 2}} while (! $isLock)
7. The other lock.
The above locks fully meet the requirements, but the authorities also provide a set of locking algorithms. Here, take PHP as an example.
$servers = ['127.0.0.1, 6379], [' 127.0.0.1, 6389], ['127.0.0.1, 6399],]; $redLock = new RedLock ($servers); / / add lock $lock = $redLock- > lock (' my_resource_name', 1000); / / remove lock $redLock- > unlock ($lock)
The above is an official locking method, which is the same as the general method of No. 6, but the official writing is more robust. So you can directly use the officially written class method to make the call. Officials provide how to implement locks in various languages.
Summary
The above is the whole content of this article, I hope that the content of this article can bring some help to your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.
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.