RSS Amplifier

Roman Zipp · Sep 9, 2025

Unique Job Processing in Laravel

0
Sign in to vote or save

Roman Zipp · Roman Zipp

ShouldBeUnique Interface

Goal: Prevent multiple jobs from being dispatched at the same time.

Behavior: A job will not be dispatched if another instance of the job is already on the queue and has not finished processing. Jobs are "unlocked" after a job completes processing or fails all of its retry attempts.

On failure: ...

Unique Id: You can define a uniqueId method on the job class to return a certain unique Id.

Timeout: You can define a uniqueFor property after which the job's unique lock will be released

See Laravel Docs: Unique Jobs

ShouldBeUniqueUntilProcessing Interface

Goal: Prevent multiple jobs from being dispatched at the same time.

Behavior: Unlock the job when it starts processing.

On failure: ...

Unique Id: You can define a uniqueId method on the job class to return a certain unique Id.

Timeout: You can define a uniqueFor property after which the job's unique lock will be released

See Laravel Docs: Unique Jobs

WithoutOverlapping Middleware

/**
 * Get the middleware the job should pass through.
 *
 * @return array<int, object>
 */
public function middleware(): array
{
    return [(new WithoutOverlapping($this->order->id))->releaseAfter(60)];
}

Goal: Prevent jobs from running in parallel.

Behavior: Any overlapping jobs of the same type will be released back to the queue

On failure: ...

Unique Id: Provide the WithoutOverlapping with a contructor string argument to define a unique id: new WithoutOverlapping('some-job')

Timeout: You may also specify the number of seconds that must elapse before the released job will be attempted again by calling ->releaseAfter() . Jobs may unexpectedly fail, in this case the lock won't be released. Call ->expireAfter() to let the lock automatically expire.

See Laravel Docs: Preventing Job Overlaps

Read the original on romanzipp.com

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.