Kritim Yantra
Mar 01, 2025
In modern web applications, running tasks one after the other can sometimes slow things down—especially if those tasks are independent of one another. Laravel’s new Concurrency facade makes it easier to run multiple tasks at the same time, giving your application a performance boost without a lot of extra hassle.
Laravel Concurrency is a new feature (currently in beta) that allows you to execute several slow or independent tasks concurrently. Instead of waiting for one task to finish before starting the next, you can run them simultaneously. This can be especially useful when you need to run multiple database queries, API calls, or other time-consuming operations.
Laravel handles concurrency by doing a couple of clever things behind the scenes:
This means that even if you’re running multiple tasks at the same time, they don’t block one another.
If you’re upgrading from Laravel 10.x to Laravel 11.x or 12.x, you may need to add the ConcurrencyServiceProvider
to your configuration. Open your config/app.php
file and add the following line to the providers array:
'providers' => ServiceProvider::defaultProviders()->merge([
/*
* Package Service Providers...
*/
Illuminate\Concurrency\ConcurrencyServiceProvider::class,
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
])->toArray(),
Laravel’s Concurrency facade supports three different drivers:
composer require spatie/fork
To run tasks concurrently, use the run
method provided by the Concurrency facade. For example, if you want to count the number of users and orders simultaneously, you can write:
use Illuminate\Support\Facades\Concurrency;
use Illuminate\Support\Facades\DB;
[$userCount, $orderCount] = Concurrency::run([
fn () => DB::table('users')->count(),
fn () => DB::table('orders')->count(),
]);
If you want to choose a specific driver, simply chain the driver
method:
$results = Concurrency::driver('fork')->run([
fn () => DB::table('users')->count(),
fn () => DB::table('orders')->count(),
]);
Sometimes, you might have tasks that don’t need to block your user’s request. For example, if you want to log metrics or send emails after the user receives a response, you can “defer” these tasks. With the defer
method, Laravel will run your tasks in the background once the HTTP response is sent:
use App\Services\Metrics;
use Illuminate\Support\Facades\Concurrency;
Concurrency::defer([
fn () => Metrics::report('users'),
fn () => Metrics::report('orders'),
]);
This allows your application to respond quickly to user requests while still performing important background tasks.
sync
driver during testing to ensure predictable behavior.Laravel’s Concurrency facade is a powerful new tool that can help you make your applications faster by running independent tasks at the same time. Whether you’re counting database records, making multiple API calls, or deferring non-critical tasks, concurrency can make your application more efficient without adding a lot of extra complexity.
Since this feature is still in beta, experimenting with it on non-critical parts of your application is a great way to get comfortable with concurrent programming in Laravel. As you gain more experience, you’ll be able to harness the full power of concurrency to build more responsive and robust applications.
Happy coding!
No comments yet. Be the first to comment!
Please log in to post a comment:
Continue with Google