April 5, 2020
laravel-listen

Laravel Queue Listen v/s Work

Often while building web applications a common mistake developers do is to add their mail sending codes in the same controller function where they are processing their initial response data. This process forces the software to run slowly as sending a mail via SMTP can be a time-consuming process. In my very latest project “Techmion Logistics” (I will add a link here once the software intro video is done) I faced a similar issue. Each successful booking would send a mail to the consignor with an acknowledgement slip. But, since I didn’t implement queue yet my entire booking process would take approximately 8-10 Seconds. The slow speed annoyed me so much that I decided to implement the queue method in Laravel and honestly it was my best decision ever. The entire booking process reduced to 1 Second. Yup! 1 second only. That’s how fast it was. I will soon write a tutorial on it. Make sure to subscribe to our newsletter to stay updated on latest TechFlow360 posts.

The queue configuration file is stored in config/queue.php which stores queue configs of which includes a database, Beanstalkd, Amazon SQS, Redis, and sync.

The Queue Listener: The Laravel artisan command will listen to new commands as they are being dispatched to the queue. It is important to note that this process doesn’t stop unless we use some process monitor like Supervisor which makes sure that the listen command is working properly.

php artisan queue:listen

The Queue Worker: Unlike queue which keeps on listening for active connections the Queue Worker only processes the first job of the queue. Again, The Queue worker also runs indefinitely; it is recommended to use Supervisor to make sure that the command is running efficiently.

php artisan queue:work

Note: Although the Queue worker is theoretically expected to stop working after processing the first job but in fact, it keeps on running and the only constraint it possesses is that it needs to be restarted whenever there is any application upgrade.

Check here to find more about installing and using Supervisor.

So Finally, here is the tabular comparison:-

FeatureQueue ListenQueue Worker
Artisan Commandphp artisan queue:listenphp artisan queue:work
Process CycleAn infinite loop of process one job, restarting and releasing full memoryAn infinite loop where the process starts once and keeps listening for new jobs.
Memory LeaksLow Risk due to constant restarts and memory releaseHigh Risk
Memory ConsumptionHighLow
CPU ConsumptionHighLow
Software Updates or Patch FixesRestart not requiredRestart required

Saifur Rahman

Saifur Rahman is a Full Stack Laravel Developer. Additionally, he has spent a significant amount of time to learn and research in the domain of the Internet of Things (IoT). He loves to share his work and contribute to helping fellow developers. Saifur also runs the following websites and services - Pingsms.in and Techmion.com

View all posts by Saifur Rahman →

Leave a Reply

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