Home > Backend Development > PHP Problem > How to deal with high concurrency in php

How to deal with high concurrency in php

王林
Release: 2023-02-24 15:02:01
Original
3191 people have browsed it

How to deal with high concurrency in php

Data security under high concurrency

We know that when multiple threads write to the same file, "thread safety" will exist (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, the use of 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 takes the photo, 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.

1. Reasons for over-delivery

Assume that in a certain rush-buying scenario, we only have 100 products in total, and at the last moment, we have consumed 99 products. Only the last one remains. 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 to deal with high concurrency in php

2. Optimistic lock

Optimistic lock is used compared to "pessimistic lock" More relaxed locking mechanisms mostly use version updates. The implementation is that all requests for this data are eligible to be modified, but a version number of the data will be obtained. Only those with consistent version numbers can be updated successfully, and other requests will be returned to failure.

In this case, we do not need to consider the queue issue, but it will increase the computational overhead of the CPU. However, overall, this is a better solution.

How to deal with high concurrency in php

There are many software and services that support the "optimistic locking" function, such as watch in Redis is one of them. With this implementation, we ensure data security.

Optimization plan: watch in Redis

<?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;
    }
}
?>
Copy after login

The above content is for reference only!

Recommended tutorial: PHP video tutorial

The above is the detailed content of How to deal with high concurrency in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template