Guides

Nested Fan-Out/Fan-In


Nested Fan-Out/Fan-In

For hierarchical data — such as regions containing stores, or categories containing products — you can nest fan-out operations. The parent workflow fans out across the top-level groups (e.g., regions), and each group is processed by a child workflow that itself fans out across its members (e.g., stores). Results bubble up through the hierarchy: stores report to regions, regions aggregate into the final report. Use child workflows for the outer fan-out so each group gets independent error handling and its own journal.

Parent Workflow
generate_report
fan-out by region
Region A
child workflow
Store 1
Store 2
Store 3
Region B
child workflow
Store 1
Store 2
Store 3
Region C
child workflow
Store 1
Store 2
Store 3
fan-in
Aggregate Report
1
use orcher::prelude::*;
2
use anyhow::Result;
3
4
#[derive(Debug, Clone, Serialize, Deserialize)]
5
struct Region {
6
region_id: String,
7
stores: Vec<String>,
8
}
9
10
#[derive(Debug, Clone, Serialize, Deserialize)]
11
struct RegionalData {
12
region_id: String,
13
store_count: usize,
14
total_sales: f64,
15
}
16
17
#[derive(Debug, Clone, Serialize, Deserialize)]
18
struct ReportResult {
19
total_regions: usize,
20
total_stores: usize,
21
total_sales: f64,
22
}
23
24
// Task to get a single store's sales
25
#[task(name = "get_store_sales")]
26
pub async fn get_store_sales(
27
_ctx: TaskContext,
28
store_id: String,
29
) -> Result<f64> {
30
// Fetch sales data for this store
31
Ok(store_id.len() as f64 * 1000.0)
32
}
33
34
// Child workflow: Level 2 fan-out for stores in a region
35
#[workflow(name = "process_region", version = "1.0.0")]
36
pub async fn process_region(
37
ctx: WorkflowContext,
38
region: Region,
39
) -> Result<RegionalData> {
40
// Level 2 Fan-Out: Process each store in parallel
41
let mut store_tasks = Vec::new();
42
43
for store in &region.stores {
44
let task = ctx.execute_task(get_store_sales, store.clone());
45
store_tasks.push(task);
46
}
47
48
// Level 2 Fan-In: Aggregate store sales
49
let mut total_sales = 0.0;
50
51
for task in store_tasks {
52
let sales: f64 = task.await?;
53
total_sales += sales;
54
}
55
56
Ok(RegionalData {
57
region_id: region.region_id,
58
store_count: region.stores.len(),
59
total_sales,
60
})
61
}
62
63
// Parent workflow: Level 1 fan-out across regions
64
#[workflow(name = "regional_report", version = "1.0.0")]
65
pub async fn regional_report(
66
ctx: WorkflowContext,
67
regions: Vec<Region>,
68
) -> Result<ReportResult> {
69
println!("Generating report for {} regions", regions.len());
70
71
// Level 1 Fan-Out: Process each region as a child workflow
72
let mut region_handles = Vec::new();
73
74
for region in &regions {
75
let handle = ctx
76
.start_child_workflow("process_region", region.clone())
77
.await?;
78
region_handles.push(handle);
79
}
80
81
// Level 1 Fan-In: Collect regional results
82
let mut total_stores = 0;
83
let mut total_sales = 0.0;
84
85
for handle in region_handles {
86
let regional_data: RegionalData = handle.result().await?;
87
total_stores += regional_data.store_count;
88
total_sales += regional_data.total_sales;
89
}
90
91
println!("Report complete: {} regions, {} stores, ${:.2} total sales",
92
regions.len(), total_stores, total_sales);
93
94
Ok(ReportResult {
95
total_regions: regions.len(),
96
total_stores,
97
total_sales,
98
})
99
}

Next Steps