Introduction
Laravel is a powerful MVC framework for PHP known for its elegant syntax and a rich set of features. One powerful feature is its queue system, which allows developers to defer the processing of time-consuming tasks, such as sending emails or processing uploaded videos, thus speeding up web requests to your application. In this tutorial, we will explore Laravel’s queue system with examples ranging from basic to advanced usage.
Getting Started with Queues
To get started with queues in Laravel, you first need to configure your queue settings in the config/queue.php file. Laravel supports several queue backends out of the box: sync, database, redis, sqs, and beanstalkd. For many applications, the database queue driver is a good start. Here’s how to set it up:
php artisan queue:table
php artisan migrateThis will create the necessary tables in your database to manage jobs. You then set your .env file’s queue driver to database:
QUEUE_CONNECTION=databaseCreating a Job Class
Next, you’ll want to create a job class that actually contains the logic of what you wish to queue. For example:
php artisan make:job ProcessPodcastThis command generates a class ProcessPodcast within the app/Jobs directory. You can then define your job’s logic in the handle() method:
<?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 handle()
{
// Process the podcast...
}
}
Dispatching Jobs
Once you have a job class, you can dispatch instances of the job using the dispatch function:
ProcessPodcast::dispatch();Basic Example: If you want to send an email after a user has registered, instead of sending it in the controller method, you can dispatch a job:
SendWelcomeEmail::dispatch($user);Working with Queues
With your job dispatched, you now need to run a worker that will listen for new jobs and process them. In your command line:
php artisan queue:workThis command will continue to run and process jobs as they are dispatched.
Handling Failed Jobs
Sometimes jobs fail. Laravel makes it easy to retry them or even automatically retry:
php artisan queue:retry allYou can also define the number of attempts and delay between attempts directly in your job class with the $tries and $backoff properties.
Advanced Queue Topics
Laravel offers advanced queue features like job chaining, rate limiting, and job batching. Let’s go into them one by one.
Job Chaining
If you need to run several jobs in sequence, you can chain them together:
ProcessPodcast::withChain([
new OptimizePodcast,
new ReleasePodcast
])->dispatch();Rate Limiting
Rate limiting can prevent a particular job from overwhelming your system or an external API:
Redis::throttle('key')->allow(10)->every(60)->then(function () {
// Job logic...
}, function () {
// Could not obtain lock...
return $this->release(10);
});Job Batching
Laravel 8 introduced job batching, which allows you to execute a batch of jobs and then perform a completion job:
Bus::batch([
new ProcessPodcast($podcast1),
new ProcessPodcast($podcast2),
// ...
])->then(function (Batch $batch) {
// All podcast jobs completed successfully...
})->catch(function (Batch $batch, Throwable $e) {
// First batch job failure detected...
})->finally(function (Batch $batch) {
// The batch has finished executing...
})->dispatch();Conclusion
In this tutorial, we have covered Laravel’s queue system from basic job dispatching to advanced features like job batching. Queues are essential for performing tasks without delaying user responses in your application. Laravel provides an elegant and simple way to implement queues that can scale from simple email dispatches to complex job workflows.