Intro to Laravel queues

When developing a web application, queues are necessary for certain types of tasks and duties. Laravel provides an easy to use queue system that allows developers to easily add tasks to a queue and process them asynchronously. With the new advanced queues in Laravel, you can assign priorities to your tasks and jobs, as well as control the rate at which tasks are processed. This allows you to have a highly optimized queue system that can provide your web application with fast and reliable service.

The main advantage of queues is that they allow developers to prioritize tasks and have tasks processed at the rate they need to be. This allows developers to optimize the performance of their web application, as certain tasks can be completed first, and non-critical tasks can be completed later. Another advantage of advanced queues is that you can set the rate at which tasks are processed, meaning that your web application can handle a large number of requests more easily. This can increase the overall performance of your web application and it redcuses the response time to the end user as well.

What is Laravel Queue?

Laravel provides a unified API for various queue backends (including Beanstalk, Amazon SQS, Redis, or even a relational database). It also provides powerful tools for creating, reading, updating and deleting queued jobs.

How to use Laravel Queue?

Laravel queues are used to run time-consuming tasks in a separate process. Here’s an overview of how to use them:

  1. Create a queue job. Following command will create a job class in app/Jobs directory.
php artisan make:job ProcessPostImage
  1. Add the job processing logic. The handle() method is responsible for executing the given processing logic.
<?php
 
namespace App\Jobs;

// ...imports
 
class ProcessPostImage implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 
    public $postImageUrl;
 
    public function __construct($postImageUrl)
    {
        $this->postImageUrl = $postImageUrl;
    }
 
    public function handle()
    {
        // Image processing logic
    }
}

You can inject dependencies to the handle() method. Let's say we have a spearate ImageProcessor service.


use App\Services\ImageProcessor;

public function handle(ImageProcessor $processor)
{
    $processor->processImage($this->postImageUrl);
    // Rest of the code
}
  1. Dispatch the job when necessary.
SendEmailNotification::dispatch($postImageUrl)
  1. Running the worker to start processing the jobs in the queue.
php artisan queue:work

Above command will start the queue worker and process the jobs added to the queue.

So how do we run this command in a production environment? where it should keep running in the background. To do that we have to use a tool like Supervisor.

Setting up Supervisor

First we have to install Supervisor in your server.

sudo apt-get install supervisor

Then we have to create a worker configuration file for the laravel queue worker inside the /etc/supervisor/conf.d directory, This is the default Supervisor configuration files path.

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php artisan queue:work --daemon --tries=3 --max-time=3600
directory=/var/www/html
autostart=true
autorestart=true
user=root
stdout_logfile=/var/log/supervisor/worker.log
stderr_logfile=/var/log/supervisor/worker-error.log

Next we have to start the Supervisor service in order to start processing.

sudo supervisorctl reread
 
sudo supervisorctl update
 
sudo supervisorctl start all

// If you want start only one queue, you can provide the queue name to the above command.
sudo supervisorctl start queue-name

Benefits of using Laravel Queue

  1. Performance: Queueing allows your application to process multiple tasks faster, as it offloads tasks from the main application. It also allows you to scale performance when the application traffic increases.

  2. Reliability: Queueing ensures that tasks will be executed, even if the main application experiences errors or downtime.

  3. Flexibility: Laravel queues provide a unified API across different queue backends, which makes it easier to switch between queue services if needed.

Conclusion

Laravel queues are a powerful and flexible tool for running time-consuming tasks in the background. They provide a unified API across a variety of different queue backends, which makes it easier to switch between queue services if needed. Queuing can improve the performance and scalability of applications, as well as ensure that tasks are executed with reliability.

There are many more options in queues API in Laravel and you can read more about with following links.

https://laravel.com/docs/9.x/queues

https://divinglaravel.com/queues