Concurrency

Introduction

lightbulb

Laravel's Concurrency facade is currently in beta while we gather community feedback.

Sometimes you may need to execute several slow tasks which do not depend on one another. In many cases, significant performance improvements can be realized by executing the tasks concurrently. Laravel's Concurrency facade provides a simple, convenient API for executing closures concurrently.

Concurrency Compatibility

If you upgraded to Laravel 11.x from a Laravel 10.x application, you may need to add the ConcurrencyServiceProvider to the providers array in your application's config/app.php configuration file:

1'providers' => ServiceProvider::defaultProviders()->merge([
2 /*
3 * Package Service Providers...
4 */
5 Illuminate\Concurrency\ConcurrencyServiceProvider::class,
6 
7 /*
8 * Application Service Providers...
9 */
10 App\Providers\AppServiceProvider::class,
11 App\Providers\AuthServiceProvider::class,
12 // App\Providers\BroadcastServiceProvider::class,
13 App\Providers\EventServiceProvider::class,
14 App\Providers\RouteServiceProvider::class,
15])->toArray(),
1'providers' => ServiceProvider::defaultProviders()->merge([
2 /*
3 * Package Service Providers...
4 */
5 Illuminate\Concurrency\ConcurrencyServiceProvider::class,
6 
7 /*
8 * Application Service Providers...
9 */
10 App\Providers\AppServiceProvider::class,
11 App\Providers\AuthServiceProvider::class,
12 // App\Providers\BroadcastServiceProvider::class,
13 App\Providers\EventServiceProvider::class,
14 App\Providers\RouteServiceProvider::class,
15])->toArray(),

How it Works

Laravel achieves concurrency by serializing the given closures and dispatching them to a hidden Artisan CLI command, which unserializes the closures and invokes it within its own PHP process. After the closure has been invoked, the resulting value is serialized back to the parent process.

The Concurrency facade supports three drivers: process (the default), fork, and sync.

The fork driver offers improved performance compared to the default process driver, but it may only be used within PHP's CLI context, as PHP does not support forking during web requests. Before using the fork driver, you need to install the spatie/fork package:

1composer require spatie/fork
1composer require spatie/fork

The sync driver is primarily useful during testing when you want to disable all concurrency and simply execute the given closures in sequence within the parent process.

Running Concurrent Tasks

To run concurrent tasks, you may invoke the Concurrency facade's run method. The run method accepts an array of closures which should be executed simultaneously in child PHP processes:

1use Illuminate\Support\Facades\Concurrency;
2use Illuminate\Support\Facades\DB;
3 
4[$userCount, $orderCount] = Concurrency::run([
5 fn () => DB::table('users')->count(),
6 fn () => DB::table('orders')->count(),
7]);
1use Illuminate\Support\Facades\Concurrency;
2use Illuminate\Support\Facades\DB;
3 
4[$userCount, $orderCount] = Concurrency::run([
5 fn () => DB::table('users')->count(),
6 fn () => DB::table('orders')->count(),
7]);

To use a specific driver, you may use the driver method:

1$results = Concurrency::driver('fork')->run(...);
1$results = Concurrency::driver('fork')->run(...);

Or, to change the default concurrency driver, you should publish the concurrency configuration file via the config:publish Artisan command and update the default option within the file:

1php artisan config:publish concurrency
1php artisan config:publish concurrency

Deferring Concurrent Tasks

If you would like to execute an array of closures concurrently, but are not interested in the results returned by those closures, you should consider using the defer method. When the defer method is invoked, the given closures are not executed immediately. Instead, Laravel will execute the closures concurrently after the HTTP response has been sent to the user:

1use App\Services\Metrics;
2use Illuminate\Support\Facades\Concurrency;
3 
4Concurrency::defer([
5 fn () => Metrics::report('users'),
6 fn () => Metrics::report('orders'),
7]);
1use App\Services\Metrics;
2use Illuminate\Support\Facades\Concurrency;
3 
4Concurrency::defer([
5 fn () => Metrics::report('users'),
6 fn () => Metrics::report('orders'),
7]);

Comments

No Comments Yet

“Laravel” is a Trademark of Taylor Otwell.
The source documentation is released under MIT license. See laravel/docs on GitHub for details.
The translated documentations are released under MIT license. See cornch/laravel-docs-l10n on GitHub for details.