Saga Pattern
TL;DR
The Saga pattern manages distributed transactions across services using a sequence of local transactions — each step publishes an event, and compensating transactions undo completed steps on failure.
Explanation
Distributed transactions across microservices can't use 2PC (too slow, too coupled). Saga: a sequence of local transactions (T1, T2, ..., Tn) where each Ti publishes an event triggering Ti+1. On failure at Ti: run compensating transactions (C(i-1), ..., C1) to undo. Two styles: choreography (each service publishes events and other services react — no central coordinator) and orchestration (a Saga orchestrator sends commands to services and handles failures centrally). Choreography: decoupled but hard to trace. Orchestration: easier to monitor and debug. PHP: Symfony Messenger workflows, custom saga state machine.
Common Misconception
✗ Sagas provide ACID guarantees — they provide ACI (no isolation). Other transactions can see intermediate saga state. Use compensating logic, not database rollback.
Why It Matters
Saga is the standard solution for distributed transactions in microservices — without it, services either couple via distributed transactions (2PC) or leave data inconsistent.
Common Mistakes
- No compensating transactions defined — partial failures leave data inconsistent.
- Choreography without correlation ID — impossible to trace saga execution.
- Not handling compensating transaction failures — need retry + alerting.
Code Examples
✗ Vulnerable
// 2PC across services — tight coupling, deadlock risk:
$coordinator->prepare('order-service', $orderTx);
$coordinator->prepare('payment-service', $paymentTx);
$coordinator->commit(); // Both or neither
✓ Fixed
// Saga orchestration:
// 1. OrderService.createOrder() → publishes OrderCreated
// 2. PaymentService.processPayment() → publishes PaymentProcessed
// 3. InventoryService.reserveItems() → publishes ItemsReserved
// On PaymentFailed: publish OrderCancelled → inventory released
$sagaState = ['order_id' => $orderId, 'step' => 'payment', 'status' => 'pending'];
// Orchestrator retries failed steps, triggers compensations on unrecoverable failure
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
23 Mar 2026
Views
31
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 1
No pings yesterday
Perplexity 9
Amazonbot 8
SEMrush 3
Google 2
Ahrefs 2
Unknown AI 2
Also referenced
How they use it
crawler 25
crawler_json 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟠 High
⚙ Fix effort: High
⚡ Quick Fix
Define compensating transactions for every step. Assign correlation ID to track saga state. Use orchestration for complex sagas (easier to debug). Persist saga state between steps.
📦 Applies To
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
saga|compensat
Auto-detectable:
✗ No
⚠ Related Problems
🤖 AI Agent
Confidence: Low
False Positives: High
✗ Manual fix
Fix: High
Context: File
Tests: Update