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 use Redis to achieve second kill

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

Share

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

How to use Redis to achieve second kill? Many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can gain something.

Introduction: as we all know, requests for access will surge in a short period of time, while ensuring that they will not be oversold and the accuracy of the data. There are still some technical challenges. Unfortunately, there has been no chance to achieve it in the project. After reading some data, I am going to experiment. The following code is for testing only, the environment is relatively simple, please modify it according to the actual situation.

Create a second kill queue

Before starting the second kill, put the goods in the queue, as follows

/ * create a second kill list * / public function createList () {$count = 30; $redisKey = 'goods_list'; for ($I = 1; $I = $count) {break;} Redis::rpush ($redisKey, $I);}}

After execution, take a look at it in Redis

There are 30 merchandise ID, the data is normal.

SecKill

The next critical step is to use Redis's lpop command to get the merchandise ID, taking advantage of the atomicity of Redis.

/ * second kill * / public function buy () {/ / random user name, meaningless, only marked $username = Hash::make (now ()); if ($goodsId = Redis::lpop ('goods_list')) {/ / purchased successful Redis::hset (' buy_success', $goodsId, $username) } else {/ / failed to purchase Redis::incr ('buy_fail');}}

As above, the code is simplified, and after the purchase, the success is just a record. In practical applications, of course, it will be more complex, but it is important to note that do not synchronize the operation of Mysql. By the way, Hash:make (now ()) will not generate the same data even if it has the same value, see here.

test

Finally, there is a test. Use ab test to execute ab-c 3000-n 3000 http://localhost/buy/. The above command means 300 concurrency, a total of 3000 requests.

The execution was completed, not very fast, and there were 794 failed visits. Let's see if the data are correct. Print buy_success values in the page

30 winners. Let's take a look at the number of second kill failures.

Not an exact number, 2165: 30 is the number of all requests successful, plus the number of failures, the total is 2989, still less than 3000.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, 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.

Share To

Database

Wechat

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

12
Report