Solve the problem of redis seconds killing and overselling
Xiaobian to share with you to solve the redis spike oversold problem, I hope you read this article after the harvest, let us discuss it together!
First, generate inventory count
public function kucun() { //There are ten stocks $count=10; //add to redis list for($i=0;$ilpush('kucun',111111111); } self::dd(Predis::getInstance()->lrange('kucun',0,-1)); }
After the use of redis lpop or rpop to cut the list, before using llen or incr to judge the data, there will be oversold phenomenon, so here the use of lpop logic to solve the oversold problem
public function ru() { //judgment counter if (Predis::getInstance()->lpop('kucun')) { $user=User::where('user_id',1082)->find(); //Deposit Member ID Predis::getInstance()->lpush('user',$user['user_id']); //Counters accumulate by 1 // Predis::getInstance()->incr('number'); echo 'add kill successful'; exit(); }else{ echo 'activity as of'; exit(); } }
Test:
ab -r -n 1000 -c 1000 http://149.28.16.212/index/index/ru
After reading this article, I believe you have a certain understanding of solving the problem of redis spike oversold, want to know more related knowledge, welcome to pay attention to the industry information channel, thank you for reading!