Guides

Fan-Out/Fan-In Pattern


The Fan-Out/Fan-In pattern splits work across multiple parallel operations (fan-out) and then collects and aggregates their results (fan-in). This maximizes throughput by executing independent operations concurrently — the total latency equals the slowest operation rather than the sum of all operations. Use this pattern when you need to process multiple items in a batch, call multiple APIs simultaneously, aggregate data from distributed sources, run independent validation checks in parallel, or perform map-reduce style dataset transformations.

Workflow Start
fan-out
Op 1
Op 2
Op 3
Op 4
Op 5
fan-in
Aggregate Results
Workflow Complete

Basic Fan-Out/Fan-In

The simplest form of fan-out/fan-in dispatches all tasks at once and waits for every result. Each item is processed independently as a separate task, and the workflow collects all results into a single aggregated response. This approach works well when the number of items is moderate (under a few hundred) and you can tolerate all-or-nothing semantics — if any task fails, the workflow fails.

1
use orcher::prelude::*;
2
use anyhow::Result;
3
use serde::{Deserialize, Serialize};
4
5
#[derive(Debug, Clone, Serialize, Deserialize)]
6
struct BatchInput {
7
job_id: String,
8
items: Vec<String>,
9
}
10
11
#[derive(Debug, Clone, Serialize, Deserialize)]
12
struct ItemResult {
13
item_id: String,
14
processed: bool,
15
value: i32,
16
}
17
18
#[derive(Debug, Clone, Serialize, Deserialize)]
19
struct BatchResult {
20
job_id: String,
21
total_items: usize,
22
total_value: i32,
23
results: Vec<ItemResult>,
24
}
25
26
// Task to process a single item
27
#[task(name = "process_item")]
28
pub async fn process_item(
29
_ctx: TaskContext,
30
item_id: String,
31
) -> Result<ItemResult> {
32
let value = item_id.len() as i32 * 10;
33
34
Ok(ItemResult {
35
item_id,
36
processed: true,
37
value,
38
})
39
}
40
41
// Basic fan-out/fan-in workflow
42
#[workflow(name = "batch_processor", version = "1.0.0")]
43
pub async fn batch_processor(
44
ctx: WorkflowContext,
45
input: BatchInput,
46
) -> Result<BatchResult> {
47
println!("Processing batch {} with {} items", input.job_id, input.items.len());
48
49
// Fan-Out: Start processing all items in parallel
50
let mut task_futures = Vec::new();
51
52
for item in &input.items {
53
let task = ctx.execute_task(process_item, item.clone());
54
task_futures.push(task);
55
}
56
57
// Fan-In: Wait for all tasks to complete
58
let mut results = Vec::new();
59
let mut total_value = 0;
60
61
for task in task_futures {
62
let result: ItemResult = task.await?;
63
total_value += result.value;
64
results.push(result);
65
}
66
67
println!("Batch processing complete: {} items, total value: {}",
68
results.len(), total_value);
69
70
Ok(BatchResult {
71
job_id: input.job_id,
72
total_items: results.len(),
73
total_value,
74
results,
75
})
76
}

Next Steps