This sample demonstrates the fan-out/fan-in pattern with Azure Durable Task Scheduler using the JavaScript SDK.
In this sample:
- The orchestrator receives a list of work items as input.
- It fans out by scheduling
processWorkItemactivities in parallel. - It waits for all activity tasks to complete using
whenAll. - It fans in by calling
aggregateResultsto compute totals. - The aggregated result is returned to the client.
This pattern is useful for:
- Parallel batch processing
- Workload distribution across independent tasks
- Aggregating results from concurrent activities
- Node.js 22+
- Docker (for running the emulator)
- Azure CLI (optional, if using a deployed Durable Task Scheduler)
The sample checks for deployed scheduler settings in environment variables. If none are present, it uses the local emulator.
-
From the repository root, navigate to this sample:
cd samples/durable-task-sdks/javascript/fan-out-fan-in -
Pull the emulator image:
docker pull mcr.microsoft.com/dts/dts-emulator:latest
-
Run the emulator:
docker run --name dtsemulator -d -p 8080:8080 -p 8082:8082 mcr.microsoft.com/dts/dts-emulator:latest
Default local settings used by this sample:
- Endpoint:
http://localhost:8080 - Task hub:
default
For local development against deployed Durable Task Scheduler resources:
-
Install the Durable Task CLI extension:
az upgrade az extension add --name durabletask --allow-preview true -
Create a resource group in a supported region:
az provider show --namespace Microsoft.DurableTask --query "resourceTypes[?resourceType=='schedulers'].locations | [0]" --out table az group create --name my-resource-group --location <location>
-
Create a scheduler and task hub:
az durabletask scheduler create \ --resource-group my-resource-group \ --name my-scheduler \ --ip-allowlist '["0.0.0.0/0"]' \ --sku-name Dedicated \ --sku-capacity 1 az durabletask taskhub create \ --resource-group my-resource-group \ --scheduler-name my-scheduler \ --name my-taskhub -
Assign yourself
Durable Task Data Contributoron the task hub scope:subscriptionId=$(az account show --query "id" -o tsv) loggedInUser=$(az account show --query "user.name" -o tsv) az role assignment create \ --assignee $loggedInUser \ --role "Durable Task Data Contributor" \ --scope "/subscriptions/$subscriptionId/resourceGroups/my-resource-group/providers/Microsoft.DurableTask/schedulers/my-scheduler/taskHubs/my-taskhub"
-
Set environment variables in each terminal where you run
workerorclient:export ENDPOINT=$(az durabletask scheduler show \ --resource-group my-resource-group \ --name my-scheduler \ --query "properties.endpoint" \ --output tsv) export TASKHUB="my-taskhub"
-
Install dependencies:
npm install
-
Start the worker:
npm run worker
-
In a separate terminal, run the client:
npm run client
Optionally, pass the number of work items:
npm run client -- 15
When you run the sample, output is generated from both processes.
The worker shows:
- Worker startup and scheduler connection details
- Parallel execution of
processWorkItemactivities - Aggregation via
aggregateResults
The client shows:
- Scheduled orchestration instance ID
- Final orchestration runtime status
- Aggregated output JSON (total items, sum, average, and per-item results)
You can inspect orchestration history in the dashboard:
- Open
http://localhost:8082 - Select the
defaulttask hub - Open the orchestration instance to inspect activity fan-out and aggregation details
This sample includes an azure.yaml file to deploy both worker and client to Azure Container Apps.
Note: This sample uses the shared infrastructure templates at
samples/infra/.
-
Install Azure Developer CLI
-
Authenticate:
azd auth login
-
From this sample directory, initialize once:
azd init
-
Provision infrastructure and deploy:
azd up
This command provisions shared infrastructure (including Durable Task Scheduler and Azure Container Apps), builds container images, and deploys both services.
-
After deployment, monitor execution using:
- Azure Portal Container App log streams for
workerandclient - Durable Task Scheduler dashboard URL from the deployed task hub
- Azure Portal Container App log streams for