Build With Abdallah logo Build With Abdallah Software · AI · Automation
Tutorial 3 min read Jun 10, 2026

Implementing Laravel 13's New Queue System for Scalable Background Jobs

In this tutorial, we will explore how to implement Laravel 13's new queue system to manage scalable background jobs effectively. By the end of this guide, you'll have a clear under

A
Abdallah Mohamed
Senior Full-Stack Engineer
Implementing Laravel 13's New Queue System for Scalable Background Jobs

Implementing Laravel 13's New Queue System for Scalable Background Jobs

In this tutorial, we will explore how to implement Laravel 13's new queue system to manage scalable background jobs effectively. By the end of this guide, you'll have a clear understanding of how to set up and use Laravel's queue system to handle tasks asynchronously, improving the performance and scalability of your applications.

What You'll Build

We'll create a simple application that processes user-uploaded files in the background. The application will use Laravel's queue system to handle file processing tasks, allowing the main application to remain responsive to user interactions. Here's what the final outcome will look like:

  1. Users upload files through a web interface.
  2. The application offloads file processing to a queue.
  3. A worker processes the files in the background.
  4. Users receive a notification once their files are processed.

Why This Matters

Handling tasks asynchronously is crucial for maintaining the responsiveness and scalability of modern web applications. When applications perform time-consuming operations, such as file processing or sending emails, during HTTP requests, it can lead to slow response times and a poor user experience.

When to Use Laravel's Queue System:

  • When you have tasks that are not time-sensitive and can be processed later.
  • If you want to improve the performance of your application by offloading heavy tasks.
  • When you need to scale your application to handle a large number of background jobs.

Who Benefits:

  • Developers who want to optimize application performance.
  • Businesses that need to handle high volumes of background tasks efficiently.
  • Users who expect fast and responsive applications.

Architecture Overview

Here's a simple architecture diagram to illustrate how Laravel's queue system works:

[User] --(upload file)--> [Web Server] --(dispatch job)--> [Queue] --> [Worker] --(process file)--> [Notification]
  • Web Server: Accepts user requests and dispatches jobs to the queue.
  • Queue: Holds jobs until workers are ready to process them.
  • Worker: Processes jobs from the queue and performs the necessary operations.
  • Notification: Informs users when their files have been processed.

Step-by-Step Implementation

Let's dive into the implementation. We'll start by setting up a new Laravel project and configuring the queue system.

1. Setting Up a New Laravel Project

First, you need to create a new Laravel project. Make sure you have Composer installed on your system.

composer create-project --prefer-dist laravel/laravel laravel-queue-example

Navigate to the project directory:

cd laravel-queue-example

This command sets up a new Laravel project named laravel-queue-example.

2. Configuring the Queue Driver

Laravel supports multiple queue drivers like database, Redis, and Beanstalkd. For this tutorial, we'll use the database driver. First, update your .env file to set the queue connection:

QUEUE_CONNECTION=database

Next, run the following command to create the necessary database tables for the queue:

php artisan queue:table
php artisan migrate

These commands create a table named jobs in your database to store queued jobs.

3. Creating a Job for File Processing

Now, let's create a job that will handle file processing. Use the following Artisan command:

php artisan make:job ProcessUploadedFile

This command generates a new job class located at app/Jobs/ProcessUploadedFile.php. Open this file and add the logic to process the uploaded file:

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;

class ProcessUploadedFile implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $filePath;

    /**
     * Create a new job instance.
     *
     * @param string $filePath
     * @return void
     */
    public function __construct($filePath)
    {
        $this->filePath = $filePath;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // Process the file (e.g., resize an image, convert a document, etc.)
        // This is a placeholder for actual file processing logic
        $fileContents = Storage::get($this->filePath);
        // Perform operations on $fileContents

        // Once processing is done, you might want to store the result
        Storage::put('processed/' . basename($this->filePath), $fileContents);
    }
}

In this job, we're simulating file processing by reading the file from storage and saving it to a processed directory. You can replace this logic with actual file processing as per your application's requirements.

