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));
make:jobcreatesapp/Jobs/SendDueReminder.php.implements ShouldQueue: this job goes on the queue instead of running right away, like aShouldQueuelistener in the previous lesson.use Queueablegives the class the methodsdispatch(),delay(), andonQueue()(which picks a queue other thandefault). A model that comes in through the constructor (here$loan) is stored on the queue as its id only. When the job runs, that model is fetched again from the database.handle()holds the work. The worker calls it, not you.SendDueReminder::dispatch($loan)writes one row to the queue, and$loangoes to the job's constructor. Where the queue lives is set byQUEUE_CONNECTIONin.env. A new project usesdatabase: the queue is thejobstable, and the migration for that table is already there. The valuesyncruns jobs right away, with no queue.
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
- For each job, the worker prints two lines:
RUNNINGwhen the job is picked up, then how long it took andDONE(it worked) orFAIL(it failed). - Jobs are taken in order: the one that has waited longest comes first. A
DONEjob is deleted from thejobstable. - The worker keeps running and waits for new jobs until you press
Ctrl+C. There is alsophp artisan queue:listen, which loads your code again for every job, so code you just changed is used right away. It is slower, but handy during development. - On a server, a process manager (a program that starts the worker again if it dies) keeps the worker running. The Systemd & Service Management lesson (DevOps track) shows how.
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.
- Without
$tries, a job is tried only once, unless the worker runs with--tries=3. Without a backoff, the next attempt starts right away. - Laravel 13 also accepts the attributes
#[Tries(3)]and#[Backoff([10, 60])], written above the class. They mean the same as$triesand$backoff. php artisan queue:failedlists what is infailed_jobs. Once the mail server works again,php artisan queue:retry allputs all those jobs back on the queue.
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
- No worker running.
dispatch()works, but the jobs just collect in thejobstable, and no email goes out. - New code, old worker. A worker loads your code into memory once, when it starts. After a deploy, run
php artisan queue:restart. The worker stops after its current job, then the process manager starts it again with the new code. - Forgetting
implements ShouldQueue, orQUEUE_CONNECTION=sync. The job runs right away, inside the request, and the user still waits. - In the exercise, nothing is printed.
handle()in the exercise returns text, so print it:echo $job->handle();. Afailed()method has to be there too.
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.