Queues & Jobs — Laravel

A queue hands slow work to another process. One task on the queue is called a job, for example SendDueReminder, which emails a reminder. Make a job with php…

In plain PHP (with no framework), sending an email means calling mail() in the middle of the script. The visitor waits until the mail server has answered. If the mail server is down, the visitor's page shows an error too.

Laravel puts slow work like sending email on a queue: a list of tasks that another process does later. A job is one task on that list. Think of the kitchen of a meatball soup stall. The waiter writes your order on a ticket, clips it to the ticket rail, and right away says "got it, it'll be ready soon". The cook takes the tickets one by one from the end of the rail. The waiter doesn't cook, and you don't have to stand by the stove. In Laravel, the controller is the waiter, each ticket is one job, the jobs table is the ticket rail, and the worker (a separate process that takes jobs and runs them) is the cook.

Why a queue

Every morning, the Library app emails a reminder to members whose book is due tomorrow: Budi, Sari, and Andi. One email takes about 800 ms, because the app has to talk to a mail server. If all three are sent inside the request, the admin who presses the send button waits 2.4 seconds. If the mail server is down, the request fails.

With a queue, the request only writes 3 rows to the jobs table (about 12 ms), and then it is done. The emails are sent by the worker, outside that request. If sending fails, the worker tries again. The general idea is covered in the Message Queue lesson (System Design track). Here is the Laravel way.

Your first job

Make the job with Artisan:

php artisan make:job SendDueReminder

This is the job file:

<?php
// app/Jobs/SendDueReminder.php
namespace App\Jobs;

use App\Mail\DueReminder;
use App\Models\Loan;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Mail;

class SendDueReminder implements ShouldQueue
{
    use Queueable;

    public function __construct(public Loan $loan) {}

    public function handle(): void
    {
        Mail::to($this->loan->user)->send(new DueReminder($this->loan));
    }
}

Then put the job on the queue with dispatch:

// In a controller or a command: put the job on the queue
SendDueReminder::dispatch($loan);

// Or only let it be picked up 5 minutes from now
SendDueReminder::dispatch($loan)->delay(now()->addMinutes(5));

The worker: php artisan queue:work

Jobs in the jobs table don't run by themselves. A worker has to be running. This is a worker doing the three reminder jobs (Budi, Sari, Andi):

$ php artisan queue:work

   INFO  Processing jobs from the [default] queue.

  2026-10-09 09:00:00 App\Jobs\SendDueReminder ....................... RUNNING
  2026-10-09 09:00:00 App\Jobs\SendDueReminder ................. 812.40ms DONE
  2026-10-09 09:00:00 App\Jobs\SendDueReminder ....................... RUNNING
  2026-10-09 09:00:01 App\Jobs\SendDueReminder ................. 790.15ms DONE
  2026-10-09 09:00:01 App\Jobs\SendDueReminder ....................... RUNNING
  2026-10-09 09:00:02 App\Jobs\SendDueReminder ................. 805.62ms DONE

When a job fails: $tries, backoff, failed_jobs

A job that throws an exception isn't lost. The worker puts it back on the queue to be tried again, as many times as you allow in the job class:

class SendDueReminder implements ShouldQueue
{
    use Queueable;

    public int $tries = 3;           // at most 3 attempts
    public array $backoff = [10, 60]; // wait before attempts 2 and 3, in seconds

    public function __construct(public Loan $loan) {}

    public function handle(): void { /* send the email, as before */ }

    public function failed(?Throwable $exception): void
    {
        Log::warning("Reminder for loan #{$this->loan->id} failed: {$exception?->getMessage()}");
    }
}

failed() is called once, when the job gives up. Here, failed() writes the message of $exception to the log.

Say the mail server is down, and this job is first tried at 09:00:00:

Attempt Time Result Then
1 09:00:00 FAIL back on the queue, wait 10 seconds
2 09:00:10 FAIL back on the queue, wait 60 seconds
3 09:01:10 FAIL $tries used up: failed() is called, the job moves to failed_jobs

So the job gives up at 09:01:10, after three failures.

A job must be safe to repeat

One job can run more than once. The job may be retried after failing halfway. Or the worker may die after handle() has finished but before the job is deleted, for example because of a server restart or a deploy (putting new code on the server). That job is still in the jobs table, and it is picked up again once retry_after has passed: 90 seconds in config/queue.php. If the job isn't ready for this, Budi gets the reminder email twice.

The solution is an idempotent job: a job that gives the same result whether it runs once or twice. To do that, check first whether the work was already done:

public function handle(): void
{
    if ($this->loan->reminded_at !== null) {
        return; // already sent, don't send again
    }

    Mail::to($this->loan->user)->send(new DueReminder($this->loan));
    $this->loan->update(['reminded_at' => now()]);
}

The reminded_at column records when the reminder was sent.

For work that costs more when it repeats, a payment for example, use a unique key for each operation. The pattern is in the Idempotency & Retry Safety lesson (System Design track).

⚠️ Why jobs collect, run stale code, or keep users waiting

TL;DR: A queue hands slow work to another process. One task on the queue is called a job, for example SendDueReminder, which emails a reminder. Make a job with php artisan make:job SendDueReminder: the class is marked implements ShouldQueue with use Queueable, and the work goes in handle(). Put the job on the queue with SendDueReminder::dispatch($loan). QUEUE_CONNECTION=database stores it in the jobs table, and a worker (a separate process, started with php artisan queue:work) runs the jobs one by one and prints RUNNING, then DONE or FAIL. $tries limits the number of attempts, and $backoff sets a wait between them. A job that keeps failing goes into the failed_jobs table, and its failed() method is called. A job can run twice, so make your job idempotent (the same result even if it runs twice): check first whether the work was already done.

⚡ Your first job

Write:

class SendDueReminder implements ShouldQueue
{
    use Queueable;

    public function __construct(public Loan $loan) {}

    public function handle(): void
    {
        Mail::to($this->loan->user)->send(new DueReminder($this->loan));
    }
}

Meaning: A job is one task that waits on a queue and is done later by another process. This job emails a due-date reminder for one loan ($loan). php artisan make:job SendDueReminder creates the class. implements ShouldQueue means this job goes on the queue instead of running right away. use Queueable gives the class the dispatch() method, which puts the job on the queue. The work goes in handle(), and the worker (a separate process that runs the queue's jobs) calls it, not you.

⚡ On the queue, a worker does it

Write:

php artisan queue:work
Guess first: SendDueReminder::dispatch($loan) puts the job on the queue, here the jobs table. dispatch was called, but no worker is running. Does the email go out?

No. dispatch() only writes one row to the jobs table. Without a worker, the jobs collect there.

Meaning: queue:work starts a worker. The worker takes jobs one by one and prints RUNNING, then DONE (it worked) or FAIL (it failed). After a deploy (putting new code on the server), run php artisan queue:restart so the worker loads the new code.

⚡ When it fails, it's tried again

Meaning: A job that fails (throws an exception) goes back on the queue and is tried again. public int $tries = 3; in the job class limits the attempts: at most 3. $backoff sets a wait between attempts. If it keeps failing, the job moves to the failed_jobs table, and that job's failed() method is called. A job can also run twice, for example when the worker dies before the job is deleted. So check first whether the work was already done.

In the exercise, handle() returns text, so print it with echo $job->handle();, and a failed() method has to be there too.

What you will learn