Mall snapped up seconds to prevent inventory oversold

Source: Internet
Author: User
Tags delete key

http://www.php.cn/"target=" _blank ">php-mall

1. Mall snapping, seconds to kill inventory oversold is a more headache, the following three ways to prevent oversold

1.mysql lock mechanism, pessimistic lock innodb row-level lock scheme, is not recommended to use, the database pressure is large, if a deadlock will cause can not be updated, unless the kill process 2.mysql optimistic lock does not use a third-party scenario may use this scenario 3.redis Incrby Decrby atomic operation to prevent oversold 4. In order to facilitate the expansion, the inventory class abstraction out of the interface, easy to expand later, you can also use other ways to achieve

1.1.mysql lock mechanism, pessimistic lock, InnoDB row-level lock scheme, query need to use index

1. The transaction level must be SERIALIZABLE level 2. The query condition verifies that the inventory is sufficed purchase, for example: id = 1 and Inventory >=13.pdo Update, not only to verify that the return status is!==false, Also verify that the number of rows affected is greater than 04. Database links must use the same link, a single case or a DB link passed in, it is recommended to use a single case, because the test online to find a DB class, did not implement a single case, so use a more stupid method, pass the database link 5.update pieces to increase the number of verification purchase conditions and Inventory >=1

1.2.mysql optimistic Lock

Database tables Add version fields, such as version, each time the revision number is +1 if the update operation is performed sequentially, the version of the data is incremented sequentially, and no conflict occurs. However, if there are different business operations to modify the same version of the data, then the first commit operation (Figure B) will update the data version to 2, when a after B commits the update found that the data version has been modified, then A's update operation will fail. After the PDO update is updated, not only does it verify that the return status is true, but also verifies that the number of rows affected is greater than 0

1.3.redis Atomic operation

INCR/DECR atomic operation, incr increase, DECR reduction///here can not be removed after the PHP variable to determine the inventory, otherwise there will be phantom read, resulting in oversold if ($redis _client->decrby ($key, $parmas [' num ']) >-1) {        //minus inventory        $goods = new Goods ();        $parmas [' version '] = 1;        Return $goods->subinventory ($parmas);} else {    //when purchasing multiple, such as insufficient inventory, need to add the quantity back, otherwise there will be a library to reduce inventory, the goods did not sell to    $redis _client->incrby ($key, $parmas [' num ']);    return false;} When using Redis testing, you need to delete Key:del every time you finish modifying inventory goods_1

2. Testing

Configure Usage//db pessimistic lock (transaction level serializable) DB2 MySQL optimistic lock Redis redis mode define (' Inventory_type ', ' DB2 '), concurrent hours may not be a problem, such as large concurrency will have oversold image, In order to be able to reproduce the oversold the following code is added 2 seconds later services/inventory/db/inventory.php 31 lines around $data = $goods->getinventory ($params); sleep (2); Stop for 2 seconds, easy to test the problem nginx configuration site mall.com native hosts bind domain name 10.211.55.100 mall.com (10.211.55.100 for real site IP) test method using AB test, request 100, concurrent 10AB- N100-C10 http://mall.com

3. Questions

 note due to the order process, the verification inventory and deduction inventory are placed before placing the order, you can reduce the number of orders and open transactions, but if the user after deducting inventory after the order process failure, There will be less selling phenomenon if the inventory requirements are not high, you can not consider, if the inventory requirements are high, you need to reduce the success of a single failure to save the order of the orders to the journal, and then asynchronous processing to restore inventory or to the user to fill the 
Related Article

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.