Why Use Sub-workflows?
Modularity
Break complex workflows into smaller, manageable pieces
Reusability
Use the same workflow logic in multiple parent workflows
Separation of Concerns
Keep each workflow focused on a single responsibility
Independent Scaling
Scale parent and child workflows independently
Creating Workflow Stubs
UsenewWorkflow() to create a stub for calling another workflow:
Synchronous Calls
Call child workflows synchronously when you need the result immediately:Asynchronous Calls
Usedispatch() to call child workflows asynchronously:
Parallel Sub-workflows
Dispatch multiple child workflows in parallel:Recursive Workflows
Workflows can call themselves recursively:Workflow Stubs with Tags
Create child workflows with specific tags for routing or identification:Workflow Stubs with Metadata
Pass metadata to child workflows for context or tracing:Accessing Existing Workflows
By ID
Get a stub for an existing workflow instance by its ID:By Tag
Get a stub for an existing workflow instance by its tag:Combining Services and Sub-workflows
Mix service calls and sub-workflow calls in the same workflow:Lifecycle and Cancellation
When a parent workflow is canceled, all running child workflows are automatically canceled:Complete Example
Here’s a complete example showing a complex workflow orchestration:Best Practices
Design for single responsibility
Design for single responsibility
Each workflow should have a clear, single purpose:
Use parallel execution when possible
Use parallel execution when possible
Dispatch independent child workflows in parallel:
Initialize workflow stubs as properties
Initialize workflow stubs as properties
Create workflow stubs once as class properties:
Use tags for workflow organization
Use tags for workflow organization
Next Steps
Channels
Learn about workflow channels for external signals
Timers
Add delays and timeouts to workflows
Versioning
Update workflows while maintaining compatibility
Calling Services
Learn service orchestration patterns