Controlling and Limiting Jobs

How to keep your jobs under control

4 min readJun 18, 2022


When two or more processes try to make changes on the same element, this is called a race condition and in Laravel while dealing with Jobs we want to find ways to avoid this.

As you have seen how complicated some workflows may get, we don’t always have an easy solution for this.

A common case would be having a job to modify the data for a user and having two of these jobs running at the same time can cause problems.

To prevent this race condition, we can create a lock on our job.

The block method accepts two arguments, the number of seconds to block and a closure that should be the code to run if it acquired a lock successfully.

Now, because we have a lock, jobs can be dispatched concurrently but they will run only after the other is finished because they have to acquire a lock to run.

We can also use the Redis concurrency limiter.

This gives us more flexibility as we can use the limit method to specify how many instances of the same job we want running at the same time.

We can also use the block method to wait for a couple of seconds until a lock has been acquired.

Our code should be inside the then method:

Using the redis throttle method we can acquire in a given period of time with the help of allow and every method.

What we did was allow 10 instances of the job to run every 60 seconds.

One more way we can make sure jobs do not overlap is by using a middleware, just like we do with Routes.

Inside the middleware method we define an array that represents all the middlewares we want to…




I convert real world problem into technical solutions. I do research and make robots.