Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

colinlongworth's avatar

WithoutOverlapping & ShouldBeUnique with dontRelease()

I'm considering using the WithoutOverlapping middleware in my job class but I want to be sure I have understood how it works.

In this configuration, multiple jobs can be pushed to the queue, but only one can process at a time e.g. They will not run in parallel.

class MyJob implements ShouldQueue, ShouldBeUnique
{

public function middleware()
{
        return [(new WithoutOverlapping($this->user->id)];
}

}

But in this configuration, only one job will ever exist on the queue as 'dontRelease' will mean any job pushed to the queue, with the same job already on it, will be immediately deleted.

class MyJob implements ShouldQueue, ShouldBeUnique
{

public function middleware()
{
        return [(new WithoutOverlapping($this->user->id)->dontRelease()];
}

}

So my question is, what is the difference between using ShouldBeUnique and dontRelease()? Are they not one and the same? i.e. Can I remove ShouldBeUnique as dontRelease() does the same thing?

0 likes
1 reply
LaryAI's avatar
Level 58

The ShouldBeUnique interface and the WithoutOverlapping middleware with the dontRelease() method serve similar purposes, but they are not exactly the same. Here's the difference:

  • ShouldBeUnique is an interface that, when implemented by a job, ensures that only one instance of that job, with a unique set of properties, is allowed in the queue at any given time. If another job with the same unique properties is pushed to the queue, it will not be queued if an existing job with those properties is already present.

  • WithoutOverlapping middleware prevents jobs from overlapping, meaning it ensures that a job with the same key (in your case, $this->user->id) does not run simultaneously. However, without dontRelease(), once the job is finished, another job with the same key can start.

  • Adding dontRelease() to WithoutOverlapping middleware means that if a job is attempted while another job with the same key is running, the new job will be immediately deleted and never re-attempted. It's a way to prevent the queue from even trying to run a new job if one is already in progress.

So, to answer your question, ShouldBeUnique and dontRelease() are not exactly the same. ShouldBeUnique prevents duplicate jobs from being queued, while WithoutOverlapping with dontRelease() prevents duplicate jobs from being executed and also deletes any new attempts if a job is currently running.

You could potentially remove ShouldBeUnique if you are using WithoutOverlapping with dontRelease(), but this would change the behavior slightly. Without ShouldBeUnique, if no job is running, a new job can be queued even if there are already jobs with the same key waiting in the queue. With ShouldBeUnique, those jobs would not be queued in the first place.

Here's a revised version of your job class with comments explaining the middleware:

class MyJob implements ShouldQueue
{
    public function middleware()
    {
        // WithoutOverlapping prevents jobs with the same key from running at the same time.
        // dontRelease() will delete any new job attempts if a job with the same key is currently running.
        return [(new WithoutOverlapping($this->user->id))->dontRelease()];
    }
}

In this configuration, you don't necessarily need ShouldBeUnique unless you want to prevent the same job from being queued multiple times, regardless of whether a job is currently running or not.

Please or to participate in this conversation.