To use this site please enable javascript on your browser! Bryce Andy | How Queues Make Laravel Run Faster

How Queues Make Laravel Run Faster

Bryce on Oct 21, 2018


What are Queues?

Queues are everywhere, in ticketing offices, banks, food vendors, gyms, and ohh...your Laravel app. Today we are going to see what these queues are in our lara-apps. In Laravel, queues are a bit different. For example, imagine your user registration activity.

First there is the creation of the user in the database, after creating this user maybe you triggered an event and the listener requires an email verification emailfor this user.

This series of activities may seem little but the web application will take some time until the email is sent and redirecting the user  to their dashboard or home page. It will take even a longer time if the email uses a third-party API to deliver. To save time and pages loading faster for better user experience, Laravel Queues come to the rescue.


Implementing Queues 

There are plenty of things that can make use of queues in Laravel, not just emails and events. Collect all of the following and we can start queuing them:

If any of them are connected to each other (say an event listener fires up an email), be sure to only queue the one that will start. To configure queues first we set its configurations on the .env file as follows


There are plenty of other drivers to use for queuing, but for a start we will use our database. Now make sure your database configs are also all set. Next up we are going to create a table for the queue jobs and migrate the respective table migration.

php artisan make:queue table

Then migrate the migration to create a Jobs table in our database,

php artisan migrate

Whenever a queue is being processed then there will be a new record in the Jobs table and when it is completed it will not be seen. The following step requires identifying which task needs to be queued, whether its your emails, notifications etc. 

We could set up a custom queued job using php artisan make:job SomeNewJobClass, then use it to apply delays or many other modifications but lets save that for another lengthy post. 

For simplicity of implemeting queues, we will use simple classes we have that need queuing to fasten our app. Now go to an app/Email class, app/Notification or an event app/Listener class  then import the ShouldQueue contract and below it implement the trait like this;


namespace App\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;

class SendSubscribersNotification implements ShouldQueue


For the case above, I had an event Listener called SendSubscribersNotification which sends emails to my subscribers when a new post is published. The last bit is now making sure the queued jobs are implemented. If we just leave it like this, all email sending tasks that were supposed to be sent will stack up in the Jobs table and are not going to be performed.

To implement our jobs we are going to use the artisan command

php artisan queue:work

For production we should make sure this command is run at all times and I will write another post dedicated to having queue workers in production environment. Thats it.


Now that we have implemented queues emails will no longer take 6-12 seconds to send but just with a single click and boom you are there.


Bryce is a web developer based in Dar. Apart from writing code, you can find him in the gym, writing informative articles or playing football.

Copyright © Bryce Andy 2018