Efficient Webhook Handling in Laravel Using Unique Jobs

Ibrar Hussain
2 min readJul 15, 2024

One of the Laravel projects I’m working on connects to multiple third-party applications, and we use webhooks to sync data between them.

Recently, I encountered an issue where our Laravel application was receiving multiple webhook calls from a third-party application for the same object within 10 seconds. Processing each of these calls individually is inefficient because it updates the same data in the database multiple times, draining our server and database resources, especially under high traffic.

To optimize this, we want to process only a single webhook call out of the multiple received within a 10-second window. Laravel has a built-in feature called Unique Jobs that can help us achieve this.

Here’s how we can use this feature:

First, let’s define our queue job:

class UpdateProductFromWebhook implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public $product;

/**
* Create the event listener for this class.
*/
public function __construct($product)
{
$this->product = $product;
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// Logic here
}
}

To make this job unique, we need to implement the ShouldBeUnique interface. We also need to set the uniqueFor property to 10 seconds and define the uniqueId method to use the product ID as the…

--

--

Ibrar Hussain

I am a full stack web developer with experience working on Laravel and Vue. I am passionate about exploring and learning new technologies in the field.