Home > Article > PHP Framework > laravel high concurrency lottery flash sale solution
The following tutorial column from Laravel will give you laravel’s high concurrency lottery flash sale solution. I hope it will be helpful to friends in need!
<span style="font-size: 16px;">8-core 16G</span>
server<span style="font-size: 16px;">Jmeter</span>
Concurrency<span style="font-size: 16px;">2000</span>
Do not Testing on a computer. Due to network reasons, it is normal to test concurrency of
<span style="font-size: 16px;">1000</span>
without locking on this machine. You can buy a test machine on Alibaba Cloud
sql plus shared lock, stock Decrease the field by 1. Returning success means success, returning failure means decrement failed. The stock field is unsigned
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateStockTestTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('stock_test', function (Blueprint $table) { $table->increments('id'); $table->integer('stock')->default(0)->comment('库存1'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('stock_test'); } }
$model = new \App\Models\StockTest(); $id = $request->input('id',1); try { // 手动开始事务 DB::beginTransaction(); // sql加共享锁,stock字段减1。返回成功表示成功,返回失败表示自减失败。stock字段是无符号的 $is = DB::table('stock_test')->lockForUpdate()->increment('stock',-1); if($is) { log_info('id='.$id.'库存减1'); // 提交事务 DB::commit(); return response('成功',200); } else { return response('失败',201); } } catch (\Exception $exception) { // 回滚事务 DB::rollBack(); return response('失败',201); }
<span style="font-size: 16px;">lpush</span>
Join queue
<span style="font-size: 16px;">lpop</span>
Pop-up queue, the corresponding value is returned successfully, if it does not exist, it returns <span style="font-size: 16px;">null</span>
The above is the detailed content of laravel high concurrency lottery flash sale solution. For more information, please follow other related articles on the PHP Chinese website!