Imagine a user signing up on your website. Your application needs to validate their input, save them to MySQL, generate an invoice PDF, and send a welcome email via an external mail provider.

If you perform all these tasks sequentially during the HTTP request, the user stares at a loading spinner for 3 to 5 seconds. If the mail server times out, the registration crashes. Laravel Queues solve this problem by executing time-consuming work in the background.

How Queues Work in Plain English

Think of a queue like a restaurant ticket system. When an order arrives, the cashier writes a ticket and places it on the kitchen rail. The cashier immediately hands the customer their receipt and serves the next person. In the kitchen, a background cook (the queue worker) picks up the ticket and prepares the meal.

Step 1: Creating Your First Job

In Laravel, background tasks are packaged into classes called Jobs. Create one with Artisan:

php artisan make:job SendWelcomeEmailJob

Step 2: Defining the Job Logic

Inside app/Jobs/SendWelcomeEmailJob.php, write the code that should run in the background:

namespace App\Jobs;

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

class SendWelcomeEmailJob implements ShouldQueue
{
    public function __construct(public User $user) {}

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

Step 3: Dispatching the Job in Your Controller

Instead of sending the email directly, dispatch the job in your controller:

public function register(Request $request)
{
    $user = User::create($request->validated());

    // Dispatches to background queue instantly
    SendWelcomeEmailJob::dispatch($user);

    return redirect()->route('dashboard')->with('success', 'Welcome aboard!');
}

Step 4: Running the Queue Worker

To process jobs stored in your database or Redis queue, start the worker process in your terminal:

php artisan queue:work

Best Practices for Production

  • Keep job parameters small: Pass Eloquent model IDs or lightweight models instead of heavy data collections.
  • Configure retries: Set public $tries = 3; on jobs that depend on third-party APIs so transient network hiccups retry automatically.
  • Use database driver for local dev, Redis for production: Database queue is great for testing on your machine, while Redis handles high volume easily in production.