Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Getting Started With Laravel Queues and Background Jobs


First we need to create our class for a background job. In this case I am creating a background job for sending a welcome email to a user.

php artisan make:job EmailUserWelcomeEmail

You will now see the class within /app/Jobs

The class will implement the ShouldQueue interface.

To prevent issues with loading, put parameters in constructor and pass parameters using ::dispatch


Configure Laravel To Use Database Driver

When I first ran this, I saw that the code was not running these jobs in the background. It turns out this was because my config/queue.php file had the queue driver set to sync. This essentially means there is no queue and the jobs run immdediately. (More info).

The simplest solution to get an asynchronous queue working was to switch to the database driver. Before enabling this driver, we need to make sure the queue table is created as well as a table to hold details of any failed jobs:

php artisan queue:table
php artisan queue:failed-table

This creates the migration script to create the "jobs" table like so:

Schema::create('jobs', function (Blueprint $table) {

... and the failed jobs table like so:

Schema::create('failed_jobs', function (Blueprint $table) {

Then you need to run migrations to run this new migration:

php artisan migrate

Specify the Database Driver

Now you can go to /config/queue.php and update the queue driver like so:

    | Default Queue Connection Name
    | Laravel's queue API supports an assortment of back-ends via a single
    | API, giving you convenient access to each back-end using the same
    | syntax for every one. Here you may define a default connection.

    'default' => env('QUEUE_CONNECTION', 'database'),

Also, make sure that if QUEUE_CONNECTION is set in your .env file, that you update it to state database.

Older versions of Laravel referred to the environment variable as QUEUE_DRIVER instead. Refer here.

Configure Supervisor

After performing all of the steps above, I saw that my request was returning immediately, but I wasn't getting any emails. Looking in the jobs table, I could see the jobs there with an attempts count set to 0. It appears that nothing was actually running the queue. To do this, the easiest thing to do is configure Supervisor, by adding the following configuration to the Supervisor config file:

command=php /var/www/my-site/artisan queue:work database --sleep=3 --tries=3

In this example, the numprocs directive will instruct Supervisor to run 1 queue:work processes and monitor it, automatically restarting it if it fails. Logs of errors will be redirected to the log file within laravel's storage directory.


Last updated: 1st September 2021
First published: 16th June 2021