Home  >  Article  >  Backend Development  >  How to prevent high concurrency in php

How to prevent high concurrency in php

王林
王林Original
2019-09-30 17:44:043758browse

How to prevent high concurrency in php

1. Reasons for over-issuance

Assume that in a rush-buying scenario, we only have 100 products in total. At the last moment, we have consumed 99 products were purchased, only the last one is left. 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.

How to prevent high concurrency in php

In the picture above, this 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.

File lock ideas

For applications where the daily IP is not high or the number of concurrency is not very large, generally there is no need to consider these! There is no problem at all with normal file manipulation methods. But if the concurrency is high, when we read and write files, it is very likely that multiple processes will operate on the next file. If the access to the file is not exclusive at this time, it will easily cause data loss.

How to prevent high concurrency in php

Optimization solution: use non-blocking file exclusive lock

<?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);

?>

Recommended tutorial: PHP video tutorial

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

Statement:
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