Parent-Child Orchestration
Parent-child orchestration is a hierarchical pattern where a parent workflow spawns and manages one or more child workflows. Each child runs independently with its own execution context, state, and lifecycle, while the parent coordinates their overall behavior. This pattern lets you build complex systems from simple, reusable building blocks — different teams can own different child workflows, each child has isolated error handling, and you can run hundreds of children in parallel. Use parent-child orchestration when you need to break down complexity, coordinate multiple services, process items in parallel, or enable independent versioning of workflow components.
Basic Parent-Child Communication
The simplest parent-child pattern spawns a child workflow, passes input data, and waits for the result. The parent calls start_child_workflow (or startChildWorkflow in TypeScript) with the child's name and input, receiving a handle that can be awaited for the child's return value. This decouples the parent from the child's internal implementation — the parent only needs to know the child's name and its input/output types.
1use orcher::prelude::*;2use anyhow::Result;3use serde::{Deserialize, Serialize};45// Child workflow - validates an order6#[workflow(name = "validate_order", version = "1.0.0")]7pub async fn validate_order(8 ctx: WorkflowContext,9 order: OrderData,10) -> Result<ValidationResult> {11 println!("Validating order {}", order.id);1213 // Validate customer14 let customer = ctx.execute_task(check_customer, order.customer_id).await?;1516 // Validate inventory17 let inventory = ctx.execute_task(check_inventory, order.items.clone()).await?;1819 // Validate pricing20 let pricing = ctx.execute_task(check_pricing, order.total).await?;2122 Ok(ValidationResult {23 valid: customer.valid && inventory.available && pricing.correct,24 errors: vec![],25 })26}2728// Parent workflow - orchestrates order processing29#[workflow(name = "process_order", version = "1.0.0")]30pub async fn process_order(31 ctx: WorkflowContext,32 order: OrderData,33) -> Result<OrderResult> {34 println!("Processing order {}", order.id);3536 // Start validation child workflow37 let validation_handle = ctx38 .start_child_workflow("validate_order", order.clone())39 .await?;4041 println!("Validation workflow started: {}", validation_handle.id());4243 // Wait for validation result44 let validation = validation_handle.result::<ValidationResult>().await?;4546 if !validation.valid {47 return Ok(OrderResult::rejected(order.id, validation.errors));48 }4950 // Continue with payment processing51 let payment = ctx.execute_task(process_payment, order.total).await?;5253 Ok(OrderResult::success(order.id, payment.transaction_id))54}Start the parent workflow:
1use orcher::client::OrcherClient;23#[tokio::main]4async fn main() -> Result<()> {5 let client = OrcherClient::new("localhost:50051").await?;67 let order = OrderData {8 id: "ord-123".to_string(),9 customer_id: "cust-456".to_string(),10 items: vec!["item-1".to_string(), "item-2".to_string()],11 total: 99.99,12 };1314 let handle = client15 .start_workflow("process_order", "ord-123", order)16 .await?;1718 println!("Order processing started: {}", handle.id());1920 let result = handle.result::<OrderResult>().await?;21 println!("Order result: {:?}", result);2223 Ok(())24}Next Steps
Dynamic Spawning
Spawn child workflows dynamically based on runtime data for fan-out patterns
Event Coordination
Send events between parent and child workflows for runtime control
Hierarchical Workflows
Build multi-level orchestration hierarchies for complex business processes
Error Handling & Monitoring
Strategies for handling child failures and monitoring execution trees
Map-Reduce Pattern
Split large datasets into batches, process in parallel, and aggregate results
Best Practices & Performance
Guidelines for designing efficient parent-child hierarchies