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 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:-
|php artisan queue:listen
|php artisan queue:work
|An infinite loop of process one job, restarting and releasing full memory
|An infinite loop where the process starts once and keeps listening for new jobs.
|Low Risk due to constant restarts and memory release
|Software Updates or Patch Fixes
|Restart not required
Md. Saifur Rahman is a Full Stack Django and Laravel Developer. Additionally, he loves to spend time learning and researching about the Internet of Things (IoT). He loves to share his work and contribute to helping fellow developers.