While building your web application, you may have some tasks, such as parsing and storing an uploaded CSV file, that take too long to perform during a typical web request. Thankfully, Laravel allows you to easily create queued jobs that may be processed in the background. By moving time intensive tasks to a queue, your application can respond to web requests with blazing speed and provide a better user experience to your customers.
Laravel queues provide a unified queueing API across a variety of different queue backends, such as Amazon SQS, Redis, or even a relational database.
Laravel's queue configuration options are stored in your application's
php artisan queue:work --queue=high,default
44 configuration file. In this file, you will find connection configurations for each of the queue drivers that are included with the framework, including the database, Amazon SQS, Redis, and Beanstalkd drivers, as well as a synchronous driver that will execute jobs immediately [for use during local development]. A
php artisan queue:work --queue=high,default
45 queue driver is also included which discards queued jobs.
[!NOTE] Laravel now offers Horizon, a beautiful dashboard and configuration system for your Redis powered queues. Check out the full Horizon documentation for more information.
Connections vs. Queues
Before getting started with Laravel queues, it is important to understand the distinction between "connections" and "queues". In your
php artisan queue:work --queue=high,default
44 configuration file, there is a
php artisan queue:work --queue=high,default
47 configuration array. This option defines the connections to backend queue services such as Amazon SQS, Beanstalk, or Redis. However, any given queue connection may have multiple "queues" which may be thought of as different stacks or piles of queued jobs.
Note that each connection configuration example in the
php artisan queue:work --queue=high,default
48 configuration file contains a
php artisan queue:work --queue=high,default
48 attribute. This is the default queue that jobs will be dispatched to when they are sent to a given connection. In other words, if you dispatch a job without explicitly defining which queue it should be dispatched to, the job will be placed on the queue that is defined in the
php artisan queue:work --queue=high,default
48 attribute of the connection configuration:
use App\Jobs\ProcessPodcast;
// This job is sent to the default connection's default queue...
ProcessPodcast::dispatch[];
// This job is sent to the default connection's "emails" queue...
ProcessPodcast::dispatch[]->onQueue['emails'];
Some applications may not need to ever push jobs onto multiple queues, instead preferring to have one simple queue. However, pushing jobs to multiple queues can be especially useful for applications that wish to prioritize or segment how jobs are processed, since the Laravel queue worker allows you to specify which queues it should process by priority. For example, if you push jobs to a
php artisan queue:work --queue=high,default
51 queue, you may run a worker that gives them higher processing priority:
php artisan queue:work --queue=high,default
Driver Notes and Prerequisites
Database
In order to use the
php artisan queue:work --queue=high,default
52 queue driver, you will need a database table to hold the jobs. Typically, this is included in Laravel's default
php artisan queue:work --queue=high,default
53 database migration; however, if your application does not contain this migration, you may use the
php artisan queue:work --queue=high,default
54 Artisan command to create it:
php artisan make:queue-table
php artisan migrate
Redis
In order to use the
php artisan queue:work --queue=high,default
55 queue driver, you should configure a Redis database connection in your
php artisan queue:work --queue=high,default
56 configuration file.
[!WARNING]The
php artisan queue:work --queue=high,default
57 and
php artisan queue:work --queue=high,default
58 Redis options are not supported by the
55 queue driver.
php artisan queue:work --queue=high,default
Redis Cluster
If your Redis queue connection uses a Redis Cluster, your queue names must contain a . This is required in order to ensure all of the Redis keys for a given queue are placed into the same hash slot:
'redis' => [
'driver' => 'redis',
'connection' => env['REDIS_QUEUE_CONNECTION', 'default'],
'queue' => env['REDIS_QUEUE', '{default}'],
'retry_after' => env['REDIS_QUEUE_RETRY_AFTER', 90],
'block_for' => null,
'after_commit' => false,
],
Blocking
When using the Redis queue, you may use the
php artisan queue:work --queue=high,default
60 configuration option to specify how long the driver should wait for a job to become available before iterating through the worker loop and re-polling the Redis database.
Adjusting this value based on your queue load can be more efficient than continually polling the Redis database for new jobs. For instance, you may set the value to
php artisan queue:work --queue=high,default
61 to indicate that the driver should block for five seconds while waiting for a job to become available:
'redis' => [
'driver' => 'redis',
'connection' => env['REDIS_QUEUE_CONNECTION', 'default'],
'queue' => env['REDIS_QUEUE', 'default'],
'retry_after' => env['REDIS_QUEUE_RETRY_AFTER', 90],
'block_for' => 5,
'after_commit' => false,
],
[!WARNING]Setting
php artisan queue:work --queue=high,default
60 to
php artisan queue:work --queue=high,default
63 will cause queue workers to block indefinitely until a job is available. This will also prevent signals such as
64 from being handled until the next job has been processed.
php artisan queue:work --queue=high,default
Other Driver Prerequisites
The following dependencies are needed for the listed queue drivers. These dependencies may be installed via the Composer package manager:
- Amazon SQS:
65php artisan queue:work --queue=high,default
- Beanstalkd:
66php artisan queue:work --queue=high,default
- Redis:
67 or phpredis PHP extensionphp artisan queue:work --queue=high,default
Creating Jobs
Generating Job Classes
By default, all of the queueable jobs for your application are stored in the
php artisan queue:work --queue=high,default
68 directory. If the
php artisan queue:work --queue=high,default
68 directory doesn't exist, it will be created when you run the
php artisan queue:work --queue=high,default
70 Artisan command:
php artisan make:job ProcessPodcast
The generated class will implement the
php artisan queue:work --queue=high,default
71 interface, indicating to Laravel that the job should be pushed onto the queue to run asynchronously.
[!NOTE] Job stubs may be customized using .
Class Structure
Job classes are very simple, normally containing only a
php artisan queue:work --queue=high,default
72 method that is invoked when the job is processed by the queue. To get started, let's take a look at an example job class. In this example, we'll pretend we manage a podcast publishing service and need to process the uploaded podcast files before they are published: