Don't Block Your Users! Leverage Laravel Queues for a Smooth User Experience
Jul
06

Don't Block Your Users! Leverage Laravel Queues for a Smooth User Experience



Laravel's queue system is a powerful tool that allows you to offload time-consuming tasks from your web application's main request cycle. This enhances responsiveness and overall user experience by keeping your application snappy while lengthy processes run in the background.

Understanding the Players: Jobs, Queues, and Workers

  • Jobs: are the actual tasks that you want to run in the background. They are dispatched to the queue and executed by a queue worker at a later time. They're typically encapsulated in classes extending the Illuminate\Contracts\Queue\ShouldQueue interface. These job classes define the logic for your background tasks.
  • Queues: Think of them as task lists. Laravel supports various queue drivers like Redis, Beanstalkd, Amazon SQS, and even the database. You can push jobs onto these queues, and worker processes will handle them.
  • Workers: These are the background processes that consume jobs from the queue and execute them. Laravel provides a queue:work command to run a worker process. You can also configure multiple worker processes for increased throughput.

Why Use Queues?

  1. Improved Performance: By offloading time-consuming tasks to the background, your application can respond to requests more quickly.
  2. Scalability: Queues can help distribute the load across multiple workers, making it easier to scale your application.
  3. Fault Tolerance: Queues can be used to retry failed jobs, ensuring that tasks are eventually completed even if they fail initially.

Setting Up Queues in Laravel

Laravel supports a variety of queue drivers, including database, Redis, and Amazon SQS. For this example, we will use the database driver.

Step 1: Configure the Queue Driver

First, update your .env file to set the queue driver to database:

QUEUE_CONNECTION=database

Then, update your config/queue.php configuration file to ensure the database connection is set:

'connections' => [
    'database' => [
        'driver' => 'database',
        'table' => 'jobs',
        'queue' => 'default',
        'retry_after' => 90,
    ],
],

Step 2: Create the Jobs Table

Run the queue job table migration to create the jobs table:

php artisan queue:table
php artisan migrate

Step 3: Generate the Mailable Class

Run the following Artisan command to create a new mailable class named WelcomeMail:

php artisan make:mail WelcomeMail

Step 4: Edit the Mailable Class

After running the command, Laravel will generate a new mailable class in the app/Mail directory. Open the generated WelcomeMail.php file and modify the build method to define the email content:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class WelcomeMail extends Mailable
{
    use Queueable, SerializesModels;

    protected $user;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($user)
    {
        $this->user = $user;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.welcome')
                    ->with(['user' => $this->user])
                    ->subject('Welcome to Our Application');
    }
}

Step 5: Create the Email Blade View

Next, create a blade view file named welcome.blade.php in the resources/views/emails directory (create the emails directory if it doesn't exist already). This view will contain the HTML and content of your welcome email. For example:

resources/views/emails/welcome.blade.php:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Welcome to Our Application</title>
</head>
<body>
    <h1>Welcome, {{ $user->name }}!</h1>
    <p>Thank you for joining our application. We're excited to have you on board!</p>
</body>
</html>

Step 6: Creating a Job

Next, create a job class using the make:job Artisan command. For example, let's create a job to send a welcome email:

php artisan make:job SendWelcomeEmail

This command will create a new job class in the app/Jobs directory. Here is a basic implementation of the SendWelcomeEmail job:

<?php

namespace App\Jobs;

use App\Mail\WelcomeMail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;

class SendWelcomeEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $user;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($user)
    {
        $this->user = $user;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Mail::to($this->user->email)->send(new WelcomeMail($this->user));
    }
}

Step 7: Dispatching the Job

You can dispatch the job from anywhere in your application. For example, let's dispatch the job when a new user registers:

use App\Jobs\SendWelcomeEmail;
use App\Models\User;

Route::post('/register', function (Request $request) {
    $user = User::create($request->all());

    SendWelcomeEmail::dispatch($user);

    return response('User registered and welcome email sent.');
});

Step 8: Running the Queue Worker

To start processing jobs, you need to run the queue worker. Use the queue:work Artisan command:

php artisan queue:work

This command will start processing jobs from the specified queue.

Step 9: Handling Failed Jobs

Laravel provides a simple way to handle failed jobs. You can configure the failed_jobs table to log failed jobs:

php artisan queue:failed-table
php artisan migrate

You can also specify the number of times a job should be retried before it is considered failed by setting the tries property on the job class:

public $tries = 5;

Conclusion

Laravel's queue system is a powerful tool for offloading time-consuming tasks to the background, improving the performance and responsiveness of your application. By following the steps outlined in this post, you can easily set up and start using queues and jobs in your Laravel application. Whether you're sending emails, processing video uploads, or performing any other time-consuming task, Laravel's queue system has you covered.

Happy coding!

Contact

Get in touch with us

Feel free to request missing tools or give some feedback.

Contact Us