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

Source: Internet
Author: User
Tags flock
Php solves the problem of inventory negative numbers caused by high-traffic concurrent warehouse receiving such as flash sales and flash sales. We know that SQL statements are processed in a database. assume that the process for purchasing a product is as follows: sql1: query the product inventory if (inventory quantity 0) {generate an order... sql2: inventory-1} when we do not know that the SQL statement for database processing is processed in a single line, we assume that the process for 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);?>

Sql1: query commodity inventory if (inventory quantity 0) {// Generate order... sql2: inventory-1} when no...

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.