Queues in Laravel

Sometimes individual process in a workflow takes longer time & increases the response time of the service.

Apps such as sending emails, payment gateway, online ordering systems, import jobs, etc.

Just imagine if your app or website has massive traffic, using queues will resolve the problem. And this will make your service fast for the user.

In order to work with queues in Laravel, you need to have knowledge of the following few commands:

Create a queue handler

php artisan make:job ProcessSendingEmail

Send a new event to the queue

ProcessSendingEmail::dispatch($user)

And run the handler for all events

php artisan queue:work

Let’s jump deeper, queues consist of two main components

  • queue servers
  • handler

The queue server keeps a list of messages (or tasks, queues) sent by the application process. A task is simply information about what needs to be done and how.

A handler (or worker) is a part of the main program, that receives new messages from the queue and performs the appropriate actions.

Example

Taking an example of the most common feature (Signup form), let’s suppose if Signup form contains 2 fields e-mail & password along with a button.

When the user presses the register button after entering the email and password, the service will validate the input, then it will verify if a user is not already registered, service will store the user & send the verification email to the given user email, to do so we two options:

  • send synchronously within a single request.
  • or add a task to send an email to the queue.

By looking at the possible solution, which depends on your app/service/web, if you are running a small app; that doesn’t have huge traffic. In this case, the synchronous method would be the fastest way.

But regardless of the volume of the app/website, if you have large traffic & registration request the queue would be the best way of handling it.

Developing a queue setup is simple and easy, start by creating Job.

Create a queue handler

RUN php artisan make:job ProcessSendingEmail

All default handlers are created in app/Jobs. Open the created app/Jobs/ProcessSendingEmail.php file and update the handle function:

public function handle(User $user)                             
{                                                               
    Mail::send(
        'mail.confirm-registration',
        [                    
            'html' => 'Confirm your email using the link - https://danyal.dk'                                               
        ],
        function ($message) use ($user) {                       
            $message->to($user->email)->subject('Confirmation of registration');                                                   
        }
    );                                                         
}

Now our handler receives the user model and sends him a letter using the standard Mail package in Laravel, read more in the documentation.

Send a new event to the queue

public function register(Request $request)                             
{                                                               
    // code                                              
    ProcessSendingEmail::dispatch($user);                      
    // code                                                       
}

After calling the dispatch function, a new event will immediately fly to our queue, and we just have to run our queue handler and wait for the email to be sent to the user.

Run the handler for queued jobs

php artisan queue:work

After the handler is launched, all our queued jobs will be processed one by one, obviously if we have many jobs queued it will take time, but to speed it up we have the option of running multiple processes by using Supervisor. Read more details at Running Multiple Queue Workers and our emails will be sent in parallel. Finally, we must be careful when choosing to run the number of handlers parallelly, because each handler will consume the server resources.

There is always a number of possibilities to do one task, but I always use the best match for my solution.

Keep reading and sharing.

Author: Danyal
I'm working as programmer since 2002. Born, grew up, studied and worked in Pakistan now Im located in Denmark since 2006.