How task queueing work in a web application (PHP, Laravel)

In this article mainly focused on Laravel framework and how queuing work and why use queuing in a web application.

Why use Queues?

We use queues in a web application to improve performance or to send quick response to user. Think that user registration has mail verification process that require sending a email to user’s mail address. User had to wait till mail is send to receive user registration success in a web application. What if we can send the user success message while processing mail sending in the background. We can do that with Queues.

In other word I can say queues are used to convert asynchronous tasks to synchronous. So what are synchronous and asynchronous tasks.

Synchronous – Synchronous tasks block client response till operations are completed. User will see a loading screen for a long time for time intensive tasks.

Asynchronous – Asynchronous tasks does not block the client response instead it can send response quickly to client while handling the required tasks specially time intensive tasks in the background or server.

How task queueing work?

There are 3 players when queuing tasks. They are

  1. Queue worker – background process written in PHP, NodeJS, etc. that always in alert to get tasks from the queue and execute them.
  2. Queuing service – Beanstalk, AWS SQS, Redis can be used to store queues. These provide task by task to execute for queue worker.
  3. Database – Store Job data and provide extra information required to process the job.

Below shows how task queueing work when you register for a web application. Here user registration message is send via a mail. Sending a mail is a time consuming task. Therefore mail sending task is send to queue and user success response is send back to user soon after storing user data in the database while mail sending task is handle.

Browser
Browser
User Registration
Request
User Registration…
Server
Server
Queue 
worker
Queue…
Process
User
Registration
Process…
Send User Success
Response
Send User Success…
Passing mail registration job to queue
by web application
Passing mail reg…
1
1
3
3
4
4
Queue Service
ex: Beanstalkd
Queue Service…
Database
Database
Mail registration in the queue passed to
queue worker
Mail registration in…
5
5
Actually 
sending mail 
registration
Actually…
Store user information
Store user information
2
2
send information
required to process  mail verification
send information…
6
6
7
7
Text is not SVG – cannot display