Hi guys,
Laravel Queue is one of the greatest features that Laravel ships for us.
DummyTechDev will share with you all the details and real-life examples so that you can understand why Queue is so good!
Why do we need to use Laravel Queue?
Let’s see, a simple HTTP request that many applications out there have: Sign Up.
After creating an account for a user, ideally, we should send either:
- Welcome email mail
- Email verification mail
Sending Email is a heavy task (it can consume around 1.5~60 seconds) because it interacts with the third-party Email service (SMTP or API, both are the same).
A good sign-up process shouldn’t take more than 1 second, otherwise, we’ll have a bad User Experience (UX) and cause frustration to the user (which is not good).
Therefore, Laravel Queue is here to save the day!
Laravel Queue for dummies
Introduction
Laravel Queue provides us with an intuitive way to delegate heavy tasks to the background workers.
With the example above, instead of sending email directly in the HTTP layer, we can tell Laravel to delegate the Send Email task to the background.
Therefore, the sign-up request will be less than 1 second (which makes users happy)
When the background workers receive the Send Email task, it will automatically pick up the task and send out an email for you, using CLI mode (powered by PHP-CLI which is way more powerful than the PHP-FPM)
Pretty cool, right?
How to implement it?
Configure the Driver
There are plenty of drivers that we can use in the Laravel codebase, for this example, I’ll use database
driver (built-in and no other dependencies).
On production, we usually use either SQS or Redis, since it’s better for distribution and provides us with more functionalities.
Open the .env
and find the QUEUE_CONNECTION
QUEUE_CONNECTION=database
Set it to database
and we’re all good to go next!
Create a Job
Let’s create a SendWelcomeEmailJob
in app/Jobs
:
<?php
namespace App\Jobs;
use Illuminate\Foundation\Queue\Queueable;
use App\Models\User;
class SendWelcomeEmailJob implements ShouldQueue
{
use Queueable;
public function __construct(public User $user)
{
}
public function handle(): void
{
// for example, using Mail facade to send out email
Mail::send(); //
}
}
Notes:
- We must implement the
ShouldQueue
interface from Laravel use Queueable
is optional, it helps you to interact with the Queue
Push the Job to Queue (aka Dispatch the Job)
In the AuthController::signUp
method, let’s dispatch the job instead of sending email directly:
public function signUp(): void
{
$user = User::create([...]);
SendWelcomeEmail::dispatch($user);
// response ok
}
Start the Queue Worker
Use this command:
php artisan queue:work
It will start the Queue Worker and be ready to pick up your tasks.
Now we can try to sign up and see the results!
How does it work in the Staging or Production environment?
For Server
With a server, we’ll need to use supervisor
to manage our queue:work
process.
The supervisor helps us manage the process (start, observe, restart, etc) and also dispatch multiple processes.
To learn more about how to start it, check out: Laravel Queue Supervisor
For Serverless
We can run Laravel in serverless mode (which is nice). To deploy Laravel Queue Workers using Serverless, we can use:
- Laravel Vapor (Subscription-required)
- Bref
Both are using SQS. When SQS receives messages, it will automatically send the message to the AWS Lambda to handle.
With this, we don’t have to run the queue:work
by ourselves and the workers are unlimited (AWS Lambda charges us by the number of requests).
Real-life examples
- Send verification email
- Send welcome email
- Pull data from external services
- Sync data to external services
- Generate an Image
- Upload files to S3
- etc
Conclusion
Well, that’s the basic info about Laravel Queue for dummies.
DummyTechDev hopes this article gives you the overall overview and how it works under the hood. So that you can know and be proactive on the approaches in your projects.
Happy coding!