Mahamudul Hasan Rubel
HomeBlogCoursesAboutProjectsSkillsExperiencePhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • Blog
  • Courses
  • About
  • Projects
  • Skills
  • Experience
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 14 of the Intermediate Laravel: Real-World Application Patterns course
LaravelJune 26, 20264 min read

Asynchronous Processing with Queues in Laravel

Improve application performance by offloading heavy tasks to queues. Learn how to configure drivers, create job classes, and dispatch background jobs.

LaravelQueuesPerformanceBackground JobsPHPbackend

Previously in this course, we explored Introduction to Laravel Events and Listeners to decouple our business logic. While events help organize code, they are synchronous by default—the listener runs immediately during the request. Today, we add asynchronous processing to our arsenal, allowing us to move heavy operations into the background to keep our API responses snappy.

Why Queues Matter for Performance

In a standard web request, the user waits for the server to finish every operation before receiving a response. If your project board API needs to generate a PDF report, send an email notification, or process a large image upload, the user is stuck staring at a loading spinner.

By using queues, we capture these tasks as "jobs" and store them in a database or a service like Redis. A background worker process picks these jobs up and executes them independently of the main request. This is the secret to scaling API design for asynchronous processing and maintaining low latency.

Configuring Your Queue Driver

Laravel abstracts the queue system through the QUEUE_CONNECTION setting in your .env file. For development, the sync driver is the default, which executes jobs immediately (blocking the request).

To move to true background processing, you should switch to database or redis. For our project board, let's use the database driver:

  1. Run php artisan queue:table to create the migration.
  2. Run php artisan migrate to create the jobs table.
  3. Update your .env: QUEUE_CONNECTION=database.

Once configured, any job you dispatch will be inserted into this table, waiting for a worker to process it.

Creating and Dispatching Job Classes

Laravel provides a simple artisan command to generate a job class: php artisan make:job ProcessTaskReport. This creates a class in app/Jobs.

Jobs are essentially plain PHP classes with a handle() method. This is where your heavy lifting resides. Here is how we define a job to handle a task-related report in our project board:

PHP
namespace App\Jobs;

use App\Models\Task;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

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

    public function __construct(public Task $task) {}

    public function handle(): void
    {
        #6A9955">// Simulate a heavy task, like generating a PDF
        sleep(5); 
        logger("Report generated for task: {$this->task->id}");
    }
}

The ShouldQueue interface is the critical piece; it tells Laravel that this class should be handled asynchronously rather than immediately.

Dispatching the Job

You trigger the job from your controller or service layer using the dispatch() method:

PHP
public function store(Request $request)
{
    $task = Task::create($request->validated());

    #6A9955">// Offload the heavy report generation
    ProcessTaskReport::dispatch($task);

    return response()->json(['message' => 'Task created, report processing.'], 202);
}

Hands-on Exercise

  1. Generate a Job: Create a job named SendTaskNotification that accepts a User model.
  2. Implement Logic: Inside the handle() method, use Log::info() to simulate sending an email.
  3. Dispatch: Trigger this job inside your TaskService after a task is successfully created.
  4. Test: Run php artisan queue:work in a separate terminal window and create a task via your API. Watch your terminal logs to see the job execute in the background.

Common Pitfalls

  • Forgetting the ShouldQueue Interface: If you omit this, your code will run synchronously, defeating the purpose of using queues.
  • Database Bloat: With the database driver, successful jobs remain in the table. Use php artisan queue:prune to clear out old, completed jobs.
  • Serialization Issues: When you pass a model to a job, Laravel uses SerializesModels. If you delete the model from the database before the job runs, the job will fail. Always ensure your data integrity logic accounts for this, as discussed in Database transactions for data integrity.
  • Worker Latency: If you don't run php artisan queue:work (or a supervisor process), your jobs will sit in the database forever. In production, always use a process monitor like Supervisor or Laravel Horizon.

Recap

Queues are your primary tool for improving performance in high-load applications. By configuring a driver like database, creating ShouldQueue classes, and dispatching them from your service layer, you decouple the user experience from heavy background work. When implemented alongside reliable background jobs, your application becomes significantly more resilient.

Up next: We will explore how to group these tasks together using Job Chaining and Batching to handle complex, multi-step workflows.

Previous lessonIntroduction to Laravel Events and Listeners
Back to Blog

Similar Posts

LaravelJune 25, 20264 min read

Querying Related Data: Mastering Eager Loading in Laravel

