In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you what kinds of redis locks there are. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Redis lock classification
The subtables of lock commands that can be used by redis are INCR, SETNX, SET
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 server to obtain key value 1 means lock 2, client B also requests server to obtain key value 2, client An executes code, deletes lock 4, client B acquires key value 1 after waiting for a certain period of time, client B executes code successfully 5 Delete lock $redis- > incr ($key) $redis- > expire ($key, $ttl); / / set the generation time to 1 second
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, client B also requests the server to set the value of key. If it fails, it means that locking failed. 3. Client A completes the execution of the code. 4. Client B requests to set the value of key after waiting for a period of time. Set successfully 5. Client B executes the code and deletes the lock $redis- > setNX ($key, $value) $redis- > expire ($key, $ttl)
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, client B also requests the server to set the value of key. If it fails, it means that locking failed. 3. Client A completes the execution of the code. 4. Client B requests to set the value of key after waiting for a period of time. 5. Client B executes the code successfully and deletes the lock $redis- > set ($key, $value, array ('nx',' ex' = > $ttl)) / / ex indicates that the above is the number of redis locks shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.