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
38 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
39 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
38 configuration file, there is a
php artisan queue:work --queue=high,default
41 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
42 configuration file contains a
php artisan queue:work --queue=high,default
42 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
42 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
45 queue, you may run a worker that gives them higher processing priority:
php artisan queue:work --queue=high,default
Driver Notes & Prerequisites
Database
In order to use the
php artisan queue:work --queue=high,default
46 queue driver, you will need a database table to hold the jobs. To generate a migration that creates this table, run the
php artisan queue:work --queue=high,default
47 Artisan command. Once the migration has been created, you may migrate your database using the
php artisan queue:work --queue=high,default
48 command:
php artisan queue:table
php artisan migrate
Finally, don't forget to instruct your application to use the
php artisan queue:work --queue=high,default
46 driver by updating the
php artisan queue:work --queue=high,default
50 variable in your application's
php artisan queue:work --queue=high,default
51 file:
QUEUE_CONNECTION=database
Redis
In order to use the
php artisan queue:work --queue=high,default
52 queue driver, you should configure a Redis database connection in your
php artisan queue:work --queue=high,default
53 configuration file.
WarningThe
php artisan queue:work --queue=high,default
54 and
php artisan queue:work --queue=high,default
55 Redis options are not supported by the
52 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' => 'default',
'queue' => '{default}',
'retry_after' => 90,
],
Blocking
When using the Redis queue, you may use the
php artisan queue:work --queue=high,default
57 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
58 to indicate that the driver should block for five seconds while waiting for a job to become available:
'redis' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => 'default',
'retry_after' => 90,
'block_for' => 5,
],
WarningSetting
php artisan queue:work --queue=high,default
57 to
php artisan queue:work --queue=high,default
60 will cause queue workers to block indefinitely until a job is available. This will also prevent signals such as
61 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:
62php artisan queue:work --queue=high,default
- Beanstalkd:
63php artisan queue:work --queue=high,default
- Redis:
64 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
65 directory. If the
php artisan queue:work --queue=high,default
65 directory doesn't exist, it will be created when you run the
php artisan queue:work --queue=high,default
67 Artisan command:
php artisan make:job ProcessPodcast
The generated class will implement the
php artisan queue:work --queue=high,default
68 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
69 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: