All skills
wshobson avatar

/workflow-orchestration-patterns

@be57c0b
by Seth Hobsonwshobson/agents40k stars
4,281

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 processes, distributed transactions, or microservice orchestration.

Use this Skill: https://skilld.dev/gh/wshobson/agents/workflow-orchestration-patterns

This session only. Nothing lands on disk.

referencesdetails.md

≈1.5k tokens on demand. Your agent reads this file only when SKILL.md points to it.

workflow-orchestration-patterns — detailed patterns and worked examples

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

  1. Reserve inventory (compensation: release inventory)
  2. Charge payment (compensation: refund payment)
  3. 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
  • External system processes asynchronously
  • Sends signal to resume workflow
  • Workflow continues with response

Use Cases:

  • Human approval workflows
  • Webhook callbacks
  • Long-running external processes

State Management and Determinism

Automatic State Preservation

How Temporal Works (Source: docs.temporal.io/workflows):

  • Complete program state preserved automatically
  • Event History records every command and event
  • Seamless recovery from crashes
  • Applications restore pre-failure state

Determinism Constraints

Workflows Execute as State Machines:

  • Replay behavior must be consistent
  • Same inputs → identical outputs every time

Prohibited in Workflows (Source: docs.temporal.io/workflows):

  • ❌ Threading, locks, synchronization primitives
  • ❌ Random number generation (random())
  • ❌ Global state or static variables
  • ❌ System time (datetime.now())
  • ❌ Direct file I/O or network calls
  • ❌ Non-deterministic libraries

Allowed in Workflows:

  • ✅ workflow.now() (deterministic time)
  • ✅ workflow.random() (deterministic random)
  • ✅ Pure functions and calculations
  • ✅ Calling activities (non-deterministic operations)

Versioning Strategies

Challenge: Changing workflow code while old executions still running

Solutions:

  1. Versioning API: Use workflow.get_version() for safe changes
  2. New Workflow Type: Create new workflow, route new executions to it
  3. Backward Compatibility: Ensure old events replay correctly

Resilience and Error Handling

Retry Policies

Default Behavior: Temporal retries activities forever

Configure Retry:

  • Initial retry interval
  • Backoff coefficient (exponential backoff)
  • Maximum interval (cap retry delay)
  • Maximum attempts (eventually fail)

Non-Retryable Errors:

  • Invalid input (validation failures)
  • Business rule violations
  • Permanent failures (resource not found)

Idempotency Requirements

Why Critical (Source: docs.temporal.io/activities):

  • Activities may execute multiple times
  • Network failures trigger retries
  • Duplicate execution must be safe

Implementation Strategies:

  • Idempotency keys (deduplication)
  • Check-then-act with unique constraints
  • Upsert operations instead of insert
  • Track processed request IDs

Activity Heartbeats

Purpose: Detect stalled long-running activities

Pattern:

  • Activity sends periodic heartbeat
  • Includes progress information
  • Timeout if no heartbeat received
  • Enables progress-based retry

Source: SKILL.md on GitHub

No alerts16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    No security issues detected. The skill provides architectural guidance and best practices for building durable workflows with Temporal.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer7mo

    1 file scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at be57c0b. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 3 days ago.

Activeupdated 4 months ago
  • temporal
  • workflow-orchestration
  • distributed-systems
  • saga-pattern
  • microservices
  • state-management
  • long-running-processes
  • resilience

README badge

README badge for wshobson/agents/workflow-orchestration-patterns

Teaches workflow orchestration architecture with Temporal, covering the separation of workflow orchestration from activity execution, saga patterns, state management, and determinism constraints. Use this skill when designing long-running distributed processes, implementing all-or-nothing transactions across services, or building microservice choreography that must survive failures and resume from the last successful step.

Generated from the current SKILL.md.

Does this skill cover Temporal-specific implementation, or is it framework-agnostic?
This skill is Temporal-specific. It teaches workflow orchestration patterns using Temporal's architecture, including workflow vs activity separation, determinism constraints, and Temporal's state persistence model.
What programming languages does this skill support?
The SKILL.md references Python examples (datetime.now() vs workflow.now()) but does not explicitly list all supported languages. Temporal supports TypeScript/JavaScript, Python, Go, and Java.
Does this cover the saga pattern?
Yes. The skill includes saga pattern guidance for distributed transactions and references Temporal's saga pattern documentation.
What should I use this skill for instead of tools like Airflow or Kafka?
Use this skill for long-running workflows with automatic state persistence and failure recovery (hours to years), distributed transactions requiring all-or-nothing semantics, and human-in-the-loop systems. Airflow is better for pure data pipelines; Kafka is better for real-time streaming.
Does this skill include code examples or just theory?
The skill includes worked examples and references a separate `references/details.md` file with detailed pattern documentation, though full code samples are not in the main content.

Generated from the current SKILL.md. These answers refresh after source changes.