search
HomeBackend DevelopmentPHP ProblemHow to prevent high concurrency in php

How to prevent high concurrency in php

Sep 30, 2019 pm 05:44 PM
phppreventHigh concurrency

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools