Go To Blog
Blog IconBlog

How Queues Function in Laravel

Blog Post Image

Laravel is a standout amongst the most well known and advanced PHP frameworks for web applications with its expressive syntax. It is one of the most expressive and rich web application frameworks. Laravel application development eases out the common tasks of a majority of the web projects like that of authentication, routing, sessions, and caching.

In this edition we are going to see how queues enable you to defer/postpone the preparing of a time consuming task until a later time.


Imagine that you have a site where visitors can register for an account, maybe an eCommerce or social network website. A visitor is currently on the registration page and has submitted the registration form. We need the following to happen: 

  1. Store user's information into the database 
  2. Send an appreciated email to the user 
  3. Return "thank you" page

Assume there’s some PHP in the background that takes the info and storing the user into the database. After inserting the user into the database, the script now has to send him a welcome email and, since PHP executes code line-by-line top-to-bottom, the user will see the “Thank you” page only after the email has been sent. Even though sending email sometimes happens very fast, it sometimes takes ages for this to happen. Why make the user wait?

Queues to the rescue!

That’s where queue services come into play. I’ll use terminology that’s specific to Laravel’s queue API. A queue is just a list/line of jobs waiting to be handled in order, starting from the beginning.

What if we take the process of sending the email and shove it into a job, and then push that job onto the queue instead? This approach now differs at the second step:

  1. store user's data into the database
  2. $this->dispatch(new SendWelcomeEmail($user)); // push a SendWelcomeEmail job onto the queue
  3. return "thank you" page

Instead of returning the “thank you” response after the email has been sent, we now return the response after the job has been pushed onto the queue. This way, user has to wait only as long as it takes for the job to be pushed, as opposed to waiting for an email to be actually sent.

Executing jobs

There’s this queue/list and there are these jobs that get pushed onto the queue. But when and how do these jobs get executed? When does the welcome email actually gets sent? In Laravel, there is this intimidating thing called Queue Listener. Queue Listener is nothing more than a long-running process that listens to and runs the jobs from the queue.

We start the Queue Listener by running the following command from the terminal:

php artisan queue:listen

If there were already jobs on the queue, it will just go on and do them one-by-one.

Queue Drivers

A queue driver is a concrete implementation of the Queue interface. It is responsible for managing the jobs, that is - storing and retrieving the jobs from our queue. There are several drivers that ship with Laravel, which includes a database, Beanstalkd, Amazon SQS, Redis, and a synchronous driver that will execute jobs immediately. A null queue driver is also included which simply discards queued jobs.

This was a short outline of how queues function in Laravel.

Questions? Call us.

Call us anytime with the questions you have.
We are here to help.

+94 114 387 089 / 90 /92 OR +94 214 927 089

Contact Us
If you have queries as to how we can assist you and your projects, please fill out the form below.
Contact Us
Type us a message
If you prefer, send a message.
We will get back to you.
Type Message
What our clients say about us
Colombo Jaffna Melbourne