HomePortfolioNewsGalleryContact
LaravelQueueBackground Jobs

Building a Queue System in Laravel for Heavy Background Jobs

By Aditya Nursyahbani20 min read45
Share:
Building a Queue System in Laravel for Heavy Background Jobs

As Laravel applications grow, certain operations become too expensive to process during a normal HTTP request. Tasks like sending emails, generating reports, processing uploads, syncing APIs, and AI responses can slow down your application and create poor user experiences.

Queues solve this problem by moving heavy operations into background workers.

Laravel provides one of the best queue systems in the PHP ecosystem, allowing developers to process asynchronous jobs efficiently using Redis, database queues, or services like Amazon SQS.

In this tutorial, you will learn how to build a queue system in Laravel for handling heavy background jobs in production applications.

By the end of this guide, you will understand how to:

  • Configure Laravel queues
  • Use Redis queue drivers
  • Create asynchronous jobs
  • Process chatbot or AI requests in the background
  • Run queue workers in production
  • Improve application performance

Why Use Queues?

Without queues:

  • Requests become slow
  • Users wait for long responses
  • API timeouts increase
  • Heavy operations block the application

Queues allow Laravel to process tasks asynchronously.


Step 1: Configure Queue Driver

Update .env:

env
QUEUE_CONNECTION=redis

Install Redis client:

bash
composer require predis/predis

Step 2: Create Queue Job

Generate a new job:

bash
php artisan make:job ProcessChatbotMessage

Laravel creates:

app/Jobs/ProcessChatbotMessage.php

Step 3: Build Queue Logic

php
<?php namespace App\Jobs; use Illuminate\Contracts\Queue\ShouldQueue; class ProcessChatbotMessage implements ShouldQueue { public $message; public function __construct($message) { $this->message = $message; } public function handle(): void { // Heavy AI processing here sleep(5); logger('Processed: '.$this->message); } }

Step 4: Dispatch Queue Job

Inside controller:

php
<?php use App\Jobs\ProcessChatbotMessage; public function sendMessage(Request $request) { ProcessChatbotMessage::dispatch( $request->message ); return response()->json([ 'message' => 'Message queued successfully' ]); }

The HTTP response becomes instant while processing happens in the background.

Step 5: Start Queue Worker

Run queue worker:

bash
php artisan queue:work

For development:

bash
php artisan queue:listen

Step 6: Queue Failed Jobs

Create failed jobs table:

bash
php artisan queue:failed-table php artisan migrate

Retry failed jobs:

bash
php artisan queue:retry all

View failed jobs:

bash
php artisan queue:failed

Step 7: Queue Priority

Laravel supports queue priorities.

php
<?php ProcessChatbotMessage::dispatch($message) ->onQueue('high');

Worker:

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

Step 8: Production Queue Management

Use Supervisor to keep workers running.

Example Supervisor config:

bash
[program:laravel-worker] process_name=%(program_name)s_%(process_num)02d command=php /var/www/app/artisan queue:work redis autostart=true autorestart=true numprocs=4 redirect_stderr=true stdout_logfile=/var/www/app/worker.log

Restart Supervisor:

bash
sudo supervisorctl reread sudo supervisorctl update sudo supervisorctl start laravel-worker:*

Queue Use Cases

Queues are useful for:

  • AI chatbot processing
  • Sending emails
  • Notification systems
  • Video/image processing
  • PDF generation
  • Webhook delivery
  • API synchronization

Conclusion

Laravel queues are essential for building scalable applications. By moving expensive tasks into background workers, you can dramatically improve response times, reduce server load, and build production-ready systems that handle heavy traffic efficiently.