Queue hygiene in Laravel 13: routing, uniqueness, debouncing and overlap
Four tools for keeping queues predictable: Queue::route for central routing, ShouldBeUnique, the DebounceFor attribute, and WithoutOverlapping. When to use which, with the gotchas.
- Published
- Updated
- Reading time
- 4 minutes
- Written for
- Laravel 13.x
- Tags
- QueuesLaravel 13
A lot of queue problems aren't really bugs in the job. They're the same job running too often, in the wrong place, or twice at once. Laravel 13 gives you a clean tool for each of those. Here's how to decide which one to reach for.
Where should this job run? Queue::route
The classic way to send a job to a specific queue is onQueue() in the constructor or at dispatch time. It works, but the routing ends up scattered across dozens of files.
Laravel 13 adds Queue::route, so you can declare it once in a service provider:
use App\Concerns\RequiresVideo;
use App\Jobs\ProcessPodcast;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Support\Facades\Queue;
public function boot(): void
{
Queue::route(ProcessPodcast::class, connection: 'redis', queue: 'podcasts');
Queue::route(RequiresVideo::class, queue: 'video');
Queue::route(ShouldBroadcast::class, queue: 'events');
}The nice part is that the first argument can be an interface, trait or parent class, not just a job class. Every job that implements ShouldBroadcast lands on events, including ones written next year by someone who has never heard of this provider.
There's also an array form if you prefer a map:
Queue::route([
ProcessPodcast::class => ['redis', 'podcasts'], // connection and queue
ProcessVideo::class => 'videos', // queue only
]);A job can still override its routing for a specific dispatch.
Don't queue it if it's already queued: ShouldBeUnique
When a job is expensive and idempotent, like rebuilding a search index for a product, you don't want ten copies sitting in the queue.
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\Attributes\UniqueFor;
#[UniqueFor(3600)]
class UpdateSearchIndex implements ShouldQueue, ShouldBeUnique
{
public function __construct(public Product $product) {}
public function uniqueId(): string
{
return $this->product->id;
}
}On dispatch, Laravel tries to take a cache lock keyed by uniqueId(). If the lock is held, the new dispatch is ignored. The lock is released when the job finishes or runs out of retries; #[UniqueFor(3600)] makes sure a stuck job can't hold it for more than an hour.
Gotchas:
- You need a cache driver with atomic locks:
redis,memcached,dynamodb,database,fileorarray. - Unique constraints don't apply to jobs inside batches.
- If you want the lock released as soon as the job starts (so a new one can queue while the current one runs), implement
ShouldBeUniqueUntilProcessinginstead.
Run only the latest one: #[DebounceFor]
Uniqueness keeps the first dispatch and drops the rest. Sometimes you want the opposite: a user edits a product five times in ten seconds, and you only care about the final state.
That's debouncing:
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\Attributes\DebounceFor;
#[DebounceFor(30)]
class UpdateSearchIndex implements ShouldQueue
{
use Queueable;
public function __construct(public int $productId) {}
public function debounceId(): string
{
return (string) $this->productId;
}
}Dispatch it repeatedly for the same product within 30 seconds and only the latest dispatch runs. Superseded jobs are removed from the queue and a JobDebounced event fires.
If edits never stop, the job would never run, so cap the wait:
#[DebounceFor(30, maxWait: 120)]Gotchas:
- A debounced job must not also implement
ShouldBeUnique. They solve opposite problems. - With several servers or containers dispatching, they all need to share one central cache. Override the store with a
debounceVia()method if needed.
Never two at once: WithoutOverlapping
Unique and debounced jobs control what gets queued. WithoutOverlapping controls what runs at the same time. It's job middleware:
use Illuminate\Queue\Middleware\WithoutOverlapping;
public function middleware(): array
{
return [
(new WithoutOverlapping($this->order->id))
->releaseAfter(60)
->expireAfter(180),
];
}If another job with the same key is running, this one is released back onto the queue and retried 60 seconds later. expireAfter(180) frees the lock after three minutes in case a worker dies mid-job.
The gotcha that catches everyone: a release counts as an attempt. The default is one try, so without changing it an overlapping job is simply never retried. Pair it with #[Tries]:
#[Tries(10)]
#[MaxExceptions(3)]
class ChargeOrder implements ShouldQueueIf you'd rather throw overlapping jobs away, use ->dontRelease().
Cheat sheet
| You want… | Use |
|---|---|
| Jobs of a kind always on a given queue | Queue::route() |
| Ignore new dispatches while one is pending | ShouldBeUnique + #[UniqueFor] |
| Keep only the latest dispatch | #[DebounceFor] |
| Never process two at the same time | WithoutOverlapping middleware |
| Limit throughput (e.g. per user) | RateLimited middleware |
All of the above is documented in the official queues guide.