Laravel Queue

Laravel Queues And Why They Are Important

Spread the love

Last Updated on March 15, 2023


The ROI of a good user experience is 500% or more. A good user experience increases the conversion rate of a website and saves a user time as they use a website. Laravel Queues allow a developer to improve the user experience for their website. It makes tasks asynchronous and run in the background. This allows users to interact with your website without any issues. In this article, I will show you how to use them effectively to increase the user experience for your website.


What is Laravel queue

Laravel Queues provide an implementation that allows tasks to be asynchronous and run in the background. Users can proceed with their interaction on the website and this yields a good user experience. They provide an API for different queue backends including  Amazon SQSRedis, or even a relational database.

Configuring Laravel Queues

Laravel provides a set of drivers that can be used to configure our queues. We are going to use the database driver which involves using the database to record the tasks we want to add to the queue.

To start, we need to run the following commands:

php artisan queue:table

php artisan migrate

These commands will create a migration file and run the migration to create the table responsible for holding the tasks.

Laravel Queues

We need to configure our application to use the database driver. In addition to that, we also need to update the QUEUE_CONNECTION variable in the .env file.


Using Laravel Queues To Configure Emails

The next step is to use Laravel queues in configuring tasks to run asynchronously. In this case, we are going to configure emails to run in the background.

Let’s start by running the following command:

php artisan make:mail PaymentMail

The command creates a mailable class responsible for sending payment notification emails.

Laravel Queues

The next step is to create a job to manage the emails. We can use the following command:

php artisan make:job Payment
Laravel Queues

We create a mail job that will run once the email is added to the queue.

Laravel Queue Implementation

Once the configurations are done, we now need to implement the logic of the actual queue. To do so, we need to create a controller that will handle the logic.

We use the following command:

php artisan make:controller PaymentController
Laravel Queues

This email is then added to the queue once the application logic is executed.

To run the queue worker we need to execute the following command:

php artisan queue:work

This command runs until it is manually stopped or you close the terminal.

Laravel will start processing the tasks in the queue on a first-come-first-served basis. This ensures that the email task runs in the background and does not interfere with other tasks.

Laravel Queue Worker On A Production Server

In production, we need a way of running the queue:work command. we may also need a way of restarting the command if for some reason it stops. To do so, we need a process that will monitor the state of the daemon and also restart it once it stops running. We will use supervisor for this task. Supervisor is a process monitor for the Linux Operating System that keeps track of various processes’ states.

To install it on an Ubuntu server, we can run the following command:

sudo apt-get install supervisor

Supervisor configuration files are stored in the /etc/supervisor/conf.d directory. To add configurations, we need to access the directory and create a file that will contain the configurations using the following commands:

cd /etc/supervisor/conf.d

sudo nano laravel-worker.conf

The supervisor configurations instruct supervisor on how it should monitor the processes. In this case, we need to monitor the queue: work process. To do so, we will use the following configurations:

command=php /path/to/your application/artisan queue:work --sleep=3 --tries=3 --daemon
stdout_logfile=/path/to/your application/worker.log​

In this example, the numprocs instruction tells Supervisor to run 5 processes and monitor all of them simultaneously. It also instructs Supervisor to auto-restart them if they fail. The tries flag on the command instructs Supervisor the number of times it will restart the command before flagging it as failed and recording the error on the log file.

Once the configurations are set, we need to start Supervisor using the following commands:

sudo supervisorctl reread

sudo supervisorctl update

sudo supervisorctl start laravel-worker:*

Note that if you make any changes to your code, you will need to restart the queue process for the changes to take effect in production. You can run this command to restart the queue.

php artisan queue:restart


In Conclusion, we have established that user experience is a key factor in increasing the conversion rate of a website. In this article, we have implemented a simple email sender using Laravel queues. Laravel queues can be used to increase the performance of a website which results in a good user experience. It allows for complex tasks to be asynchronous making the overall performance of the application increase.

I hope this article sheds some light on what Laravel queues are and how to implement them. Have questions? Let me know in the comments below. I’ll try my best to answer all of them.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *