In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to implement locking using SETNX from Redis. Xiao Bian thinks it is quite practical, so share it for everyone to make a reference. Let's follow the editor and have a look.
setNX is an abbreviation for set if not exists, that is, it is set only when it does not exist, and returns 1 when it is set successfully, and 0 when it fails. It can be used to achieve the effect of locking, but many people in the process of using there are some problems not considered.
For example, the interface of a query database is cached because of the large number of requests, and the cache is refreshed after expiration. When the concurrency is large and the cache expires, a large number of concurrent requests will query the database directly, resulting in avalanche. The avalanche problem can be avoided if locking mechanisms are used to control only one request to update the cache. Here's how many people subconsciously think of locking
$rs = $redis->setNX($key, $value);if ($rs) { //Process update cache logic // ...... //delete lock $redis->del($key);}
Acquire lock via setNX, update cache if successful and delete lock. There's a serious problem: if the cache update accidentally quits for some reason, the lock will never be removed and will never be updated again. To solve this problem, one might think of setting an expiration time for locks, as follows
$redis->multi();$redis->setNX($key, $value);$redis->expire($key, $ttl);$redis->exec();
Because setNX does not have the function of setting expiration time, it needs to be set by Expire, and Multi/Exec needs to be used to ensure the atomicity of the request, so as to avoid setNX succeeding Expire but failing. There is also a problem: when multiple requests arrive, although only one request setNX can succeed, but any request Expire can succeed, which means that even if the lock can not be obtained, the expiration time can be refreshed, resulting in the lock is always valid, or can not solve the above problem. Obviously setNX does not meet the requirements, Redis since 2.6.12, SET covers the SETEX function, SET itself contains the function of setting the expiration time, so using SET can solve the problem encountered above
$rs = $redis->set($key, $value, array('nx', 'ex' => $ttl));if ($rs) { //Process update cache logic // ...... //delete lock $redis->del($key);}
In fact, there is still a problem at this step. If a request takes longer to update the cache than the validity period of the lock, the lock will be invalid during the cache update process. At this time, another request will obtain the lock, but the previous request will delete the lock directly when the cache update is completed. So to avoid this problem, you need to introduce a random value when you create the lock and judge when you delete the lock
$rs = $redis->set($key, $random, array('nx', 'ex' => $ttl));if ($rs) { //Process update cache logic // ...... //first determine the random number, if it is the same, delete the lock if ($redis->get($key) == $random) { $redis->del($key); Thank you for reading! About how to use Redis SETNX lock mechanism to share here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge. If you think the article is good, you can share it so that more people can see 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.