Labsco
wshobson logo

saga-orchestration

โ˜… 37,559

by wshobson ยท part of wshobson/agents

Implement saga patterns for distributed transactions and cross-aggregate workflows. Use this skill when implementing distributed transactions across microservices where 2PC is unavailable, designing compensating actions for failed order workflows that span inventory, payment, and shipping services, building event-driven saga coordinators for travel booking systems that must roll back hotel, flight, and car rental reservations atomically, or debugging stuck saga states in production where compens

๐Ÿงฉ One of 7 skills in the wshobson/agents package โ€” works on its own, and pairs well with its siblings.

This is the playbook your agent receives when the skill activates โ€” you don't need to read it to use the skill, but it's here to audit before installing.

Saga Orchestration

Patterns for managing distributed transactions and long-running business processes without two-phase commit.

Inputs and Outputs

What you provide:

  • Service boundaries and ownership (which service owns which step)
  • Transaction requirements (which steps must be atomic, which can be eventual)
  • Failure modes for each step (transient vs. permanent, retry policy)
  • SLA requirements per step (informs timeout configuration)
  • Existing event/messaging infrastructure (Kafka, RabbitMQ, SQS, etc.)

What this skill produces:

  • Saga definition with ordered steps, action commands, and compensation commands
  • Orchestrator or choreography implementation for your chosen pattern
  • Compensation logic for each participant service (idempotent, always-succeeds)
  • Step timeout configuration with per-step deadlines
  • Monitoring setup: state machine metrics, stuck saga detection, DLQ recovery

When to Use This Skill

  • Coordinating multi-service transactions without distributed locks
  • Implementing compensating transactions for partial failures
  • Managing long-running business workflows (minutes to hours)
  • Handling failures in distributed systems where atomicity is required
  • Building order fulfillment, approval, or booking processes
  • Replacing fragile two-phase commit with async compensation

Detailed section: Core Concepts

Moved to references/details.md.

Detailed section: Templates

Moved to references/details.md.

Best Practices

Do's

  • Make every step idempotent โ€” Commands may be replayed on broker reconnect
  • Design compensations carefully โ€” They are the most critical code path
  • Use correlation IDs โ€” The saga_id must flow through every event and log
  • Implement per-step timeouts โ€” Never wait indefinitely for a participant reply
  • Log state transitions โ€” saga_id, step_name, old_state โ†’ new_state on every change
  • Test compensation paths explicitly โ€” Inject failures at each step index in integration tests

Don'ts

  • Don't assume instant completion โ€” Sagas are async and may take minutes
  • Don't skip compensation testing โ€” The rollback path is the hardest to get right
  • Don't couple services directly โ€” Use async messaging, never synchronous calls inside a saga step
  • Don't ignore partial failures โ€” A step that partially executed still needs compensation
  • Don't use a global timeout โ€” Each step has different latency characteristics

Advanced Patterns

The references/ directory contains production-grade implementations not needed for most sagas:

  • references/advanced-patterns.md โ€” Full SagaOrchestrator abstract base class, TimeoutSagaOrchestrator with per-step deadlines, detailed bank transfer compensating transaction chain, Prometheus instrumentation, stuck saga PromQL alerts, and DLQ recovery worker.

  • cqrs-implementation โ€” Pair sagas with CQRS for read-model updates after each step completes
  • event-store-design โ€” Store saga events in an event store for full audit trail and replay capability
  • workflow-orchestration-patterns โ€” Higher-level workflow engines (Temporal, Conductor) that build on saga concepts