How does PHP solve the issue of concurrent order placement? (Not necessarily orders. for example)

Source: Internet
Author: User
We know that SQL statements are processed in a database. Assume that the procedure for purchasing a product is as follows: {code ...} 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 the sql1stage, the inventory queried by both people is & amp; gt; 0 ,... we know that SQL statements are processed in a database. Assume that the process for purchasing a product is as follows:

Sql1: Query commodity 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.
What is a better solution to this problem?

Reply content:

We know that SQL statements are processed in a database. Assume that the process for purchasing a product is as follows:

Sql1: Query commodity 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.
What is a better solution to this problem?

  • Transaction Processing Mechanism

  • Data Tables can also be locked (if you useInnoDBThe engine uses the row lock; if you useMyISAMThe engine uses table locks)

The inventory field is changed to unsigned int.
In this case, no <0, at most, an error occurs in SQL Execution.
You can also use memcached/redis to cache the results and query them from the cache.

1. MySQL database locking, optimistic and pessimistic locks
2. Use queues to generate orders one by one

The inventory cache is generally there, if there is a concurrency requirement. There is also a lock, or put it into the queue to execute SQL.

There is a very stupid way. The following is an example of the assumption:

Sql1: query the product inventory (assuming the inventory is 10) if (10> 0) {// generate the order... sql2: 10-1 (the inventory is 9 at this time) // re-check the inventory sql3: query the commodity inventory = 9 (if there is concurrency at this time, the inventory may be 8 or 7. An exception is thrown and the transaction is rolled back. The order is invalid .)}

Redis is generally used to process high concurrency. Redis uses the list Data Structure of redis (high concurrency, of course, requires an asynchronous Queue) to store the request order information, and then enables the Redis transaction mechanism (see: http://redis.io/topics/transactions), before adding a list of the specified key, determine whether the length of the list and other than the specified item inventory value saved in redis, if it exceeds, do not operate, if not, then insert, after the list is inserted, check whether the length of the list exceeds the inventory value of the specified item saved in redis. If it exceeds the value, roll back. Otherwise, submit the list.

I would like to ask, how does one deal with high concurrency?

1. Maintain the atomicity of operations with transactions,
2. Use select... for update to lock the data when modifying the inventory.

update table set n = n-x where n >= x

No transaction is required. You can directly obtain the affected row count to determine whether the row is successfully deducted. The resulting row must be 0 because n = 0.

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.