PHP high concurrency

1: Process the order

  public function oneSecondSnap()
    {
        $user_id = 1;
        $goods_id = 1;
        $number = 1;
        $sql = "select count as number from food where id ='$goods_id' ";//Unlock the data of goods_id='$goods_id' and sku_id='$sku_id' in the ih_store data at this time is locked (Note 3), Other transactions must wait for this transaction to commit before executing
        $row = DB::select($sql);
        if ($row[0]->number > 0) {//High concurrency will lead to oversold
            $order_sn = $this->build_order_no();
            //Generate orders
            $sql = "insert into orders(user_id,food_id,order_id)
            values('$user_id','$goods_id','$order_sn')";
            $order_rs = DB::insert($sql);
            // stock reduction
            $sql = "update food set count = count - " . $number . " where id = '$goods_id'";
            $store_rs = DB::update($sql);
            Log::info('Inventory reduction succeeded');

        } else {
            Log::info('Not enough stock');
        }
    }

    /**
     * Generate unique order
     */
    public function build_order_no()
    {
        return date('ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }

2: Use jmeter to open multi-threaded access test

For specific operations, please check https://blog.csdn.net/qq_29099209/article/details/79998731

3: View analysis results

Set the product to 10 and initiate 20 threads to request the purchase in 0.1 seconds, and found that the product was oversold


4: Solutions

https://blog.csdn.net/nuli888/article/details/51865401

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325905172&siteId=291194637