On this page

Queues are the part of a Laravel app most likely to break without anyone noticing. A worker stops, jobs pile up, and the website keeps loading as normal. But emails are not sent, exports are not made and payments are not confirmed. Often the first sign is a customer asking where their receipt is, days later.

Here is how I set up queues on a live server so this does not happen, or at least so somebody finds out quickly.

Keep the workers running

The most common mistake I find is php artisan queue:work started by hand in an SSH session. It works until the session closes or the server restarts. Then it stops, and nothing tells you.

Workers need to run under a process monitor. This is a small program that starts them when the server boots and restarts them if they crash. Most Linux servers already have one built in. The command it runs looks something like this:

terminal
php artisan queue:work --queue=high,default --tries=3 --max-time=3600

Two things here matter more than they look:

  • --max-time=3600 makes each worker exit after an hour, and the monitor starts a fresh one. A PHP process that runs for days slowly uses more and more memory, and this keeps it under control.
  • The process monitor's stop timeout should be longer than your longest job. If it is shorter, a deploy can kill a job halfway through.

Each worker is a full PHP process, so the number of workers you can run depends on how much memory the server has.

laravel.com/docs/queues
Laravel documentation, Supervisor Configuration section of the Queues page
Laravel's own documentation uses Supervisor as the process monitor that keeps queue workers running. Screenshot: Laravel documentation, Queues

Restart the workers on every deploy

queue:work loads your code once and keeps it in memory. That makes it fast, but it also means that after you deploy new code, the running workers are still using the old code. This causes some very confusing bugs, like old job code running against a new database structure.

The fix is one command at the end of every deploy, after the new code is live and the migrations have run:

terminal
php artisan queue:restart

It does not kill anything. It tells each worker to finish its current job and exit, and the process monitor starts new workers with the new code.

Set retries for each job

The default retry settings are rarely right. I set them on each job class, based on how that job is likely to fail:

php
class SyncCustomerToCrm implements ShouldQueue
{
    public int $tries = 5;
    public int $timeout = 120;
    public array $backoff = [10, 60, 300, 900];

    public function failed(\Throwable $e): void
    {
        // Runs after the last attempt: tell someone, clean up.
    }
}
  • $backoff waits longer before each new attempt. If an outside service is busy, retrying straight away only makes things worse.
  • $timeout must be lower than retry_after in your queue config. If it is not, a second worker can pick up the same job while the first one is still running it, and the job runs twice.
  • failed() runs after the final attempt. Without it, failures just sit in a table that nobody checks.

Make every job safe to run twice

Sooner or later, a job will run more than once. A worker gets killed halfway, a timeout fires just as the job finishes, or a retry happens after a request that had actually worked. If the job charges a card or sends an email, running it twice is a real problem for your customer.

So the job first checks whether the work is already done:

php
public function handle(): void
{
    $order = Order::find($this->orderId);

    if (! $order || $order->invoice_sent_at !== null) {
        return; // already done, or the order is gone
    }

    $this->mailer->sendInvoice($order);
    $order->update(['invoice_sent_at' => now()]);
}

Laravel also has ShouldBeUnique, which stops the same job being queued twice at the same time, and the WithoutOverlapping middleware for jobs that must never run in parallel with themselves.

Look at your failed jobs

Laravel saves jobs that have failed for good in the failed_jobs table. In many apps I look at, that table has months of entries and nobody has opened it.

Send failures to the same place where you already watch for errors:

php
Queue::failing(function (JobFailed $event) {
    report($event->exception);
});

You can list failed jobs with php artisan queue:failed and run them again with php artisan queue:retry. I also add a scheduled check that sends an alert when the number of failed jobs goes over a limit.

Watch the size of the queue

A worker can be running while the queue grows faster than it can keep up. So I watch two numbers: how many jobs are waiting, and how long the oldest one has been waiting. If a job from forty minutes ago has still not started, something is wrong right now.

I also give urgent work its own queue. With one queue for everything, a big export can make a password reset email wait for ten minutes. Use ->onQueue('high') for anything a user is waiting for, and let the workers handle high first.

My checklist

  • Workers run under a process monitor that restarts them.
  • --max-time is set, so workers restart regularly.
  • queue:restart runs on every deploy, after the migrations.
  • Each job has its own tries, backoff and timeout, and the timeout is lower than retry_after.
  • Every job is safe to run twice.
  • Failed jobs send an alert.
  • The number of waiting jobs and the waiting time are monitored.
  • Urgent jobs have their own queue.

None of this is hard. It gets skipped because on a developer's laptop the queue runs instantly and these problems never show up. If you want help setting this up on your own server, have a look at Linux server management.

Sources

  1. Queues, Laravel documentation laravel.com
Irfan Bhat

Written by

Irfan Bhat

Web Developer & Server Engineer · Baramulla, Kashmir

Web developer and server engineer in Baramulla, Kashmir. I look after servers and websites, and I build practical AI tools for businesses.