Store flash sales to prevent oversold inventory

Source: Internet
Author: User
PHPMALL1. flash sales at the mall, flash sales is a headache. the following three methods are used to prevent oversales of 1.1.mysql locks, pessimistic locks, and InnoDB row-level locks, index 1.2.mysql optimistic lock 1.3.redis atomic operation is required for query 2. test 3. source code address: github. comjingyanleiPHPMALL # "target =" _ blank "> PHP-MALL1. shopping mall flash sales, flash sales is a headache, the following three methods to prevent oversales
1. mysql lock mechanism, pessimistic lock InnoDB row-level lock solution, not recommended, great pressure on the database, if a deadlock will lead to continuous update, unless kill process 2. mysql optimistic lock can be used without a third party 3. redis incrby decrby atomic operation to prevent overselling 4. in order to facilitate expansion, the inventory class is abstracted out of the interface to facilitate future expansion. Other methods can also be used for implementation.
1.1.mysql lock mechanism, pessimistic lock, InnoDB row-level lock scheme, index required for query
1. the transaction level must be SERIALIZABLE level 2. check whether the inventory is sufficient for this purchase. for example, after id = 1 AND inventory> = 13.PDO update, check whether the returned status is correct! = False, and verify whether the number of affected rows is greater than 04. the database connection must use the same link. the single instance or DB link must be passed in. we recommend that you use a single instance. because a db class is found on the test site, the single instance is not implemented, therefore, use a stupid method to pass the database link. 5. update parts add verification purchase quantity conditions AND inventory> = 1
1.2.mysql optimistic lock
The version field is added to the database table, for example, version. the version number is + 1. if the operation order is updated, the version of the data increases sequentially without conflict. However, if different business operations are performed to modify the data of the same version, the first submitted operation (B in the figure below) will update the data version to 2, when A submits an update request after B and finds that the data version has been modified, the update operation of A fails. After updating a PDO update, verify whether the returned status is true and whether the number of affected rows is greater than 0.
1.3.redis atomic operation
Incr/decr atomic operation, incr increase, decr decrease // This cannot be taken out and placed into the php variable to judge the inventory; otherwise, phantom reads may occur, result in overselling if ($ redis_client-> decrby ($ key, $ parmas ['num'])>-1) {// inventory reduction $ goods = new Goods (); $ parmas ['version'] = 1; return $ goods-> subInventory ($ parmas);} else {// when purchasing multiple instances, if the inventory is insufficient, you need to add the quantity back; otherwise, the database will be reduced to inventory, and the product is not sold $ redis_client-> incrby ($ key, $ parmas ['num']); return false ;} when using redis for testing, the KEY: DEL goods_1 needs to be deleted each time the inventory is modified.
2. test
Configure usage // db pessimistic lock (transaction-level SERIALIZABLE) db2 mysql optimistic lock redis mode define ('ventory _ type', 'db2 '); concurrent hours may not cause problems, for example, if the query concurrency is larger than the general conference, the following code adds services/inventory/db/Inventory 2 seconds later to reproduce the oversold data. php 31 rows or so $ data = $ goods-> getInventory ($ params); sleep (2); // stop for 2 seconds, easy to test problems nginx configuration site mall.com local hosts bound domain name 10.211.55.100 mall.com (10.211.55.100 for the actual site ip) Test method using AB test, request 100, concurrency 10ab-n100-c10 http://mall.com
3. problem
Note: Because the inventory verification and deduction are placed before placing an order, the order quantity and the number of transactions opened can be reduced. However, if the order fails after the inventory deduction, if you do not have high requirements on inventory, you do not need to consider it. if you have high requirements on inventory, you need to save the order corresponding to the failed order deduction to the log, then, the inventory is restored asynchronously or the order is replenished to the user.

The above is the details of the mall flash sales to prevent oversold inventory. For more information, please pay attention to other articles related to the first PHP community!

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.