Workflow Orchestration Patterns
Master workflow orchestration architecture with Temporal, covering fundamental design decisions, resilience patterns, and best practices for building reliable distributed systems.
Use this skill when
- Working on workflow orchestration patterns tasks or workflows
- Needing guidance, best practices, or checklists for workflow orchestration patterns
Do not use this skill when
- The task is unrelated to workflow orchestration patterns
- You need a different domain or tool outside this scope
Instructions
- Clarify goals, constraints, and required inputs.
- Apply relevant best practices and validate outcomes.
- Provide actionable steps and verification.
- If detailed examples are required, open
resources/implementation-playbook.md.
When to Use Workflow Orchestration
Ideal Use Cases (Source: docs.temporal.io)
- Multi-step processes spanning machines/services/databases
- Distributed transactions requiring all-or-nothing semantics
- Long-running workflows (hours to years) with automatic state persistence
- Failure recovery that must resume from last successful step
- Business processes: bookings, orders, campaigns, approvals
- Entity lifecycle management: inventory tracking, account management, cart workflows
- Infrastructure automation: CI/CD pipelines, provisioning, deployments
- Human-in-the-loop systems requiring timeouts and escalations
When NOT to Use
- Simple CRUD operations (use direct API calls)
- Pure data processing pipelines (use Airflow, batch processing)
- Stateless request/response (use standard APIs)
- Real-time streaming (use Kafka, event processors)
Critical Design Decision: Workflows vs Activities
The Fundamental Rule (Source: temporal.io/blog/workflow-engine-principles):
- Workflows = Orchestration logic and decision-making
- Activities = External interactions (APIs, databases, network calls)
Workflows (Orchestration)
Characteristics:
- Contain business logic and coordination
- MUST be deterministic (same inputs → same outputs)
- Cannot perform direct external calls
- State automatically preserved across failures
- Can run for years despite infrastructure failures
Example workflow tasks:
- Decide which steps to execute
- Handle compensation logic
- Manage timeouts and retries
- Coordinate child workflows
Activities (External Interactions)
Characteristics:
- Handle all external system interactions
- Can be non-deterministic (API calls, DB writes)
- Include built-in timeouts and retry logic
- Must be idempotent (calling N times = calling once)
- Short-lived (seconds to minutes typically)
Example activity tasks:
- Call payment gateway API
- Write to database
- Send emails or notifications
- Query external services
Design Decision Framework
Does it touch external systems? → Activity
Is it orchestration/decision logic? → Workflow
Core Workflow Patterns
1. Saga Pattern with Compensation
Purpose: Implement distributed transactions with rollback capability
Pattern (Source: temporal.io/blog/compensating-actions-part-of-a-complete-breakfast-with-sagas):
For each step:
1. Register compensation BEFORE executing
2. Execute the step (via activity)
3. On failure, run all compensations in reverse order (LIFO)
Example: Payment Workflow
- Reserve inventory (compensation: release inventory)
- Charge payment (compensation: refund payment)
- Fulfill order (compensation: cancel fulfillment)
Critical Requirements:
- Compensations must be idempotent
- Register compensation BEFORE executing step
- Run compensations in reverse order
- Handle partial failures gracefully
2. Entity Workflows (Actor Model)
Purpose: Long-lived workflow representing single entity instance
Pattern (Source: docs.temporal.io/evaluate/use-cases-design-patterns):
- One workflow execution = one entity (cart, account, inventory item)
- Workflow persists for entity lifetime
- Receives signals for state changes
- Supports queries for current state
Example Use Cases:
- Shopping cart (add items, checkout, expiration)
- Bank account (deposits, withdrawals, balance checks)
- Product inventory (stock updates, reservations)
Benefits:
- Encapsulates entity behavior
- Guarantees consistency per entity
- Natural event sourcing
3. Fan-Out/Fan-In (Parallel Execution)
Purpose: Execute multiple tasks in parallel, aggregate results
Pattern:
- Spawn child workflows or parallel activities
- Wait for all to complete
- Aggregate results
- Handle partial failures
Scaling Rule (Source: temporal.io/blog/workflow-engine-principles):
- Don't scale individual workflows
- For 1M tasks: spawn 1K child workflows × 1K tasks each
- Keep each workflow bounded
4. Async Callback Pattern
Purpose: Wait for external event or human approval
Pattern:
- Workflow sends request and waits for signal
- Ex
1---
2name: workflow-orchestration-patterns
3description: Design durable workflows with Temporal for distributed systems. Covers workflow vs activity separation, saga patterns, state management, and determinism constraints. Use when building long-running ...
4category: Document Processing
5source: antigravity
6tags: [api, ai, llm, automation, workflow, design, document, cro]
7url: https://github.com/sickn33/antigravity-awesome-skills/tree/main/skills/workflow-orchestration-patterns
8metadata:
9 source: https://skillmd.com/skills/sickn33/workflow-orchestration-patterns
10---
11
12
13# Workflow Orchestration Patterns
14
15Master workflow orchestration architecture with Temporal, covering fundamental design decisions, resilience patterns, and best practices for building reliable distributed systems.
16
17## Use this skill when
18
19- Working on workflow orchestration patterns tasks or workflows
20- Needing guidance, best practices, or checklists for workflow orchestration patterns
21
22## Do not use this skill when
23
24- The task is unrelated to workflow orchestration patterns
25- You need a different domain or tool outside this scope
26
27## Instructions
28
29- Clarify goals, constraints, and required inputs.
30- Apply relevant best practices and validate outcomes.
31- Provide actionable steps and verification.
32- If detailed examples are required, open `resources/implementation-playbook.md`.
33
34## When to Use Workflow Orchestration
35
36### Ideal Use Cases (Source: docs.temporal.io)
37
38- **Multi-step processes** spanning machines/services/databases
39- **Distributed transactions** requiring all-or-nothing semantics
40- **Long-running workflows** (hours to years) with automatic state persistence
41- **Failure recovery** that must resume from last successful step
42- **Business processes**: bookings, orders, campaigns, approvals
43- **Entity lifecycle management**: inventory tracking, account management, cart workflows
44- **Infrastructure automation**: CI/CD pipelines, provisioning, deployments
45- **Human-in-the-loop** systems requiring timeouts and escalations
46
47### When NOT to Use
48
49- Simple CRUD operations (use direct API calls)
50- Pure data processing pipelines (use Airflow, batch processing)
51- Stateless request/response (use standard APIs)
52- Real-time streaming (use Kafka, event processors)
53
54## Critical Design Decision: Workflows vs Activities
55
56**The Fundamental Rule** (Source: temporal.io/blog/workflow-engine-principles):
57
58- **Workflows** = Orchestration logic and decision-making
59- **Activities** = External interactions (APIs, databases, network calls)
60
61### Workflows (Orchestration)
62
63**Characteristics:**
64
65- Contain business logic and coordination
66- **MUST be deterministic** (same inputs → same outputs)
67- **Cannot** perform direct external calls
68- State automatically preserved across failures
69- Can run for years despite infrastructure failures
70
71**Example workflow tasks:**
72
73- Decide which steps to execute
74- Handle compensation logic
75- Manage timeouts and retries
76- Coordinate child workflows
77
78### Activities (External Interactions)
79
80**Characteristics:**
81
82- Handle all external system interactions
83- Can be non-deterministic (API calls, DB writes)
84- Include built-in timeouts and retry logic
85- **Must be idempotent** (calling N times = calling once)
86- Short-lived (seconds to minutes typically)
87
88**Example activity tasks:**
89
90- Call payment gateway API
91- Write to database
92- Send emails or notifications
93- Query external services
94
95### Design Decision Framework
96
97```
98Does it touch external systems? → Activity
99Is it orchestration/decision logic? → Workflow
100```
101
102## Core Workflow Patterns
103
104### 1. Saga Pattern with Compensation
105
106**Purpose**: Implement distributed transactions with rollback capability
107
108**Pattern** (Source: temporal.io/blog/compensating-actions-part-of-a-complete-breakfast-with-sagas):
109
110```
111For each step:
112 1. Register compensation BEFORE executing
113 2. Execute the step (via activity)
114 3. On failure, run all compensations in reverse order (LIFO)
115```
116
117**Example: Payment Workflow**
118
1191. Reserve inventory (compensation: release inventory)
1202. Charge payment (compensation: refund payment)
1213. Fulfill order (compensation: cancel fulfillment)
122
123**Critical Requirements:**
124
125- Compensations must be idempotent
126- Register compensation BEFORE executing step
127- Run compensations in reverse order
128- Handle partial failures gracefully
129
130### 2. Entity Workflows (Actor Model)
131
132**Purpose**: Long-lived workflow representing single entity instance
133
134**Pattern** (Source: docs.temporal.io/evaluate/use-cases-design-patterns):
135
136- One workflow execution = one entity (cart, account, inventory item)
137- Workflow persists for entity lifetime
138- Receives signals for state changes
139- Supports queries for current state
140
141**Example Use Cases:**
142
143- Shopping cart (add items, checkout, expiration)
144- Bank account (deposits, withdrawals, balance checks)
145- Product inventory (stock updates, reservations)
146
147**Benefits:**
148
149- Encapsulates entity behavior
150- Guarantees consistency per entity
151- Natural event sourcing
152
153### 3. Fan-Out/Fan-In (Parallel Execution)
154
155**Purpose**: Execute multiple tasks in parallel, aggregate results
156
157**Pattern:**
158
159- Spawn child workflows or parallel activities
160- Wait for all to complete
161- Aggregate results
162- Handle partial failures
163
164**Scaling Rule** (Source: temporal.io/blog/workflow-engine-principles):
165
166- Don't scale individual workflows
167- For 1M tasks: spawn 1K child workflows × 1K tasks each
168- Keep each workflow bounded
169
170### 4. Async Callback Pattern
171
172**Purpose**: Wait for external event or human approval
173
174**Pattern:**
175
176- Workflow sends request and waits for signal
177- Ex