Home >Database >Mysql Tutorial >Introduction to Redis's method of implementing flash sales (with code)
This article brings you an introduction to the method of realizing flash sales in Redis (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Introduction: Everyone must know about flash sales. Access requests will surge in a short period of time. At the same time, it is necessary to ensure that the data is not oversold and the data is accurate. There are still some technical challenges. Unfortunately, there has never been a chance to implement it in the project. After reading some more information, I plan to experiment. The following code is only for testing, the environment is relatively simple, please modify it according to the actual situation.
Create flash sale queue
Before starting the flash sale, first put the product into the queue, as follows
/** * 创建秒杀列表 */ public function createList() { $count = 30; $redisKey = 'goods_list'; for ($i = 1; $i = $count) { break; } Redis::rpush($redisKey, $i); } }
After execution, view it in Redis There are 30 product IDs under
, and the data is normal.
The next step is to use Redis’s lpop
command to obtain the product ID, taking advantage of the atomicity of Redis.
/** * 秒杀 */ public function buy() { // 随机用户名,无意义,仅做标记 $username = Hash::make(now()); if ($goodsId = Redis::lpop('goods_list')) { // 购买成功 Redis::hset('buy_success', $goodsId, $username); } else { // 购买失败 Redis::incr('buy_fail'); } }
As above, the code is simplified. After purchase, the success or failure is only recorded. In actual applications, it will of course be more complicated, but you should be careful not to operate Mysql synchronously. One more thing, Hash:make(now())
Even if the values are the same, the same data will not be generated, refer to here.
The last step is to test, use ab test, executeab -c 300 -n 3000 http://localhost/buy/
, the meaning of the above command It is 300 concurrency, with a total of 3000 requests
The execution is completed, the speed is not fast, and there are 794 access failures. Let’s see if the data is correct. Print buy_success
value
30 winners in the page. Let’s take a look at the number of failed flash sales.
is not an accurate number. 2165 30 is the number of successful requests. Adding in the 794 failed requests, the total is 2989, which is still less than 3000.
Conclusion
The above tests have shortcomings, such as slow response speed, failed requests, and inaccurate failure counts. It seems that there are many places to optimize, not just the code layer. During the test, I forgot to turn off the access record to the database, which may have some impact.
The good thing is that the number of successful flash sales is accurate and not oversold. [Related recommendations: Redis tutorial]
The above is the detailed content of Introduction to Redis's method of implementing flash sales (with code). For more information, please follow other related articles on the PHP Chinese website!