Azure Durable Functions: Fan-Out/Fan-In Pattern

The Fan-Out/Fan-In pattern allows you to execute tasks in parallel and then aggregate the results. This is famously difficult in standard serverless, but trivial with Durable Functions.

The Orchestrator

[FunctionName("BackupOrchestrator")]
public static async Task RunOrchestrator(
    [OrchestrationTrigger] IDurableOrchestrationContext context)
{
    var files = await context.CallActivityAsync<string[]>("GetFileList", null);
    
    var tasks = new List<Task<long>>();
    foreach (var file in files)
    {
        // Fan-Out: Start all tasks immediately
        tasks.Add(context.CallActivityAsync<long>("BackupFile", file));
    }
    
    // Fan-In: Wait for all to complete
    long[] results = await Task.WhenAll(tasks);
    
    long totalBytes = results.Sum();
    await context.CallActivityAsync("ReportStatus", totalBytes);
}

How it Scales

graph TB
    Orch[Orchestrator] -->|Schedule| Act1[Activity 1]
    Orch -->|Schedule| Act2[Activity 2]
    Orch -->|Schedule| Act3[Activity 3]
    
    Act1 -.->|Result| Orch
    Act2 -.->|Result| Orch
    Act3 -.->|Result| Orch
    
    style Orch fill:#E1F5FE

Key Takeaways

  • The Orchestrator function replays from the start after every `await`. Avoid non-deterministic code (like `DateTime.Now`) inside the orchestrator logic.
  • Activities run on separate VMs, scaling massively.

Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.