Stop the N+1 query problem in its tracks. Learn how to use eager loading in Laravel to keep your application fast and efficient as your data grows.

Read more
LaravelPHPJune 23, 20264 min read

Laravel Queues: Implementing Redis Lua Scripting Rate Limiting

Part of the course

Intermediate Laravel: Real-World Application Patterns

intermediate · Lesson 14 of 58

  1. 1

    Architecting for Maintainability

    3 min
  2. 2

    Implementing the Service Layer

    3 min
  3. 3

    Repository Pattern Fundamentals

    3 min

Laravel Queues and Redis Lua scripting provide the foundation for high-throughput architecture. Learn to implement deterministic rate limiting today.

Read more
LaravelPHPJune 23, 20264 min read

Laravel Serialization: Architecting Deterministic Payloads for High-Performance Queues

Laravel serialization often bloats your message bus. Learn to implement deterministic custom converters for high-performance, type-safe payload transmission.

Read more
  • 4

    Project Board Domain Modeling

    3 min
  • 5

    Advanced Eloquent Scopes and Accessors

    4 min
  • 6

    Service-Oriented Task Management

    3 min
  • 7

    REST API Fundamentals with Sanctum

    3 min
  • 8

    Resource Controllers and API Responses

    3 min
  • 9

    Handling API Validation and Form Requests

    3 min
  • 10

    Implementing Middleware for API Security

    4 min
  • 11

    Database Transactions for Data Integrity

    3 min
  • 12

    Error Handling and Global Exceptions

    3 min
  • 13

    Introduction to Laravel Events and Listeners

    3 min
  • 14

    Asynchronous Processing with Queues

    4 min
  • 15

    Job Chaining and Batching

    Coming soon
  • 16

    Feature Testing Fundamentals

    Coming soon
  • 17

    Mocking Services and Repositories in Tests

    Coming soon
  • 18

    Testing Events and Jobs

    Coming soon
  • 19

    Database Factories and Seeding

    Coming soon
  • 20

    API Versioning Strategies

    Coming soon
  • 21

    Advanced Request Filtering and Sorting

    Coming soon
  • 22

    Handling File Uploads in REST APIs

    Coming soon
  • 23

    Real-time Notifications with Broadcasting

    Coming soon
  • 24

    Using Observers for Model Lifecycle Hooks

    Coming soon
  • 25

    Implementing Policies for Authorization

    Coming soon
  • 26

    Customizing Authentication Guards

    Coming soon
  • 27

    Rate Limiting API Endpoints

    Coming soon
  • 28

    Eloquent Performance Optimization

    Coming soon
  • 29

    Caching Strategies for Performance

    Coming soon
  • 30

    Using Traits for Code Reuse

    Coming soon
  • 31

    Advanced Dependency Injection with Service Providers

    Coming soon
  • 32

    Command Line Tools with Artisan

    Coming soon
  • 33

    Scheduled Tasks and Cron Jobs

    Coming soon
  • 34

    Integrating Third-Party Services

    Coming soon
  • 35

    Handling Webhooks

    Coming soon
  • 36

    Logging and Monitoring

    Coming soon
  • 37

    Database Migrations Best Practices

    Coming soon
  • 38

    Advanced Testing: Integration Tests

    Coming soon
  • 39

    Testing API Authentication

    Coming soon
  • 40

    Code Quality and Static Analysis

    Coming soon
  • 41

    Project Structure for Large Applications

    Coming soon
  • 42

    Environment and Configuration Management

    Coming soon
  • 43

    Deploying Laravel Applications

    Coming soon
  • 44

    Database Indexing Strategies

    Coming soon
  • 45

    Using Value Objects

    Coming soon
  • 46

    Strategy Pattern for Business Rules

    Coming soon
  • 47

    Advanced Queue Monitoring

    Coming soon
  • 48

    Building a Search API

    Coming soon
  • 49

    Handling Concurrency and Race Conditions

    Coming soon
  • 50

    API Documentation with OpenAPI

    Coming soon
  • 51

    Testing with Test Doubles

    Coming soon
  • 52

    Implementing Multi-Tenancy

    Coming soon
  • 53

    Refactoring Legacy Code

    Coming soon
  • 54

    Using Middleware for Feature Flags

    Coming soon
  • 55

    Building Reusable Packages

    Coming soon
  • 56

    Performance Profiling

    Coming soon
  • 57

    Secure API Design

    Coming soon
  • 58

    Event Sourcing Concepts

    Coming soon
  • View full course