Laravel queues let you defer time-consuming tasks — sending emails, processing images, making slow API calls, generating PDFs — to a background worker. The user gets an instant response while the work happens asynchronously. This tutorial builds a complete queue setup with Redis and Laravel Horizon.
Table of Contents
Why Queues?
Without queues: user submits a form → your server sends a welcome email (200–500ms) → page loads. With queues: user submits form → job dispatched to Redis (1–5ms) → page loads immediately → queue worker sends email in background.
Use queues for anything that does not need to complete before the HTTP response: email sending, PDF generation, image resizing, webhook delivery, data exports, third-party API calls.
Step 1: Configure Redis Queue Backend
# .env:
QUEUE_CONNECTION=redis
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
# Install predis or phpredis extension (see PHP Redis tutorial)
composer require predis/predis
Step 2: Create a Job
php artisan make:job SendWelcomeEmail
// app/Jobs/SendWelcomeEmail.php
class SendWelcomeEmail implements ShouldQueue {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public int $tries = 3; // Retry up to 3 times
public int $timeout = 30; // Fail after 30 seconds
public int $backoff = 60; // Wait 60s before retrying
public function __construct(private readonly User $user) {}
public function handle(MailService $mail): void {
$mail->sendWelcome($this->user);
Log::info("Welcome email sent to {$this->user->email}");
}
// Called when all retries are exhausted:
public function failed(Throwable $e): void {
Log::error("Failed to send welcome email to {$this->user->email}: " . $e->getMessage());
// Notify your team, trigger a fallback, etc.
}
}
Step 3: Dispatch Jobs
// Dispatch immediately (goes to default queue):
SendWelcomeEmail::dispatch($user);
// Dispatch to a specific queue:
SendWelcomeEmail::dispatch($user)->onQueue('emails');
// Delay execution (send 5 minutes from now):
SendWelcomeEmail::dispatch($user)->delay(now()->addMinutes(5));
// Dispatch only if condition is met:
SendWelcomeEmail::dispatchIf($user->wantsEmails(), $user);
// Chain jobs (run sequentially):
Bus::chain([
new ProcessOrder($order),
new SendOrderConfirmation($order),
new UpdateInventory($order),
])->dispatch();
// Batch jobs (run in parallel, callback when all done):
Bus::batch([
new ExportUserCsv($chunk1),
new ExportUserCsv($chunk2),
new ExportUserCsv($chunk3),
])->then(function (Batch $batch) {
// All chunks done — merge files
})->dispatch();
Step 4: Run Queue Workers
# Process all queues (development):
php artisan queue:work
# Process specific queue:
php artisan queue:work redis --queue=emails,default
# Specify max jobs before worker restarts (prevents memory leaks):
php artisan queue:work --max-jobs=1000
# In production with Supervisor (keeps worker running):
# /etc/supervisor/conf.d/laravel-worker.conf
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/myapp/artisan queue:work redis --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data
numprocs=4 ; 4 parallel workers
redirect_stderr=true
stdout_logfile=/var/www/myapp/storage/logs/worker.log
Handling Failed Jobs
# Create the failed_jobs table:
php artisan queue:failed-table
php artisan migrate
# View failed jobs:
php artisan queue:failed
# Retry a specific failed job:
php artisan queue:retry 5
# Retry all failed jobs:
php artisan queue:retry all
# Delete all failed jobs:
php artisan queue:flush
Laravel Horizon — Queue Dashboard
composer require laravel/horizon
php artisan horizon:install
php artisan migrate
# Start Horizon (replaces artisan queue:work in production):
php artisan horizon
# Access dashboard at: http://yourapp.com/horizon
Horizon provides a web dashboard showing:
- Jobs processed per minute (throughput)
- Queue depth (how many jobs are waiting)
- Failed jobs with stack traces
- Worker status and resource usage
- Job execution time history
// config/horizon.php — configure workers per queue:
'environments' => [
'production' => [
'supervisor-1' => [
'maxProcesses' => 10,
'balanceMaxShift' => 1,
'balanceCooldown' => 3,
],
'emails' => [
'connection' => 'redis',
'queue' => ['emails'],
'maxProcesses' => 5,
],
],
],
Frequently Asked Questions
What happens if the queue worker crashes?
The current job is lost unless it was already removed from the queue (which happens after successful execution). Supervisor automatically restarts the worker process. Use $this->release(60) inside your job to return it to the queue if it cannot complete, or rely on the $tries property for automatic retries.
Can I use queues on shared hosting?
Limited. Shared hosting typically does not support long-running processes or Supervisor. You can use the database queue driver and a cron-based worker (php artisan queue:work --stop-when-empty called every minute via cron), but this is much less efficient. For serious queue usage, a VPS or PaaS platform is required.
Conclusion
Laravel queues transform slow synchronous operations into fast, deferred background tasks. The workflow is: create a job with make:job, implement handle() with the work, dispatch from a controller, and run workers with Supervisor. Add Laravel Horizon for monitoring and you have a production-ready queue system that scales from a few jobs per hour to millions per day.
Next Step for Your Project Journey
Pick your next action to keep building: download complete source code, continue with practical tutorials, or explore more project systems.

