dongzan9069 2018-04-27 14:49
浏览 982

Laravel队列是否防止并发请求插入重复记录?

We have a Laravel API with a MariaDB database where we are having the issue of duplicate records in the database that are created by concurrent requests.

We assumed that implementing a queue would solve this, by handling one request at a time. But we still have the same issue of duplicate records.

In the application we have a check that looks for duplicates, but of course that won't work for concurrent requests that will create the same record at the same time.

We are unable to use a unique constraint at database level, partially because of Laravel's soft delete mechanism, and partially because of complex business logic that should allow duplicates if the value of one of the fields matches a certain set of values.

We use Redis as the queue driver.

Are we wrong by thinking that the queue should prevent these issues? Or did we somehow made a mistake somewhere implementing the queue?

This is a simplified version of the controller:

class CreateRecordJob extends Job implements SelfHandling
{

    public function __construct(array $data)
    {
        $this->data = $data;
    }

    public function handle()
    {
        $data = $this->data;

        // check if we have this record in the database already
        if(!$this->hasDuplicate($data)) {
            $this->createRecord();
        }
    }
}

In config/queue.php we have set Redis as the default queue driver:

'default' => env('QUEUE_DRIVER', 'redis'),

And also in config/queue.php we have this for the redis connection:

'redis' => [
    'driver'     => 'redis',
    'connection' => 'default',
    'queue'      => 'default',
    'expire'     => 60,
],
  • 写回答

1条回答

  • douyan5481 2018-04-27 15:45
    关注

    basicly - not
    you may find implementation of FIFO-compatable queue-drivers laravel has interface Illuminate\Console\Scheduling\Mutex for this purposes, byt yuo need to extend queueManager to use it

    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条