Docs / Digging Deeper / Concurrency
Concurrency
Introduction
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. Laraflare's Concurrency facade provides a simple, convenient API for executing functions concurrently.
How it Works
The Concurrency facade supports three drivers: process (the default), fork, and sync.
Note
A Worker cannot start child processes or fork, so the
processandforkdrivers run the given functions side by side in the same isolate, each one started at once and awaited together. The results keep the same keys as the tasks. Theforkdriver may only be used within the console, never during a web request.
The sync driver is primarily useful during testing when you want to disable all concurrency and simply execute the given functions in sequence.
Running Concurrent Tasks
To run concurrent tasks, you may invoke the Concurrency facade's run method. The run method accepts an array of functions which should be executed simultaneously, and returns a promise for their results:
import { Concurrency, DB } from '@laraflare/framework';
const [userCount, orderCount] = await Concurrency.run([
() => DB.table('users').count(),
() => DB.table('orders').count(),
]);
To use a specific driver, you may use the driver method:
const results = await 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:
npx artisan config:publish concurrency
Named Results
If you would like to access concurrent task results by name rather than by position, you may provide an object of functions. Each result will be returned using the same key as its corresponding function:
import { Concurrency, DB } from '@laraflare/framework';
const results = await Concurrency.run({
users: () => DB.table('users').count(),
orders: () => DB.table('orders').count(),
});
const userCount = results.users;
const orderCount = results.orders;
Task Timeouts
You may specify a maximum number of seconds the concurrent tasks are allowed to run before they are abandoned by providing a timeout as the second argument to the run method:
import { Concurrency, DB } from '@laraflare/framework';
const [userCount, orderCount] = await Concurrency.run([
() => DB.table('users').count(),
() => DB.table('orders').count(),
], 30);
You may also provide an interval if you prefer a more expressive timeout definition:
import { Concurrency, seconds } from '@laraflare/framework';
await Concurrency.run([...], seconds(30));
Deferring Concurrent Tasks
If you would like to execute an array of functions concurrently, but are not interested in the results returned by those functions, you should consider using the defer method. When the defer method is invoked, the given functions are not executed immediately. Instead, Laraflare will execute the functions concurrently after the HTTP response has been sent to the user:
import { Concurrency } from '@laraflare/framework';
import { Metrics } from '../Services/Metrics';
Concurrency.defer([
() => Metrics.report('users'),
() => Metrics.report('orders'),
]);