We are using Laravel with webhook heavily.
We are getting about 10 requests / second via webhook and some services are sending the multiple same request at the same time via webhook. We need to save only 1 entry for the same request. (service_id, param)
We are getting duplicate entry issue in the following logic:
$model = MyModel::where("service_id", $request->service_id)->where("param", $request->param)->first();
if($model)
{
//update model
}else {
//create new model
}
When I check service_id
and param
in MyModel, there are a lot of duplication entries.
I think while one is being created, the other same request can query and create new one.
Can anyone help me how to solve this issue? I think we can use Queue so that we can handle syncronously but Queue is not our option for now.
If you followed the documents from Laravel's supervisor configuration, there might be 8 daemons.
And there will be 8 running jobs simultaneously.
Or in the other case too, there might be some simultaneously running jobs at the same time.
We can solve this by using middleware inside Laravel Job.
// App\Jobs\MyJob.php
...
public function middleware()
{
return [new WithoutOverlapping($this->what_ever_id)];
}
This will prevent to run the jobs simultaneously for the same what_ever_id
values.
This solved my issue.