Home  >  Article  >  Backend Development  >  How PHP solves high concurrency problems

How PHP solves high concurrency problems

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-05-19 17:38:233921browse

This article will introduce to you how PHP solves high concurrency problems. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How PHP solves high concurrency problems

For example, at a highway intersection, 5 cars come and pass 5 cars per second, and the highway intersection operates normally. Suddenly, only 4 cars can pass through this intersection in one second, and the traffic flow is still the same. As a result, there will definitely be a traffic jam. (The feeling of 5 lanes suddenly turning into 4 lanes)

Video Course Recommendation →: "Concurrency Solution for Tens of Millions of Data (Theory and Practical Combat)"

Similarly, in a certain second, 20*500 available connection processes are working at full load, but there are still 10,000 new requests, no connection processes are available, and the system is trapped The abnormal state is also expected.

How PHP solves high concurrency problems

In fact, in normal non-high-concurrency business scenarios, similar situations occur. There is a problem with a certain business request interface, and the response time is extremely slow. The entire Web request The response time is very long, gradually filling up the number of available connections on the web server, and no connection process is available for other normal business requests.

The more frightening problem is that it is the behavioral characteristics of users. The more unavailable the system is, the more frequent users click. The vicious cycle eventually leads to an "avalanche" (one of the web machines hangs up, causing the traffic to be dispersed to On other machines that are working normally, the normal machines will also hang, and then a vicious circle will occur), bringing down the entire Web system.

Restart and overload protection

If an "avalanche" occurs in the system, restarting the service rashly will not solve the problem. The most common phenomenon is that after starting up, it hangs up immediately. At this time, it is best to deny traffic at the ingress layer and then restart. If services like redis/memcache are also down, you need to pay attention to "warming up" when restarting, and it may take a long time.

In flash sale and rush sale scenarios, the traffic is often beyond our system’s preparation and imagination. At this time, overload protection is necessary. Denying requests is also a protective measure if a full system load condition is detected. Setting up filtering on the front-end is the simplest way, but this approach is a behavior that is "criticized" by users. It is more appropriate to set overload protection at the CGI entry layer to quickly return direct requests from customers

Data security under high concurrency

We know that when multiple threads write the same file (Multiple threads run the same piece of code at the same time. If the result of each run is the same as that of a single-thread run, and the result is the same as expected, it is thread-safe). If it is a MySQL database, you can use its own lock mechanism to solve the problem. However, in large-scale concurrency scenarios, MySQL is not recommended. There is another problem in flash sale and rush sale scenarios, which is "over-delivery". If this aspect is not controlled carefully, excessive delivery will occur. We have also heard that some e-commerce companies conduct rush buying activities. After the buyer successfully purchases the product, the merchant does not recognize the order as valid and refuses to deliver the goods. The problem here may not necessarily be that the merchant is treacherous, but that it is caused by the risk of over-issuance at the technical level of the system.

The reason for over-issuance

Suppose in a certain rush-buying scenario, we only have 100 products in total. At the last moment, we have consumed 99 products, leaving only the last one. At this time, the system sent multiple concurrent requests. The product balances read by these requests were all 99, and then they all passed this balance judgment, which eventually led to over-issuance. (Same as the scene mentioned earlier in the article)

How PHP solves high concurrency problems

In the picture above, it resulted in concurrent user B also "successfully buying", allowing one more person to obtain the product. This scenario is very easy to occur in high concurrency situations.

