ENGINEERING JOURNAL

Laravel Queues with Priorities: Keeping Slow Jobs from Blocking Fast Ones

Slow jobs should never block fast ones. Learn how Laravel queue priorities with multiple queues keep password resets and notifications moving.

THE ARTICLE 01 — 02

If every job in your Laravel app goes into one queue, slow work blocks fast work. A PDF export that takes two minutes sits in front of a password reset email, and the user who asked for the reset waits two minutes for a mail that should take seconds. That is the whole problem Laravel queue priorities solve: separate the work by urgency, then give the urgent queue first claim on workers.

One queue, one line

By default, every dispatched job lands in the default queue. Workers pull jobs in the order they arrived. That is first in, first out, and it treats a two-minute report the same as a one-second email. The order a job was created in is not the order that matters to your users. What matters is how long they wait for the result.

You notice this first on a busy day. A bulk export or a batch of webhook deliveries fills the queue, and suddenly password resets, verification emails, and notifications all arrive late. Nothing has failed. The queue is working exactly as configured. The configuration is the problem.

Split the work by urgency

The fix is boring on purpose: use more than one queue. Three queues cover most apps well.

  • high: password resets, verification emails, anything a user is waiting on right now.
  • default: ordinary background work such as notifications and cache warming.
  • low: reports, exports, bulk imports, and anything that can wait minutes without anyone noticing.

Queues are just names. There is nothing to configure in config/queue.php to create them. Redis or your database driver will hold a separate list per name, and Laravel creates each one on first use. Keep the list short. Ten queues with unclear boundaries are worse than three with obvious ones.

Dispatch to the right queue

Every job class can name its queue. The cleanest way is a $queue property on the job, so the choice lives with the job instead of at every call site.

class GenerateMonthlyReport implements ShouldQueue
{
    public string $queue = 'low';

    public function handle(): void
    {
        // Build the report. This can take minutes.
    }
}

For a one-off dispatch, chain onQueue() instead:

SendPasswordResetEmail::dispatch($user)->onQueue('high');

Pick the queue when you write the job, not when you notice the delay. If a job touches something a user is staring at, it belongs in high. If nobody is waiting, it belongs in low. When in doubt, put it lower. A slow job in the high queue hurts everyone behind it.

Order matters: how workers pick jobs

A worker started with a list of queues checks them in order and takes the first job it finds:

php artisan queue:work redis --queue=high,default,low

That order is the priority. The worker looks at high first, then default, then low. A low job only runs when both higher queues are empty. This is cheap to set up and it fixes the common case, but understand its limit: once a worker starts a low job, it finishes that job before checking the high queue again. A two-minute low job still delays a high job that arrives during those two minutes.

That limit is why timeouts matter. Give long jobs a generous $timeout so they can finish, but keep the worker timeout sane with --timeout so a stuck job is killed instead of holding a worker forever. Priority is only as good as the longest job a worker can be stuck on.

When one worker is not enough

Queue order helps, but true isolation needs separate workers. Run one worker process per queue, and a slow low job can never occupy the worker that handles password resets. On Ubuntu, a supervisor config makes this concrete:

[program:laravel-high]
command=php /var/www/app/artisan queue:work redis --queue=high --sleep=3 --tries=3
autostart=true
autorestart=true
numprocs=2

[program:laravel-low]
command=php /var/www/app/artisan queue:work redis --queue=low --sleep=3 --tries=3 --timeout=300
autostart=true
autorestart=true
numprocs=1

Notice the low worker gets a longer timeout and a single process. The high worker gets two processes and a short timeout, because a stuck high job should fail fast and retry instead of blocking the queue. If you use Horizon, the same idea applies with one supervisor per queue, each with its own maxProcesses and balance setting. The tool changes; the principle does not.

Watch the low queue after you split. If it grows without bound, you have not fixed a bottleneck, you have hidden one. Check queue lengths with php artisan queue:monitor or in Horizon, and add a low worker before the backlog turns into a Monday-morning incident.

Mistakes that undo the priority

Three mistakes show up again and again. First, dispatching everything to default out of habit and wondering why the priority setup does nothing. Second, naming queues inconsistently, so High and high become two different queues and jobs sit in one nobody watches. Queue names are case-sensitive. Third, running a single worker with --queue=low,default,high and reversing the order by accident. The first queue in the list wins.

Retries deserve the same care as the first attempt. A job that fails and retries belongs on the same queue it started on, and making the job idempotent matters even more once retries can land on a busy queue and run twice. Priority changes when work runs. It does not change whether work is safe to run twice.

The takeaway

Queue priorities are a small change with an outsized effect on perceived speed. Split jobs into high, default, and low queues, dispatch each job to the queue that matches how long a user can wait, and give workers the queue order or the separate processes to respect it. The password reset should never wait behind a report, and once the queues are split, it will not.

Muhammad Aqib ← ALL ARTICLES
KEEP EXPLORING

More ideas.
Better engineering.

Explore the journal ↗