Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the concurrency problem of inventory and realize quantity Control by redis

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to solve the inventory concurrency problem of redis to achieve quantity control". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Redis is single-process, blocking, and can only handle one request at a time, and subsequent requests need to be queued.

Advantages: because it is a single process, there is no need to deal with concurrency problems and reduce the complexity of the system.

Disadvantages: not suitable for caching large objects (more than 100kb)

Reason: because Redis uses only a single core, while Memcached can use multiple cores, on average, Redis performs better than Memcached in storing small data on each core.

In the data of more than 100k, the performance of Memcached is higher than that of Redis. Although Redis has also optimized the performance of storing big data, it is still slightly inferior to Memcached.

Memcache is multi-process and non-blocking. If it is only used as a cache, memcache is more appropriate.

I. order

Exists to see if the key key already exists in redis, such as exists mycounter

The set setting initializes a key value such as set mycounter 99

Get gets a key value such as getmycounter

Incr increases by 1, for example, incrmycounter / / outputs a result of 100

Incrby specifies a growth value such as incrbymycounter 2 / / the output result is 102

Specify a reduction value such as incrbymycounter-2 / / output to 100

Setnx. If the value does not exist, the value is set successfully. For example, setnxmycounter 99 / / the output result is 0, which means the setting failed, and it already exists in redis.

The output result of setnx key1 12 / / is 1, which means the setting is successful. It did not exist in redis before.

Expire sets the life cycle of a key, such as expire mycounter 30 / / to 30 seconds.

Ttl acquires key expiration time, for example, ttlmycounter / / output is 13, which means there are 13 seconds left. If-1 is returned, it will never expire and will always exist in the redis cache, unless there is insufficient memory.

/ / if-2 is returned, it is invalid. Redis does not have this key value. You can verify it with exists. If 0 is returned, it means that it is not saved.

Second, common scenes

Goods are snapped up, the quantity is out of control, the inventory is out of limit, and the cost is insufficient (for example, inventory is 1000, but 2000 is successfully snapped up by users, inventory is insufficient)

The lottery is limited, it is out of control, and the money is overspent.

Grab the red packet

Third, flow chart and code

Option 1 flowchart:

Option 2 flowchart:

/ / scenario 1 code, test environment TP5public function redisCountV1 () {Log::record ("Test version 1 concurrent start", Log::INFO); $redis = new Redis (); / / Total inventory quantity $amountLimit = 100; / / redis storage inventory key name $keyName = "mycounter_v6"; / / assume that the number of stocks consumed each time is 1$ incrAmount = 1 / / determine whether the value exists in redis, and if not, set it with set (the problem is that if concurrency occurs, two or more users visit at the same time, it will cause inventory to be reset) if (! $redis- > exists ($keyName)) {$redis- > set ($keyName, 95);} / / withdraw the current inventory from redis $currAmount = $redis- > get ($keyName) / / if current inventory + increased inventory > total inventory, directly return if ($currAmount + $incrAmount > $amountLimit) {file_put_contents ("/ Users/han/Documents/www/cs/testv1.log", "bad luck\ n", FILE_APPEND); Log::record ("bad luck", Log::INFO); return false;} / / increase cache inventory by $redis- > incrby ($keyName, $incrAmount) File_put_contents ("/ Users/han/Documents/www/cs/testv1.log", "good luck\ n", FILE_APPEND); Log::record ("good luck", Log::INFO);} / / Test method: ab-c 100-n 200 http://www.fenleduo.com:8080/V7/Test/redisCountV1// scenario 2 code, test environment TP5public function redisCountV2 () {Log::record ("Test version 2 concurrent start", Log::INFO) $redis = new Redis (); / / Total inventory quantity $amountLimit = 100; / / redis storage inventory key name $keyName = "mycounter_v12"; / / assuming that the number of stocks consumed is 1$ incrAmount = 1 / / determine whether the value exists in redis. If not, set it with setnx (Note: if concurrency occurs, two or more users visit at the same time, it will not cause inventory reset) if (! $redis- > exists ($keyName)) {/ / setnx if the value does not exist, it will not be set $redis- > setnx ($keyName, 95) } / / withdraw current inventory from redis $currAmount = $redis- > get ($keyName); / / if current inventory + increased inventory > total inventory, directly return if ($redis- > incrby ($keyName, $incrAmount) > $amountLimit) {file_put_contents ("/ Users/han/Documents/www/cs/testv2.log", "bad luck\ n", FILE_APPEND); Log::record ("bad luck", Log::INFO) Return false;} file_put_contents ("/ Users/han/Documents/www/cs/testv2.log", "good luck\ n", FILE_APPEND); Log::record ("good luck", Log::INFO) } / / Test method: ab-c 100-n 200 http://www.fenleduo.com:8080/V7/Test/redisCountV2"redis how to solve the inventory concurrency problem to achieve quantity control "is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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: 297

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report