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 realize the second kill function with Redis optimistic lock

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

In the development of high-traffic programs, there are bound to be scenarios of high concurrency applications. The solution is roughly divided into two directions, message queuing and locking.

Redis implements a simple version of the message queue core

$key = 'quque'; / * A limited number of products are pre-stored in the message queue * / public function qnquque () {for ($I = 1; $ilpush ($key,$i)) }} / * Business logic processing is omitted here, default business logic processing is finished, and out of queue * / public function dequque () {$redis- > rpop ($key) / * * here starts the business logic processing after the purchase of goods * /}

Message queuing is a good solution to prevent overbuying and overselling. Professional message queuing tools such as rabbitmq are needed to realize the advanced functions of message queuing. User Redis user redis implementation of message queue still has some shortcomings, you can find their own articles to supplement. We will not summarize them one by one here. The biggest problem is the problem of distributed clusters.

Redis optimistic lock realizes second kill function.

Its advantages are as follows:

Message queue consumes a lot of memory, with 10000 requests and needs to operate 10000 out of queue. It is easy to cause memory resources to explode instantly.

Using optimistic locking logic, CPU consumes relatively low consumption and takes up less memory resources.

$redis = new redis (); $result = $redis- > connect ('127.0.0.1 get, 6379); $cachekey = $redis- > get ("cachekey"); $number = 100; / snapping up quantity if ($cachekeywatch ("cachekey"); $redis- > multi (); / / set delay to facilitate testing. Sleep (5); / insert panic buying data $redis- > hSet ("cachekeyList", "user_id_" .mt _ rand (1, 9999), time ()); $redis- > set ("cachekey", $cachekey+1); $result = $redis- > exec (); if ($result) {$cachekeyList = $redis- > hGetAll ("cachekeyList"); echo "Congratulations". $cachekeyList. "snap purchase success!" ;} else {echo "keep up your efforts"; exit;}}

These are the details of the redis second kill scenario solution, please pay attention to other related articles!

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.

Share To

Database

Wechat

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

12
Report