DEV Community

Ibrar Hussain
Ibrar Hussain

Posted on • Originally published at Medium

Efficient Webhook Handling in Laravel Using Unique Jobs

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
    }
}

Enter fullscreen mode Exit fullscreen mode

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 unique identifier. Here’s the updated job class:

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

     /**
     * The number of seconds after which the job's unique lock will be released.
     *
     * @var int
     */
    public $uniqueFor = 10;  // 10 seconds

    public $product;

    /**
     * The unique ID of the job.
     */
    public function uniqueId(): string
    {
        return sprintf('Product::%s', $this->product->id);
    }

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

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

Enter fullscreen mode Exit fullscreen mode

With these changes, the job will be unique for 10 seconds. If multiple webhook calls for the same product are received within this period, only the first one will be processed, and the rest will be ignored, thus conserving server and database resources.

This concept can be applied to your application's existing queue jobs to prevent duplicate processing and improve efficiency. By ensuring that jobs are unique within a specific time frame, you can reduce unnecessary load on your server and database, leading to better performance and resource utilization.

Top comments (0)