Optimization plan 1: Set the inventory field number field to unsigned. When the inventory is 0, because the field cannot be a negative number, false will be returned

 <?php
 //优化方案1:将库存字段number字段设为unsigned,当库存为0时,因为字段不能为负数,将会返回false
 include(&#39;./mysql.php&#39;);
 $username = &#39;wang&#39;.rand(0,1000);
 //生成唯一订单
 function build_order_no(){
   return date(&#39;ymd&#39;).substr(implode(NULL, array_map(&#39;ord&#39;, str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
 }
 //记录日志
 function insertLog($event,$type=0,$username){
     global $conn;
     $sql="insert into ih_log(event,type,usernma)
     values(&#39;$event&#39;,&#39;$type&#39;,&#39;$username&#39;)";
     return mysqli_query($conn,$sql);
 }
 function insertOrder($order_sn,$user_id,$goods_id,$sku_id,$price,$username,$number)
 {
       global $conn;
       $sql="insert into ih_order(order_sn,user_id,goods_id,sku_id,price,username,number)
       values(&#39;$order_sn&#39;,&#39;$user_id&#39;,&#39;$goods_id&#39;,&#39;$sku_id&#39;,&#39;$price&#39;,&#39;$username&#39;,&#39;$number&#39;)";
      return  mysqli_query($conn,$sql);
 }
 //模拟下单操作
 //库存是否大于0
 $sql="select number from ih_store where goods_id=&#39;$goods_id&#39; and sku_id=&#39;$sku_id&#39; ";
 $rs=mysqli_query($conn,$sql);
 $row = $rs->fetch_assoc();
   if($row[&#39;number&#39;]>0){//高并发下会导致超卖
       if($row[&#39;number&#39;]<$number){
         return insertLog(&#39;库存不够&#39;,3,$username);
       }
       $order_sn=build_order_no();
       //库存减少
       $sql="update ih_store set number=number-{$number} where sku_id=&#39;$sku_id&#39; and number>0";
       $store_rs=mysqli_query($conn,$sql);
       if($store_rs){
           //生成订单
           insertOrder($order_sn,$user_id,$goods_id,$sku_id,$price,$username,$number);
           insertLog(&#39;库存减少成功&#39;,1,$username);
       }else{
           insertLog(&#39;库存减少失败&#39;,2,$username);
       }
   }else{
       insertLog(&#39;库存不够&#39;,3,$username);
   }
 ?>

Pessimistic lock idea

There are many ideas for solving thread safety, and we can start the discussion from the direction of "pessimistic locking".

Pessimistic lock, that is, when modifying data, the lock state is adopted to exclude modifications from external requests. When encountering a locked state, you must wait.

How PHP solves high concurrency problems

虽然上述的方案的确解决了线程安全的问题,但是,别忘记,我们的场景是“高并发”。也就是说,会很多这样的修改请求,每个请求都需要等待“锁”,某些线程可能永远都没有机会抢到这个“锁”,这种请求就会死在那里。同时,这种请求会很多,瞬间增大系统的平均响应时间,结果是可用连接数被耗尽,系统陷入异常。

优化方案2:使用MySQL的事务,锁住操作的行

 <?php
 //优化方案2:使用MySQL的事务,锁住操作的行
 include(&#39;./mysql.php&#39;);
 //生成唯一订单号
 function build_order_no(){
   return date(&#39;ymd&#39;).substr(implode(NULL, array_map(&#39;ord&#39;, str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
 }
 //记录日志
 function insertLog($event,$type=0){
     global $conn;
     $sql="insert into ih_log(event,type)
     values(&#39;$event&#39;,&#39;$type&#39;)";
     mysqli_query($conn,$sql);
 }
 //模拟下单操作
 //库存是否大于0
 mysqli_query($conn,"BEGIN");  //开始事务
 $sql="select number from ih_store where goods_id=&#39;$goods_id&#39; and sku_id=&#39;$sku_id&#39; FOR UPDATE";//此时这条记录被锁住,其它事务必须等待此次事务提交后才能执行
 $rs=mysqli_query($conn,$sql);
 $row=$rs->fetch_assoc();
 if($row[&#39;number&#39;]>0){
     //生成订单
     $order_sn=build_order_no();
     $sql="insert into ih_order(order_sn,user_id,goods_id,sku_id,price)
     values(&#39;$order_sn&#39;,&#39;$user_id&#39;,&#39;$goods_id&#39;,&#39;$sku_id&#39;,&#39;$price&#39;)";
     $order_rs=mysqli_query($conn,$sql);
     //库存减少
     $sql="update ih_store set number=number-{$number} where sku_id=&#39;$sku_id&#39;";
     $store_rs=mysqli_query($conn,$sql);
     if($store_rs){
       echo &#39;库存减少成功&#39;;
         insertLog(&#39;库存减少成功&#39;);
         mysqli_query($conn,"COMMIT");//事务提交即解锁
     }else{
       echo &#39;库存减少失败&#39;;
         insertLog(&#39;库存减少失败&#39;);
     }
 }else{
   echo &#39;库存不够&#39;;
     insertLog(&#39;库存不够&#39;);
     mysqli_query($conn,"ROLLBACK");
 }
 ?>

FIFO队列思路

那好,那么我们稍微修改一下上面的场景,我们直接将请求放入队列中的,采用FIFO(First Input First Output,先进先出),这样的话,我们就不会导致某些请求永远获取不到锁。看到这里,是不是有点强行将多线程变成单线程的感觉哈。

How PHP solves high concurrency problems

然后,我们现在解决了锁的问题,全部请求采用“先进先出”的队列方式来处理。那么新的问题来了,高并发的场景下,因为请求很多,很可能一瞬间将队列内存“撑爆”,然后系统又陷入到了异常状态。或者设计一个极大的内存队列,也是一种方案,但是,系统处理完一个队列内请求的速度根本无法和疯狂涌入队列中的数目相比。也就是说,队列内的请求会越积累越多,最终Web系统平均响应时候还是会大幅下降,系统还是陷入异常。

文件锁的思路

对于日IP不高或者说并发数不是很大的应用,一般不用考虑这些!用一般的文件操作方法完全没有问题。但如果并发高,在我们对文件进行读写操作时,很有可能多个进程对进一文件进行操作,如果这时不对文件的访问进行相应的独占,就容易造成数据丢失

优化方案4:使用非阻塞的文件排他锁

 <?php
 //优化方案4:使用非阻塞的文件排他锁
 include (&#39;./mysql.php&#39;);
 //生成唯一订单号
 function build_order_no(){
   return date(&#39;ymd&#39;).substr(implode(NULL, array_map(&#39;ord&#39;, str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
 }
 //记录日志
 function insertLog($event,$type=0){
     global $conn;
     $sql="insert into ih_log(event,type)
     values(&#39;$event&#39;,&#39;$type&#39;)";
     mysqli_query($conn,$sql);
 }
 $fp = fopen("lock.txt", "w+");
 if(!flock($fp,LOCK_EX | LOCK_NB)){
     echo "系统繁忙,请稍后再试";
     return;
 }
 //下单
 $sql="select number from ih_store where goods_id=&#39;$goods_id&#39; and sku_id=&#39;$sku_id&#39;";
 $rs =  mysqli_query($conn,$sql);
 $row = $rs->fetch_assoc();
 if($row[&#39;number&#39;]>0){//库存是否大于0
     //模拟下单操作
     $order_sn=build_order_no();
     $sql="insert into ih_order(order_sn,user_id,goods_id,sku_id,price)
     values(&#39;$order_sn&#39;,&#39;$user_id&#39;,&#39;$goods_id&#39;,&#39;$sku_id&#39;,&#39;$price&#39;)";
     $order_rs =  mysqli_query($conn,$sql);
     //库存减少
     $sql="update ih_store set number=number-{$number} where sku_id=&#39;$sku_id&#39;";
     $store_rs =  mysqli_query($conn,$sql);
     if($store_rs){
       echo &#39;库存减少成功&#39;;
         insertLog(&#39;库存减少成功&#39;);
         flock($fp,LOCK_UN);//释放锁
     }else{
       echo &#39;库存减少失败&#39;;
         insertLog(&#39;库存减少失败&#39;);
     }
 }else{
   echo &#39;库存不够&#39;;
     insertLog(&#39;库存不够&#39;);
 }
 fclose($fp);
  ?>
 <?php
 //优化方案4:使用非阻塞的文件排他锁
 include (&#39;./mysql.php&#39;);
 //生成唯一订单号
 function build_order_no(){
   return date(&#39;ymd&#39;).substr(implode(NULL, array_map(&#39;ord&#39;, str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
 }
 //记录日志
 function insertLog($event,$type=0){
     global $conn;
     $sql="insert into ih_log(event,type)
     values(&#39;$event&#39;,&#39;$type&#39;)";
     mysqli_query($conn,$sql);
 }
 $fp = fopen("lock.txt", "w+");
 if(!flock($fp,LOCK_EX | LOCK_NB)){
     echo "系统繁忙,请稍后再试";
     return;
 }
 //下单
 $sql="select number from ih_store where goods_id=&#39;$goods_id&#39; and sku_id=&#39;$sku_id&#39;";
 $rs =  mysqli_query($conn,$sql);
 $row = $rs->fetch_assoc();
 if($row[&#39;number&#39;]>0){//库存是否大于0
     //模拟下单操作
     $order_sn=build_order_no();
     $sql="insert into ih_order(order_sn,user_id,goods_id,sku_id,price)
     values(&#39;$order_sn&#39;,&#39;$user_id&#39;,&#39;$goods_id&#39;,&#39;$sku_id&#39;,&#39;$price&#39;)";
     $order_rs =  mysqli_query($conn,$sql);
     //库存减少
     $sql="update ih_store set number=number-{$number} where sku_id=&#39;$sku_id&#39;";
     $store_rs =  mysqli_query($conn,$sql);
     if($store_rs){
       echo &#39;库存减少成功&#39;;
         insertLog(&#39;库存减少成功&#39;);
         flock($fp,LOCK_UN);//释放锁
     }else{
       echo &#39;库存减少失败&#39;;
         insertLog(&#39;库存减少失败&#39;);
     }
 }else{
   echo &#39;库存不够&#39;;
     insertLog(&#39;库存不够&#39;);
 }
 fclose($fp);
  ?>

乐观锁思路

这个时候,我们就可以讨论一下“乐观锁”的思路了。乐观锁,是相对于“悲观锁”采用更为宽松的加锁机制,大都是采用带版本号(Version)更新。实现就是,这个数据所有请求都有资格去修改,但会获得一个该数据的版本号,只有版本号符合的才能更新成功,其他的返回抢购失败。这样的话,我们就不需要考虑队列的问题,不过,它会增大CPU的计算开销。但是,综合来说,这是一个比较好的解决方案。

How PHP solves high concurrency problems

有很多软件和服务都“乐观锁”功能的支持,例如Redis中的watch就是其中之一。通过这个实现,我们保证了数据的安全。

优化方案5:Redis中的watch

 <?php
 $redis = new redis();
  $result = $redis->connect(&#39;127.0.0.1&#39;, 6379);
  echo $mywatchkey = $redis->get("mywatchkey");
 /*
   //插入抢购数据
  if($mywatchkey>0)
  {
      $redis->watch("mywatchkey");
   //启动一个新的事务。
     $redis->multi();
    $redis->set("mywatchkey",$mywatchkey-1);
    $result = $redis->exec();
    if($result) {
       $redis->hSet("watchkeylist","user_".mt_rand(1,99999),time());
       $watchkeylist = $redis->hGetAll("watchkeylist");
         echo "抢购成功!<br/>";
         $re = $mywatchkey - 1;  
         echo "剩余数量:".$re."<br/>";
         echo "用户列表:<pre class="brush:php;toolbar:false">";
         print_r($watchkeylist);
    }else{
       echo "手气不好,再抢购!";exit;
    } 
  }else{
      // $redis->hSet("watchkeylist","user_".mt_rand(1,99999),"12");
      //  $watchkeylist = $redis->hGetAll("watchkeylist");
         echo "fail!<br/>";   
         echo ".no result<br/>";
         echo "用户列表:<pre class="brush:php;toolbar:false">";
       //  var_dump($watchkeylist); 
  }*/
 $rob_total = 100;   //抢购数量
 if($mywatchkey<=$rob_total){
     $redis->watch("mywatchkey");
     $redis->multi(); //在当前连接上启动一个新的事务。
     //插入抢购数据
     $redis->set("mywatchkey",$mywatchkey+1);
     $rob_result = $redis->exec();
     if($rob_result){
          $redis->hSet("watchkeylist","user_".mt_rand(1, 9999),$mywatchkey);
         $mywatchlist = $redis->hGetAll("watchkeylist");
         echo "抢购成功!<br/>";
       
         echo "剩余数量:".($rob_total-$mywatchkey-1)."<br/>";
         echo "用户列表:<pre class="brush:php;toolbar:false">";
         var_dump($mywatchlist);
     }else{
           $redis->hSet("watchkeylist","user_".mt_rand(1, 9999),&#39;meiqiangdao&#39;);
         echo "手气不好,再抢购!";exit;
     }
 }
 ?>

推荐学习:php视频教程

The above is the detailed content of How PHP solves high concurrency problems. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete