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

    评论

报告相同问题?

悬赏问题

  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播