Skills · Coding

Saga Orchestration

Unverified25/40

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 compensation steps never complete.

Originally by wshobson · MIT

Claude CodePartialHas SKILL.md but declares no allowed-tools — Claude Code will ask for permission each time
CursorPartialPlain prose you can paste in — but no Cursor rules file
CodexPartialPlain prose you can paste in — but no AGENTS.md
Gemini CLIPartialPlain prose you can paste in
CopilotPartialPlain prose you can paste in — but no Copilot instructions file
npx agentalley add saga-orchestration

This command does not work yet — the CLI is still being built. Until then, use Raw in the reader below to take the file.

Who is stuck, and on what

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 compensation steps never complete.

The whole source

No sign-in, no blur, nothing truncated
saga-orchestration/SKILL.md118 lines5.9 KBRawView on GitHub
Frontmatter — 2 properties
namesaga-orchestration
descriptionImplement 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 compensation steps never complete.
1---
2name: saga-orchestration
3description: 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 compensation steps never complete.B1Line is 540 characters — unreadable by eye
4---A5No allowed-tools declared — no way to tell what this skill may touch
5 
6# Saga Orchestration
7 
8Patterns for managing distributed transactions and long-running business processes without two-phase commit.
9 
10## Inputs and Outputs
11 
12**What you provide:**
13- Service boundaries and ownership (which service owns which step)
14- Transaction requirements (which steps must be atomic, which can be eventual)
15- Failure modes for each step (transient vs. permanent, retry policy)
16- SLA requirements per step (informs timeout configuration)
17- Existing event/messaging infrastructure (Kafka, RabbitMQ, SQS, etc.)
18 
19**What this skill produces:**
20- Saga definition with ordered steps, action commands, and compensation commands
21- Orchestrator or choreography implementation for your chosen pattern
22- Compensation logic for each participant service (idempotent, always-succeeds)
23- Step timeout configuration with per-step deadlines
24- Monitoring setup: state machine metrics, stuck saga detection, DLQ recovery
25 
26---
27 
28## When to Use This Skill
29 
30- Coordinating multi-service transactions without distributed locks
31- Implementing compensating transactions for partial failures
32- Managing long-running business workflows (minutes to hours)
33- Handling failures in distributed systems where atomicity is required
34- Building order fulfillment, approval, or booking processes
35- Replacing fragile two-phase commit with async compensation
36 
37---
38 
39## Detailed section: Core Concepts
40 
41Moved to `references/details.md`.
42 
43## Detailed section: Templates
44 
45Moved to `references/details.md`.
46 
47## Best Practices
48 
49### Do's
50 
51- **Make every step idempotent** — Commands may be replayed on broker reconnect
52- **Design compensations carefully** — They are the most critical code path
53- **Use correlation IDs** — The `saga_id` must flow through every event and log
54- **Implement per-step timeouts** — Never wait indefinitely for a participant reply
55- **Log state transitions** — `saga_id`, `step_name`, `old_state → new_state` on every change
56- **Test compensation paths explicitly** — Inject failures at each step index in integration tests
57 
58### Don'ts
59 
60- **Don't assume instant completion** — Sagas are async and may take minutes
61- **Don't skip compensation testing** — The rollback path is the hardest to get right
62- **Don't couple services directly** — Use async messaging, never synchronous calls inside a saga step
63- **Don't ignore partial failures** — A step that partially executed still needs compensation
64- **Don't use a global timeout** — Each step has different latency characteristics
65 
66---
67 
68## Troubleshooting
69 
70### Saga stuck in COMPENSATING state
71 
72A saga enters compensation but never reaches FAILED. This means a compensation handler is throwing an unhandled exception and never publishing `SagaCompensationCompleted`. Add dead-letter queue (DLQ) handling to compensation consumers and ensure every compensation action publishes a result event even when the underlying operation was already rolled back.
73 
74```python
75async def handle_release_reservation(self, command: Dict):
76 try:
77 await self.release_reservation(command["original_result"]["reservation_id"])
78 except ReservationNotFoundError:
79 pass # Already released — treat as success
80 # Always publish completion, regardless of outcome
81 await self.event_publisher.publish("SagaCompensationCompleted", {
82 "saga_id": command["saga_id"],
83 "step_name": "reserve_inventory"
84 })
85```
86 
87### Duplicate saga executions on restart
88 
89If your orchestrator service restarts mid-saga, it may replay events and re-execute already-completed steps. Guard every step action with an idempotency key — see **Template 3** above.
90 
91### Choreography saga losing events
92 
93In a choreography-based saga, a downstream service may miss an event if it was offline when published. Use a durable message broker (Kafka with replication, RabbitMQ with persistence) and store the current saga state in a dedicated `saga_log` table so you can replay from the last known good step.
94 
95### Timeout firing before a slow-but-valid step completes
96 
97A step like `create_shipment` might take up to 15 minutes during peak load but your global timeout is 5 minutes, causing spurious compensation. Make step timeouts configurable per step type — see `references/advanced-patterns.md` for the `TimeoutSagaOrchestrator` implementation and the `STEP_TIMEOUTS` dict pattern.
98 
99### Compensation order not matching execution order
100 
101When two steps both complete before a failure is detected, compensation must run in strict reverse order or you leave data in an inconsistent state. Verify that `_compensate()` iterates from `current_step - 1` down to `0`, and add an integration test that deliberately fails at each step index to confirm correct rollback order.
102 
103---
104 
105## Advanced Patterns
106 
107The `references/` directory contains production-grade implementations not needed for most sagas:
108 
109- **`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.
110 
111---
112 
113## Related Skills
114 
115- `cqrs-implementation` — Pair sagas with CQRS for read-model updates after each step completes
116- `event-store-design` — Store saga events in an event store for full audit trail and replay capability
117- `workflow-orchestration-patterns` — Higher-level workflow engines (Temporal, Conductor) that build on saga concepts
118 

Reviews

Installed this one?Write the first review and take the Trailblazer badge.

Reviews only open after a real install, so this is empty — and we leave it empty rather than invent one.

Alternatives

Also in Coding