Php solves the problem of inventory negative numbers caused by high-traffic concurrent warehouse receiving such as flash sales and flash sales _ php instance

Source: Internet
Author: User
Tags flock
Recently, I was working on a group buying project and encountered a problem: the inventory quantity was limited during flash sales, flash sales, lottery and other activities, but at the same time the number of people who placed orders exceeded the inventory quantity, this will cause the goods to be sold out. So how can we solve this problem? My idea is as follows: we know that SQL statements are handled in a database. Suppose the process of purchasing a product is as follows:

Sql1: Query product inventory

If (inventory quantity> 0) {// generate order... sql2: inventory-1}

When there is no concurrency, the above process looks so perfect. Assume that two people place orders at the same time, and there is only one inventory. In sql1stage, the inventory queried by two people is greater than 0, therefore, SQL 2 is executed in the end, and the inventory is changed to-1 at the end. If it is sold out, you can either fill the inventory or wait for the user to complain.

A popular solution to this problem:

1. when an additional single process is used to process a queue and place an order request in the queue, there will be no concurrency issues after each process. However, additional background processes and latency issues are not considered.

2. optimistic database lock, which roughly means to query the inventory first, then immediately add inventory + 1, and then query the inventory before updating the inventory after the order is generated, check whether the inventory quantity is consistent with the expected quantity. If the inventory quantity is inconsistent, the system will roll back, prompting you that the inventory is insufficient.

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

4. with file exclusive lock, a file is locked with flock when processing the order request. If the lock fails, other orders are being processed, either waiting or prompting the user "the server is busy"

This article will talk about 4th solutions. The Code is as follows:

Blocking (wait) Mode

<? Php $ fp = fopen ("lock.txt", "w +"); if (flock ($ fp, LOCK_EX )){//.. process Order flock ($ fp, LOCK_UN);} fclose ($ fp);?>

Non-Blocking Mode

<? Php $ fp = fopen ("lock.txt", "w +"); if (flock ($ fp, LOCK_EX | LOCK_NB )){//.. process the order flock ($ fp, LOCK_UN);} else {echo "the system is busy. Please try again later";} fclose ($ fp);?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.