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.
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.
1use orcher::prelude::*;2use anyhow::Result;3use serde::{Deserialize, Serialize};45#[derive(Debug, Clone, Serialize, Deserialize)]6struct BatchInput {7 job_id: String,8 items: Vec<String>,9}1011#[derive(Debug, Clone, Serialize, Deserialize)]12struct ItemResult {13 item_id: String,14 processed: bool,15 value: i32,16}1718#[derive(Debug, Clone, Serialize, Deserialize)]19struct BatchResult {20 job_id: String,21 total_items: usize,22 total_value: i32,23 results: Vec<ItemResult>,24}2526// Task to process a single item27#[task(name = "process_item")]28pub async fn process_item(29 _ctx: TaskContext,30 item_id: String,31) -> Result<ItemResult> {32 let value = item_id.len() as i32 * 10;3334 Ok(ItemResult {35 item_id,36 processed: true,37 value,38 })39}4041// Basic fan-out/fan-in workflow42#[workflow(name = "batch_processor", version = "1.0.0")]43pub 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());4849 // Fan-Out: Start processing all items in parallel50 let mut task_futures = Vec::new();5152 for item in &input.items {53 let task = ctx.execute_task(process_item, item.clone());54 task_futures.push(task);55 }5657 // Fan-In: Wait for all tasks to complete58 let mut results = Vec::new();59 let mut total_value = 0;6061 for task in task_futures {62 let result: ItemResult = task.await?;63 total_value += result.value;64 results.push(result);65 }6667 println!("Batch processing complete: {} items, total value: {}",68 results.len(), total_value);6970 Ok(BatchResult {71 job_id: input.job_id,72 total_items: results.len(),73 total_value,74 results,75 })76}Next Steps
Rate-Limited Fan-Out
Control concurrency to avoid overwhelming downstream systems
Partial Failure Handling
Continue processing when individual items fail
Dynamic Fan-Out
Determine parallelism from runtime data
Nested Fan-Out/Fan-In
Hierarchical parallel processing with child workflows
Best Practices & Performance
Guidelines, performance tuning, and real-world use cases