Date: August 02, 2026
In a monolithic architecture, managing data integrity across multiple operations is straightforward. Relational databases provide robust ACID (Atomicity, Consistency, Isolation, Durability) guarantees using local database transactions. If an operation fails midway through checking out a shopping cart—such as a credit card decline after decrementing stock—the database issues a ROLLBACK, reverting all modified tables to their initial pristine state within milliseconds.
However, as systems scale and adopt microservices with a Database-per-Service pattern, single-database ACID guarantees vanish. An end-to-end business workflow—such as creating an order, charging a customer, reserving warehouse inventory, and dispatching a delivery carrier—now spans multiple distinct databases across disparate network boundaries.
Monolithic (ACID):
[ Client ] ---> [ Monolith ] ---> [ Single Relational DB ] (Local BEGIN / COMMIT / ROLLBACK)
Microservices (Distributed):
[ Client ] ---> [ Order Service ] ---> [ Order DB ]
| (Network)
[ Payment Service ] ---> [ Payment DB ]
| (Network)
[ Inventory Service ] ---> [ Inventory DB ]
| (Network)
[ Shipping Service ] ---> [ Shipping DB ]
When a step fails in this distributed chain (e.g., payment succeeds, but inventory is out of stock), you cannot execute a database-level ROLLBACK across independent services.
To solve this challenge without sacrificing service autonomy and performance, distributed architectures rely on the Saga Pattern.
Before diving into Sagas, it is critical to understand why the classical distributed transaction standard—Two-Phase Commit (2PC / XA Transactions)—is widely avoided in modern cloud-native systems:
PREPARE (phase 1) and hold database locks on the affected rows until all participants agree, followed by a COMMIT (phase 2). If any node is slow or network latency spikes, locks are held across all services, causing massive connection pool exhaustion and cascading latency.Originally proposed in 1987 by Hector Garcia-Molina and Kenneth Salem for long-lived transactions in database engines, the Saga pattern was later adapted for distributed microservice architectures.
A Saga is a sequence of local transactions ($T_1, T_2, \dots, T_n$) where each local transaction updates data within a single service and publishes a message or event. If all local transactions complete successfully, the saga finishes.
If any local transaction ($T_k$) fails due to business rule violations (e.g., insufficient funds, out-of-stock items) or fatal validation errors, the saga executes a series of compensating transactions ($C_{k-1}, \dots, C_1$) in reverse order to undo the changes made by the preceding transactions.
Success Path:
[ T1: Create Order ] ---> [ T2: Charge Payment ] ---> [ T3: Reserve Inventory ] ---> [ T4: Dispatch Delivery ]
Failure at T3 (Inventory Out of Stock):
[ T1: Create Order ] ---> [ T2: Charge Payment ] ---> [ T3: Reserve Inventory (FAILS) ]
|
[ C1: Cancel Order ] <--- [ C2: Refund Payment ] <-------------+
In a Saga workflow, individual steps are categorized into three distinct transaction types:
A fundamental difference between database rollbacks and saga compensation is that compensating transactions are semantic rollbacks, not technical rollbacks.
| Original Transaction ($T_i$) | Compensating Transaction ($C_i$) | Semantic Reality |
|---|---|---|
| Debit $100 from Bank Account | Credit $100 to Bank Account | Generates a new statement entry; does not erase the debit record |
| Reserve 2 seats on Flight 101 | Release 2 reserved seats | Seats return to available pool; audit trail records reservation and cancellation |
Create PENDING Order Record |
Update Order status to CANCELLED |
Order record persists with updated state for analytics and audit |
Because compensating transactions are new transactions, they must be designed with two non-negotiable properties:
There are two primary architectural patterns to coordinate a Saga: Choreography (Event-Driven) and Orchestration (Command-Driven).
graph TD
subgraph Choreography ["Choreography (Decentralized / Event-Driven)"]
O1[Order Service] -->|OrderCreated| P1[Payment Service]
P1 -->|PaymentAuthorized| I1[Inventory Service]
I1 -->|InventoryFailed| P1
P1 -->|RefundInitiated| O1
end
subgraph Orchestration ["Orchestration (Centralized / Coordinator)"]
SO[Saga Orchestrator] -->|1. Process Payment| P2[Payment Service]
P2 -->|Payment Success| SO
SO -->|2. Reserve Stock| I2[Inventory Service]
I2 -->|Stock Unavailable| SO
SO -->|3. Refund Payment| P2
end
In a choreographed saga, there is no central controller. Participating services listen to domain events emitted by other services via a distributed message broker (e.g., Apache Kafka, RabbitMQ, AWS SQS/SNS) and autonomously execute their local transactions.
[Client]
| (1. POST /orders)
v
[Order Service] ------ (Publishes: OrderCreatedEvent) ------> [Message Broker]
|
+-------------------------------------------------------------+
| (Subscribes)
v
[Payment Service] ---> Executes local debit
|
+------------ (Publishes: PaymentProcessedEvent) ----> [Message Broker]
|
+-------------------------------------------------------------+
| (Subscribes)
v
[Inventory Service] -> Executes local stock reservation
|
+------------ (Publishes: InventoryReservedEvent) ---> [Message Broker]
|
+-------------------------------------------------------------+
| (Subscribes)
v
[Shipping Service] --> Schedules carrier dispatch
If Inventory Service detects that an item is out of stock:
Inventory Service emits InventoryFailedEvent.Payment Service consumes InventoryFailedEvent and executes RefundPayment ($C_2$).Order Service consumes PaymentRefundedEvent and sets the order status to REJECTED ($C_1$).In an orchestrated saga, a dedicated Saga Orchestrator (State Machine) explicitly coordinates the workflow. The orchestrator sends direct commands to participating services, waits for command responses, and determines the next step or rollback sequence based on the returned status.
Popular tools used for orchestrators include Temporal.io, AWS Step Functions, Camunda/Zeebe, Cadence, or custom state machines backed by PostgreSQL/Redis.
sequenceDiagram
autonumber
actor Client
participant SO as Order Saga Orchestrator
participant Order as Order Service
participant Payment as Payment Service
participant Inv as Inventory Service
Client->>SO: Submit Order Workflow
SO->>Order: Create Order (Status: Pending)
Order-->>SO: Order Created
SO->>Payment: Charge Customer ($150)
Payment-->>SO: Payment Success
SO->>Inv: Reserve Items [SKU-992]
Inv-->>SO: Error: Out of Stock (Failure)
Note over SO: Compensation Initiated
SO->>Payment: Refund Customer ($150)
Payment-->>SO: Refund Acknowledged
SO->>Order: Cancel Order (Status: Cancelled)
Order-->>SO: Order Cancelled
SO-->>Client: Order Failed (Out of Stock)
Saga Orchestrator sends ProcessPaymentCommand to Payment Service.Payment Service replies with PaymentSuccessReply.Saga Orchestrator updates its internal state table and sends ReserveInventoryCommand to Inventory Service.Inventory Service replies with OutOfStockReply, the Orchestrator halts forward progress and executes:
RefundPaymentCommand $\rightarrow$ Payment ServiceCancelOrderCommand $\rightarrow$ Order Service| Feature / Criterion | Choreography (Event-Driven) | Orchestration (Command-Driven) |
|---|---|---|
| Coordination | Decentralized, event-driven | Centralized state machine / coordinator |
| Coupling | Loose (Event contracts) | Low-to-Medium (Commands directed to services) |
| Complexity | Simple for 2–3 services; scales poorly | Requires state machine setup; scales well |
| Monitoring & Debugging | Difficult; requires distributed trace stitching | Easy; inspect orchestrator state log |
| Rollback Complexity | High; all services must subscribe to failure events | Low; orchestrator handles reverse sequence |
| Best Used For | Simple workflows, high-throughput micro-steps | Complex, long-running, multi-step business transactions |
The most critical architectural challenge when implementing the Saga pattern is the lack of Isolation (ACD, not ACID).
Because each local transaction commits immediately to its respective database, intermediate states are visible to concurrent transactions before the saga has finished or before compensations can be triggered.
To counteract the lack of isolation, distributed system engineers employ specific architectural design patterns:
+---------------------------------------------------------------------------------------+
| Saga Isolation Countermeasures |
+---------------------------------------------------------------------------------------+
| 1. Semantic Lock | Mark entities as PENDING / RESERVED before final commit |
| 2. Commutative Updates | Design operations where execution order does not alter sum |
| 3. Pessimistic Ordering | Order saga steps to place high-risk operations first |
| 4. Reread / Version Lock| Use optimistic concurrency control (OCC) before writes |
| 5. By-Value Routing | Route high-value transactions via 2PC, low-value via Saga |
+---------------------------------------------------------------------------------------+
Instead of hard-debiting or hard-allocating resources, set the record state to a transitional status (e.g., ORDER_PENDING_PAYMENT, SEAT_HELD_10_MIN). Other operations inspecting the record treat pending states appropriately (e.g., blocking duplicate attempts or displaying a “hold” banner).
Design compensable operations to be commutative ($A \cdot B = B \cdot A$). If operations can be executed in any order, lost updates and race conditions cannot corrupt the balance.
SET balance = 100, use atomic increments: UPDATE accounts SET balance = balance - 50 WHERE id = 123.Reorder the steps of your saga to minimize financial or operational risk. Place operations that cannot be easily compensated or have high external impact (e.g., sending physical mail, manufacturing a customized item, paying a vendor) after the pivot transaction.
Before applying an update in a subsequent saga step, verify that the record’s version token has not mutated since the initial read:
UPDATE inventory
SET available_quantity = available_quantity - 1, version = version + 1
WHERE product_id = 'SKU-100' AND version = 4;
If zero rows are updated, a concurrent modification occurred, allowing the service to fail gracefully and initiate saga compensation.
A distributed saga is only as reliable as the messaging and database interaction within each service. Two architectural patterns are mandatory to prevent split-brain and message loss:
In microservices, a common bug is updating a local database and then publishing a message to Kafka. If the database commit succeeds but the network crashes before publishing the message, the saga stalls permanently.
The Transactional Outbox Pattern ensures that database updates and message publishing occur within the same local ACID transaction:
BEGIN TRANSACTION;
-- 1. Perform core business update
UPDATE orders SET status = 'PROCESSING' WHERE id = 'ORD-501';
-- 2. Insert event record into outbox table in the SAME database
INSERT INTO outbox_events (id, aggregate_type, aggregate_id, event_type, payload, created_at)
VALUES ('evt-889', 'Order', 'ORD-501', 'OrderPaid', '{"amount": 150}', NOW());
COMMIT;
A separate background process (e.g., Debezium using Change Data Capture / CDC, or a polling relay) reads the outbox_events table and reliably delivers the events to Apache Kafka with at-least-once delivery guarantees.
flowchart LR
A[Service App] -->|1. Local ACID Transaction| B[(PostgreSQL DB)]
subgraph B [(PostgreSQL DB)]
T1[Business Table]
T2[Outbox Table]
end
T2 -->|2. CDC / Debezium| C[Kafka Broker]
C -->|3. Consume Event| D[Downstream Service]
Because message brokers guarantee at-least-once delivery, participating services will occasionally receive duplicate messages due to network retries.
Every participating service must maintain a processed message ledger:
def handle_payment_command(command: ProcessPaymentCommand, db_session):
# Check if message was already processed
if db_session.query(ProcessedMessage).filter_by(message_id=command.message_id).first():
logger.info(f"Duplicate message {command.message_id} ignored.")
return
# Execute business logic
account = db_session.query(Account).filter_by(id=command.account_id).with_for_update().first()
account.balance -= command.amount
# Record message execution inside the same transaction
db_session.add(ProcessedMessage(message_id=command.message_id, processed_at=datetime.utcnow()))
db_session.commit()
+-----------------------------+
| Does transaction span |
| multiple datastores/APIs? |
+-----------------------------+
|
+-------+-------+
No | | Yes
v v
+-----------------------+ +---------------------------+
| Use Local ACID DB | | Can business tolerate |
| Transaction | | eventual consistency? |
+-----------------------+ +---------------------------+
|
+-------+-------+
No | | Yes
v v
+-----------------------+ +---------------------------+
| Reconsider Monolith / | | Is workflow complex with |
| Single Shared DB | | 4+ steps & strict audits? |
+-----------------------+ +---------------------------+
|
+-------+-------+
No | | Yes
v v
+-----------------------+ +---------------------------+
| Choreography-Based | | Orchestration-Based |
| Saga (Kafka/RabbitMQ) | | Saga (Temporal/Step Fn) |
+-----------------------+ +---------------------------+