Make a simple second kill with Redis.

Source: Internet
Author: User

Here is a simple next order operation
<?phpinclude "MMysql.class.php"; $configArr =[' host ' =>, ' Port ' =>, ' user ' =>, ' passwd ' =>, ' dbname ' => ]; $db = new Mmysql ($CONFIGARR); $sql = "SELECT * from sdb_b2c_products where product_id= '"; $product = $db->dosql ($sql if (! $product) {echo "Error:not find product"; return;} $product = $product [0];if ($product [' Store ']-$product [' Freez ']<1) {echo "Error:no store"; return;} $sql = "SELECT * from Sdb_b2c_member_addrs where member_id= ' 256187 '"; $addr = $db->dosql ($sql); $addr = $addr [0]; $data =[ ' order_id ' =>date (' Ymdhis '). Rand (100,999), ' total_amount ' = $product [' Price '], ' final_amount ' and ' = ' $product [' Price '], ' pay_status ' = ' 0 ', ' createtime ' =>time (), ' shipping_id ' = ' + ', ' shipping ' = ' Yun da ', ' member_id ' = > ' 636389 ', ' ship_area ' + $addr [' area '], ' shipname ' = ' + $addr [' name '], ' ship_addr ' + $addr [' addr ']; er = $db->insert (' sdb_b2c_orders ', $data); if (er) {$sql = "update sdb_b2c_products set freez=freez+1 where product_id = '->dosql '; $db ($sql); echo "Order create success"; return;}Else{echo "Error:order create fail"; return;}? >

The code is interpreted as:

Find goods in the product table, get physical inventory and virtual inventory, if the time inventory minus virtual inventory less than 1, it means that there is no inventory

If there is physical inventory, go to the memeber_id as ' 256187 ' for the address information of the member, and then create the order

If the order creation is successful, the virtual inventory is updated.

Using JMeter to test the effect under concurrency, set 500 threads to simulate concurrency

Pre-test set the actual inventory store is 50, virtual inventory Freez is 0, under normal circumstances Freez can only reach 50, otherwise there will be oversold situation

The actual test, in the case of high concurrency, is usually greater than 50, at around 54 Freez.

Due to the availability of inventory, there is a time lag in the middle of changing the inventory, and there is a problem of oversold in all high concurrency.

The following code has been modified to use the Redis list as a queue.

<?php$store=50; $redis =new redis () $result = $redis->connect (' 127.0.0.1 ', 6379); $res = $redis->llen (' pro38 ') , echo $res, $count = $store-$res; for ($i =0; $i < $count; $i + +) {$redis->lpush (' pro38 ', 1);} echo $redis->llen (' pro38 ');? >

Above is a list that adds a Redis inventory of 50

The following post-order code

<?phpinclude "MMysql.class.php"; $configArr =[' host ' = ' 121.41.38.44 ', ' port ' = ' 3306 ', ' user ' = ' Yuancheng ', ' passwd ' = ' Yuancheng ', ' dbname ' = ' laiyifendb ',]; $db = new Mmysql ($CONFIGARR); $redis =new Redis (); $ result= $redis->connect (' 127.0.0.1 ', 6379), $count = $redis->lpop (' pro38 '), if (! $count) {echo "Error:no store Redis "; return;} $sql = "SELECT * from sdb_b2c_products where product_id= '"; $product = $db->dosql ($sql); if (! $product) {echo "Error: Not find product "; return;} $product = $product [0];if ($product [' Store ']-$product [' Freez ']<1) {echo "Error:no store"; return;} $sql = "SELECT * from Sdb_b2c_member_addrs where member_id= ' 256187 '"; $addr = $db->dosql ($sql); $addr = $addr [0]; $data =[ ' order_id ' =>date (' Ymdhis '). Rand (100,999), ' total_amount ' = $product [' Price '], ' final_amount ' and ' = ' $product [' Price '], ' pay_status ' = ' 0 ', ' createtime ' =>time (), ' shipping_id ' = ' + ', ' shipping ' = ' Yun da ', ' member_id ' = > ' 636389 ', ' ship_area ' + $addr [' area '], ' shipname ' and ' = ' $addr [' name '], ' ship_addr ' + $addr[' addr ',]; er = $db->insert (' sdb_b2c_orders ', $data); if (er) {$sql = "Update sdb_b2c_products set freez= freez+1 where product_id= ' ""; $db->dosql ($sql); echo "Order create success"; return;} Else{echo "Error:order create fail"; return;}? >

After testing, this method can prevent the occurrence of oversold

Make a simple second kill with Redis.

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.