agyanadda@gmail.com

Laravel Queues and Jobs

Advanced Laravel 1 min read Updated Dec 2, 2026 Topic 26 of 31

Some tasks are slow: sending email, resizing images, creating PDFs, calling other APIs. A queue runs them in the background so the user gets a fast response.

How it works

  1. Your code dispatches a job — it is saved to the queue
  2. A worker process (php artisan queue:work) picks it up and runs it

Choose a queue driver

Set QUEUE_CONNECTION in .env:

  • sync — runs immediately (no background), useful for debugging
  • database — stores jobs in the jobs table; simple and works on most hosting
  • redis — faster, for busy sites

Create and dispatch a job

  • php artisan make:job SendWelcomeEmail — the class implements ShouldQueue, and the work goes in handle()
  • SendWelcomeEmail::dispatch($user);
  • ->delay(now()->addMinutes(10)) — run later

Retries and failures

Set public $tries = 3; on the job. Jobs that still fail go to the failed_jobs table. php artisan queue:failed lists them and php artisan queue:retry all runs them again.

On the live server

The worker must keep running. Use Supervisor on a VPS. After deploying new code, run php artisan queue:restart so workers load it.

Example

Example — Send the welcome email in the background PHP
<?php
// app/Jobs/SendWelcomeEmail.php
namespace App\Jobs;

use App\Mail\WelcomeMail;
use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Mail;

class SendWelcomeEmail implements ShouldQueue
{
    use Queueable;

    public $tries = 3;

    public function __construct(public User $user) {}

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

// After registration (controller):
SendWelcomeEmail::dispatch($user);

// Terminal: start a worker
// php artisan queue:work

Registration finishes instantly. The worker sends the email a moment later and retries up to 3 times if the mail server fails.

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which command starts a queue worker?

Exercise 2 True / False

With QUEUE_CONNECTION=sync, jobs run in the background.

Exercise 3 Fill in the Blank

A job class must implement the ___ interface to be queued.

Frequently Asked Questions

Yes, with the database driver. If you cannot keep a worker running, a cron job can run php artisan queue:work --stop-when-empty every minute. A VPS with Supervisor is more reliable.