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
- Queue worker – background process written in PHP, NodeJS, etc. that always in alert to get tasks from the queue and execute them.
- Queuing service – Beanstalk, AWS SQS, Redis can be used to store queues. These provide task by task to execute for queue worker.
- 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.