How Queues Function in Laravel
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.
Scenario
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:
- Store user's information into the database
- Send an appreciated email to the user
- 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:
- store user's data into the database
- $this->dispatch(new SendWelcomeEmail($user)); // push a SendWelcomeEmail job onto the queue
- 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.