• Fri, Mar 2026

Laravel Queues and Jobs: Handle Background Tasks Like a Pro

Laravel Queues and Jobs: Handle Background Tasks Like a Pro

This comprehensive 4000+ word tutorial takes you deep into Laravel’s Queue and Job system. You’ll learn how to set up queues, dispatch jobs, choose drivers, and monitor tasks like a professional. With step-by-step instructions, real code snippets, and visuals, this article reads like a training script for developers and educators.

Introduction

When building modern applications, performance is critical. Users expect lightning-fast responses, but sometimes your app has to perform time-consuming tasks like sending emails, processing images, or generating reports. If these tasks run during the web request, your users will experience delays.

Laravel solves this with its powerful Queues and Jobs system. This allows you to offload long-running tasks into the background, keeping your application fast and responsive.

[Insert visual diagram of how a request flows through controllers, jobs, and queues]

What Are Queues in Laravel?

A queue is like a waiting line for jobs. When a job is dispatched, it is placed in the queue, and a background worker processes it later. This means your web response can be returned immediately, while the job executes in the background.

Why Use Queues?

  • Improve user experience by avoiding delays.
  • Handle heavy tasks like email sending, notifications, or video processing.
  • Scale background tasks separately from your main app.

Step 1: Setting Up Laravel Queues

Laravel supports multiple queue backends like Database, Redis, Beanstalkd, Amazon SQS, and more. Let’s start with the simplest: the database driver.

Configuring Queue Driver


// In .env file
QUEUE_CONNECTION=database
    

This tells Laravel to use the database queue driver.

Creating the Database Table


php artisan queue:table
php artisan migrate
    

This creates a table named jobs where queued jobs are stored.

Step 2: Creating Jobs

A job represents the work to be done. For example, sending a welcome email.

Generate a Job Class


php artisan make:job SendWelcomeEmail
    

This creates app/Jobs/SendWelcomeEmail.php.

Defining the Job Logic


// app/Jobs/SendWelcomeEmail.php

namespace App\Jobs;

use App\Models\User;
use App\Mail\WelcomeMail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;

class SendWelcomeEmail implements ShouldQueue
{
    use Queueable, InteractsWithQueue, SerializesModels;

    public $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

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

Here, we define what happens when this job runs: send a welcome email to the user.

Step 3: Dispatching Jobs

You can dispatch a job from controllers, services, or even tinker.


// In UserController.php
use App\Jobs\SendWelcomeEmail;

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

    // Dispatch job to the queue
    SendWelcomeEmail::dispatch($user);

    return response()->json(['message' => 'User created successfully!']);
}
    

Now, the job is stored in the jobs table and will run later.

Step 4: Running the Queue Worker

A queue worker listens for jobs and executes them in the background.


php artisan queue:work
    

Keep this running in the background (using tools like Supervisor in production).

Common Queue Drivers

Laravel makes it easy to switch between drivers.

DriverUse CasePerformance
DatabaseSimple setups, easy debuggingSlower
RedisHigh-performance appsVery fast
Amazon SQSCloud apps with AWSScalable
BeanstalkdDedicated queueing systemEfficient

Step 5: Job Retries and Failures

Sometimes jobs fail. Laravel lets you retry or log failures.

Retrying Failed Jobs


php artisan queue:retry all
    

Failed Jobs Table


php artisan queue:failed-table
php artisan migrate
    

This adds a failed_jobs table to track failed jobs.

Step 6: Delayed Jobs

Sometimes you want a job to run after a delay.


// Delay by 10 minutes
SendWelcomeEmail::dispatch($user)->delay(now()->addMinutes(10));
    

Step 7: Queues with Multiple Workers

You can set up multiple workers to process jobs faster.


php artisan queue:work --queue=high,default
    

This prioritizes jobs in the high queue before the default queue.

Step 8: Monitoring with Horizon

Laravel Horizon is a powerful dashboard for managing Redis queues.


composer require laravel/horizon
php artisan horizon:install
php artisan migrate
php artisan horizon
    

Best Practices for Queues and Jobs

  • Keep jobs small and focused.
  • Use retry mechanisms for reliability.
  • Monitor queues in production with Horizon.
  • Secure sensitive data passed into jobs.

Conclusion

Laravel queues and jobs provide a professional way to handle background tasks. They improve performance, scalability, and user experience. Whether you’re sending emails, processing files, or syncing data, Laravel makes it seamless with its queue system.

By mastering queues and jobs, you’ll be able to scale your Laravel applications and deliver a smoother experience to your users.

This website uses cookies to enhance your browsing experience. By continuing to use this site, you consent to the use of cookies. Please review our Privacy Policy for more information on how we handle your data. Cookie Policy