Saga Orchestration
Skill files3 files

references/

SKILL.md118 lines
1
---
2
name: saga-orchestration
3
description: 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.
4
---
5
6
# Saga Orchestration
7
8
Patterns 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
41
Moved to `references/details.md`.
42
43
## Detailed section: Templates
44
45
Moved 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
72
A 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
75
async 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
89
If 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
93
In 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
97
A 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
101
When 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
107
The `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

© Seth Hobson · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…