This concludes the first half of our tutorial. In the next steps, we will cover creating a controller to handle file uploads, dispatching the job to the queue, and setting up a worker to process the queue.

4. Creating a Controller for File Uploads

Next, we'll create a controller to handle file uploads and dispatch the job to the queue. Run the following Artisan command to generate a new controller:

php artisan make:controller FileUploadController

Open the newly created app/Http/Controllers/FileUploadController.php and add the following code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Jobs\ProcessUploadedFile;

class FileUploadController extends Controller
{
    /**
     * Show the form for uploading files.
     *
     * @return \Illuminate\Http\Response
     */
    public function showUploadForm()
    {
        return view('upload');
    }

    /**
     * Handle the file upload and dispatch the job.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function uploadFile(Request $request)
    {
        $request->validate([
            'file' => 'required|file|max:10240', // max 10MB
        ]);

        $filePath = $request->file('file')->store('uploads');

        // Dispatch the job
        ProcessUploadedFile::dispatch($filePath);

        return back()->with('success', 'File uploaded successfully and is being processed.');
    }
}

Here, we define two methods: one for displaying the upload form and another for handling the file upload and dispatching the job.

5. Setting Up Routes and Views

Create a simple view for the file upload form. In resources/views/upload.blade.php, add the following HTML:

<!DOCTYPE html>
<html>
<head>
    <title>Upload File</title>
</head>
<body>
    <h1>Upload a File</h1>
    @if(session('success'))
        <p style="color: green;">{{ session('success') }}</p>
    @endif
    <form action="{{ route('file.upload') }}" method="POST" enctype="multipart/form-data">
        @csrf
        <input type="file" name="file" required>
        <button type="submit">Upload</button>
    </form>
</body>
</html>

Now, define the routes in routes/web.php:

use App\Http\Controllers\FileUploadController;

Route::get('/upload', [FileUploadController::class, 'showUploadForm']);
Route::post('/upload', [FileUploadController::class, 'uploadFile'])->name('file.upload');

6. Running the Queue Worker

To process the queued jobs, you need to start a queue worker. Run the following command in your terminal:

php artisan queue:work

This command starts a worker that listens for jobs on the queue and processes them as they arrive.

Common Mistakes

  • Queue Worker Not Running: Ensure that your queue worker is running. Without it, jobs will remain in the queue unprocessed.
  • Environment Configuration: Double-check your .env settings, especially the QUEUE_CONNECTION and database settings.
  • File Permissions: Ensure your storage directories have the correct permissions, so files can be written and read.
  • Job Failures: If a job fails, check the failed_jobs table for error details. Use php artisan queue:failed-table to create this table if needed.

How I Would Use This

I would use Laravel's queue system for tasks that are not time-sensitive and can be processed asynchronously, such as sending emails, processing images, or generating reports. I would avoid using it for tasks that require immediate feedback to the user. In production, consider using a robust queue backend like Redis for better performance. Monitor your queue workers and job failures to ensure smooth operation. Be mindful of the costs associated with the infrastructure needed to support your queue system.

Lessons Learned

  • Choosing the Right Queue Driver: The choice of queue driver can significantly affect performance. Database queues are easy to set up but can become a bottleneck under heavy load.
  • Error Handling: Implement retry logic and failure handling to ensure jobs are not lost and can be retried if they fail.
  • Scalability: Queues help in scaling applications by offloading heavy tasks, but they also require careful monitoring and management.

Next Steps

  • Explore Advanced Queues: Learn about using Redis or Amazon SQS as queue backends for better scalability.
  • Monitor Queues: Implement monitoring and alerting for your queue system using Laravel Horizon or similar tools.
  • Job Prioritization: Learn how to prioritize jobs and handle different types of queues within the same application.

Sources

By following these steps, you've set up a scalable background job processing system using Laravel's queue system. This setup helps in maintaining a responsive application while offloading heavy tasks to the background efficiently.