TIP 02 / 06

Suppress Duplicate Job Dispatches with Shouldbeunique.

When only one queued job per resource should be pending, implement ShouldBeUnique and return a resource-specific key. Laravel uses that key to hold a cache lock while the job is queued and processing.

code
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;

class RebuildAccountProjection implements ShouldQueue, ShouldBeUnique
{
    public function __construct(public int $accountId) {}

    public int $uniqueFor = 900;

    public function uniqueId(): string
    {
        return (string) $this->accountId;
    }
}

Use a cache store available to every queue worker when jobs run on multiple servers. Choose a lock lifetime that safely covers the job, and keep the handler idempotent because jobs can still be retried.