On this page
Laravel Queues: From Basic Setup to Advanced Job Batching
When building modern web applications, handling time-consuming tasks efficiently is crucial for user experience. Laravel's queue system provides a powerful way to defer these tasks, allowing your application to remain responsive. Let's explore how to implement queues and take it further with job batching.
Why Use Laravel Queues?
Queues allow you to defer the processing of time-consuming tasks, such as sending emails, processing uploads, or making API calls. Instead of making users wait, these tasks are executed in the background, resulting in:
- Faster response times
- Better user experience
- Improved error handling
- Scalability
Basic Queue Setup
First, configure your queue connection in .env:
QUEUE_CONNECTION=database
For production, consider Redis or Amazon SQS instead of the database driver for better performance.
Create your first job:
php artisan make:job ProcessPodcast
Here's a simple job example:
<?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;
class ProcessPodcast implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(public Podcast $podcast) {}
public function handle()
{
// Process podcast episode
$this->podcast->process();
}
}
Dispatching Jobs
You can dispatch jobs from anywhere in your application:
// Basic dispatch
ProcessPodcast::dispatch($podcast);
// Delayed dispatch
ProcessPodcast::dispatch($podcast)
->delay(now()->addMinutes(10));
// Synchronous dispatch (for testing)
ProcessPodcast::dispatchSync($podcast);
Queue Workers
Start a queue worker to process jobs:
php artisan queue:work
For production, use a process manager like Supervisor to keep the worker running:
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=true
user=forge
numprocs=8
redirect_stderr=true
stdout_logfile=/path/to/worker.log
Advanced: Job Batching
Laravel's job batching allows you to execute a batch of jobs and perform actions when the batch completes:
use Illuminate\Support\Facades\Bus;
use App\Jobs\ProcessPodcast;
use Throwable;
$batch = Bus::batch([
new ProcessPodcast($podcast1),
new ProcessPodcast($podcast2),
new ProcessPodcast($podcast3),
])->then(function (Batch $batch) {
// All jobs completed successfully
})->catch(function (Batch $batch, Throwable $e) {
// First batch job failure detected
})->finally(function (Batch $batch) {
// The batch has finished executing
})->dispatch();
return $batch->id;
Monitoring and Debugging
Laravel Horizon provides a beautiful dashboard for monitoring your queues:
composer require laravel/horizon
php artisan horizon:install
php artisan horizon
Common Queue Configuration Options
| Option | Description | Default |
|---|---|---|
--queue |
The queue to process | default |
--timeout |
Maximum seconds a job can run | 60 |
--tries |
Number of times to attempt a job | 1 |
--backoff |
Seconds to wait before retrying | 0 |
--memory |
Memory limit in MB | 128 |
FAQ
1. How do I handle failed jobs?
Laravel provides several ways to handle failed jobs. You can define a failed method on your job class or configure global failure handling in App\Providers\AppServiceProvider:
use Illuminate\Support\Facades\Queue;
use Illuminate\Queue\Events\JobFailed;
Queue::failing(function (JobFailed $event) {
// Handle the job failure...
});
2. How can I prioritize certain jobs?
You can assign jobs to different queues and process them with different priorities:
// Dispatch to high priority queue
ProcessPodcast::dispatch($podcast)->onQueue('high');
// Process high priority queue first
php artisan queue:work --queue=high,default
3. What's the difference between dispatch and dispatchSync?
dispatch pushes the job to the queue for asynchronous processing, while dispatchSync runs the job immediately (synchronously) without queueing it.
4. How do I test queued jobs?
Laravel provides testing helpers for queues:
// Assert a job was dispatched
Bus::fake();
Bus::assertDispatched(ProcessPodcast::class);
// Assert a job was not dispatched
Bus::assertNotDispatched(ProcessPodcast::class);
5. How can I monitor queue performance?
Use Laravel Telescope or Horizon to monitor your queues. You can also use the queue:monitor command to monitor queue sizes and set up alerts.
Conclusion
Laravel's queue system is a powerful tool for building scalable applications. By implementing queues and job batching, you can significantly improve your application's performance and user experience. Remember to monitor your queues in production and adjust worker configurations based on your workload. For more Laravel tips and tutorials, visit mahbuburriad.com.