Faster pages without a queue: Concurrency::run and Concurrency::defer
Laravel's Concurrency facade runs independent work in parallel PHP processes, or after the response is sent. How it works, which driver to pick, and where it beats a queued job.
- Published
- Updated
- Reading time
- 3 minutes
- Written for
- Laravel 13.x
A dashboard that runs four slow, independent queries one after another takes the sum of all four. Run them side by side and it takes about as long as the slowest one. Laravel's Concurrency facade makes that a few lines of code, no queue workers required.
The basics
use Illuminate\Support\Facades\Concurrency;
use Illuminate\Support\Facades\DB;
[$userCount, $orderCount] = Concurrency::run([
fn () => DB::table('users')->count(),
fn () => DB::table('orders')->count(),
]);You get the results back in the same order as the closures. For anything beyond two values, use named keys so you're not counting array positions:
$stats = Concurrency::run([
'users' => fn () => DB::table('users')->count(),
'orders' => fn () => DB::table('orders')->count(),
'revenue' => fn () => DB::table('orders')->sum('total'),
]);
$stats['revenue'];What's actually happening
This isn't threads. Laravel serializes each closure, hands it to a hidden Artisan command running in its own PHP process, then serializes the return value back to the parent.
That has three practical consequences:
- Every closure boots its own process. There's real overhead per task, so this pays off for slow work (heavy queries, HTTP calls to other services), not for things that take a few milliseconds.
- Closures and results must be serializable. Return plain values or arrays, not open resources or connections.
- Each child has its own state. Database connections, singletons and in-memory caches aren't shared with the parent request.
Picking a driver
There are three drivers:
process(the default) works everywhere, including web requests.forkis faster but only works on the CLI, because PHP can't fork during a web request. It needscomposer require spatie/fork. A good fit for Artisan commands and scheduled tasks.syncruns everything sequentially in the parent process. Use it in tests.
$results = Concurrency::driver('fork')->run([...]);To change the default, publish the config file and edit default:
php artisan config:publish concurrencyPut a timeout on it
With the process driver you can cap how long a task may run:
Concurrency::run([
fn () => DB::table('users')->count(),
fn () => DB::table('orders')->count(),
], timeout: 30);Fire and forget: defer
Sometimes you don't need the results at all. Reporting metrics, warming a cache or pinging a webhook shouldn't make the user wait. defer runs the closures concurrently after the HTTP response has been sent:
use App\Services\Metrics;
use Illuminate\Support\Facades\Concurrency;
Concurrency::defer([
fn () => Metrics::report('users'),
fn () => Metrics::report('orders'),
]);Concurrency or a queued job?
My rule of thumb:
| Situation | Reach for |
|---|---|
| You need the results to render this response | Concurrency::run |
| The work is small, best-effort, and can be lost on a crash | Concurrency::defer |
| The work must happen, needs retries, or takes more than a few seconds | A queued job |
defer runs inside the same request lifecycle as the web process. If the server restarts at the wrong moment, the work is gone. Anything that matters, like sending an invoice, belongs on a queue with retries.
Testing
Switch to the sync driver in tests so everything runs inline and predictably. Then your assertions don't depend on child processes. The default driver is read from the CONCURRENCY_DRIVER environment variable, so one line in phpunit.xml does it:
<env name="CONCURRENCY_DRIVER" value="sync"/>Reference: the official concurrency docs.