Home Backend Development PHP Tutorial PHP solves the problem of negative inventory caused by concurrent warehousing of large traffic such as flash sales and lottery draws_PHP Tutorial

PHP solves the problem of negative inventory caused by concurrent warehousing of large traffic such as flash sales and lottery draws_PHP Tutorial

Jul 13, 2016 am 10:24 AM
php

We know that database processing SQL is processed one by one. Assume that the process of purchasing goods is as follows:

sql1: Query product inventory

if(库存数量 > 0)
{
  //生成订单...
  sql2:库存-1
}
Copy after login

When there is no concurrency, the above process looks so perfect. Suppose two people place orders at the same time, and there is only 1 inventory. In the sql1 stage, the inventory queried by both people is >0, so in the end both After executing sql2, the inventory finally became -1, indicating that it was oversold. Either replenish the inventory or wait for user complaints.

The more popular ideas to solve this problem:

1. Use an additional single process to process a queue, place order requests in the queue, and process them one by one, so there will be no concurrency issues. However, additional background processes and delay issues will not be considered.

2. Database optimistic locking, which roughly means querying the inventory first, then immediately adding 1 to the inventory, and then after the order is generated, query the inventory again before updating the inventory to see if it is consistent with the expected inventory quantity. It rolls back and prompts the user that the inventory is insufficient.

3. Judging based on the update results, we can add a judgment condition update...where inventory>0 in sql2. If false is returned, it means that the inventory is insufficient and the transaction will be rolled back.

4. With the help of file exclusive lock, when processing an order request, use flock to lock a file. If the lock fails, it means that other orders are being processed. At this time, either wait or directly prompt the user "server busy"

This article is going to talk about the 4th solution. The approximate code is as follows:

Blocking (waiting) mode

<?php
$fp = fopen("lock.txt", "w+");
if(flock($fp,LOCK_EX))
{
  //..处理订单
  flock($fp,LOCK_UN);
}
fclose($fp);
?>
Copy after login

Non-blocking mode

<?php
$fp = fopen("lock.txt", "w+");
if(flock($fp,LOCK_EX | LOCK_NB))
{
  //..处理订单
  flock($fp,LOCK_UN);
}
else
{
  echo "系统繁忙,请稍后再试";
}

fclose($fp);
?>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825404.htmlTechArticleWe know that the database processes SQL one by one. Assume that the process of purchasing goods is as follows: sql1: Query goods Inventory if(inventory quantity 0){ //Generate order... sql2:inventory-1} When there is no...
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles