What you will learn
6- Explain why ACID and two-phase commit do not carry over to a database-per-service design
- Describe a saga as a sequence of local transactions with compensating transactions
- Decide between forward recovery (retry) and backward recovery (compensate)
- Tell compensable, pivot and retryable transactions apart
- Choose between choreography and orchestration for a given workflow
- Name the trade-offs a saga brings: eventual consistency, no isolation, idempotency and observability
When each microservice owns its database, a single BEGIN ... COMMIT can no longer cover a business operation such as placing an order. The saga pattern solves this by splitting the operation into local transactions and defining how to undo them if a later step fails.
This article explains the idea from first principles, walks through an order example, and ends with a small runnable Python sketch of compensation in reverse order. There are no installation steps: the pattern is the subject, and the product pages linked at the end cover tooling.
Why can't one database transaction cover several services?
Inside one service, a transaction follows ACID: atomicity (all operations succeed or none do), consistency, isolation and durability. That works because everything happens in a single database.
Microservices usually assign a dedicated database to each service. That brings real benefits: each service encapsulates its data, picks the technology that suits it, scales independently, and fails in isolation. The cost is that ACID guarantees do not extend across several independently managed data stores.
The classic cross-database answer is two-phase commit (2PC). A coordinator asks every participant to promise to commit in a prepare phase, then tells them to commit in a commit phase. AWS Prescriptive Guidance points out that with a database-per-service design this is not an option: the transaction is spread across many databases, and there is no single controller that can run a 2PC-style process. Temporal's documentation adds that 2PC does not scale well and couples services tightly.
The saga pattern is the usual alternative.
What is a saga?
A saga is a sequence of local transactions. Each local transaction:
- completes its work atomically inside one service,
- updates that service's own database,
- triggers the next transaction through an event or message.
If a local transaction fails, the saga runs compensating transactions that reverse the changes made by the earlier steps. Temporal describes the rule precisely: run the compensations in reverse order, and register each one as its step is reached so cleanup happens reliably.
Note what is missing: there is no global lock and no global commit. Each step commits for real, and a compensation is a new transaction with the opposite business effect, not a database rollback.
What does a saga look like for an order?
Take an online book purchase. The business workflow is order creation, inventory update, payment and shipping, and each step writes to a different service's database.
In the orchestration example from AWS, three steps run in order: T1 creates the order, T2 updates inventory, T3 takes payment. If T3 fails because the payment is invalid, the orchestrator runs compensations C1 and C2 to restore the data to its initial state.
Each compensation is itself a local transaction in the owning service. The order stays visible in the order database until C1 marks it as cancelled, which matters later when we talk about isolation.
Do you retry or compensate when a step fails?
AWS Prescriptive Guidance frames recovery with two principles: continuation decides forward recovery, compensation decides backward recovery. The right choice depends on the kind of failure.
- Platform-level failure. Something in the infrastructure broke. Use forward recovery: retry the local transaction and continue the workflow.
- Application-level failure. The business rule said no, for example the payment service rejects an invalid payment. Use backward recovery: issue compensating transactions that update inventory and orders to reinstate their previous state.
The saga therefore aims for a desirable end state through forward recovery, and reverts local transactions through backward recovery only when it cannot get there.
Which steps can you undo? Compensable, pivot and retryable
The Azure Architecture Center classifies saga steps into three kinds:
- Compensable transactions can be undone by another transaction with the opposite effect.
- The pivot transaction is the point of no return. After it succeeds, compensable transactions no longer apply, and every later action must complete for the system to reach a consistent final state. The pivot can be the last compensable step or the first retryable one.
- Retryable transactions follow the pivot. They are idempotent, so the saga can reach its final state even through temporary failures.
Design the order of your steps around this. Put steps that are hard to reverse, such as sending something to a customer, after the pivot, and make sure everything after it can be retried safely.
Choreography or orchestration?
The two usual ways to coordinate a saga differ in who knows the workflow.
Choreography. Services exchange events with no central controller. Each local transaction publishes a domain event that triggers a local transaction elsewhere. In the AWS example, the order service emits an OrderPlaced event, and the inventory service subscribes to it and updates stock.
Orchestration. A central orchestrator knows the steps. It sends a message to a participant, receives the reply, and decides which service runs next.
| Choreography | Orchestration | |
|---|---|---|
| Fits | Few participants, simple workflows | Many participants, loose coupling needed |
| Strength | No extra coordination service, no single point of failure | Workflow logic lives in one place |
| Weakness | Hard to track which events each participant reacts to as steps are added; risk of cyclic dependencies | The orchestrator can become a single point of failure |
The Eventuate documentation sums it up: choreography works well for simple sagas, while for more complex sagas orchestration is often easier to understand.
How do you code compensations?
Temporal's worked example opens a customer account in four steps and registers each compensation before running the step it protects. On failure it runs the registered compensations in reverse order. Because a compensation can be registered before its step finishes, it can run for a step that failed midway, so it must be idempotent.
The sketch below uses only the Python standard library and mirrors that idea with the order example: three steps, T3 fails, compensations run in reverse. It is an illustration of the logic, not a production saga engine: it keeps state in memory and has no durability, retries or messaging.
Save it as saga_demo.py and run it with your Python interpreter.
class PaymentDeclined(Exception):
pass
def create_order(state):
state["orders"]["o1"] = "PENDING"
print("T1 order created")
def cancel_order(state):
if "o1" in state["orders"]:
state["orders"]["o1"] = "CANCELLED"
print("C1 order cancelled")
def reserve_stock(state):
state["stock"]["book"] -= 1
state["reserved"] = True
print("T2 stock reserved")
def release_stock(state):
# Idempotent: only gives stock back if it is currently reserved.
if state["reserved"]:
state["stock"]["book"] += 1
state["reserved"] = False
print("C2 stock released")
def charge_payment(state):
print("T3 payment declined")
raise PaymentDeclined("card declined")
def refund_payment(state):
# Idempotent: does nothing when no payment was recorded.
if "o1" in state["payments"]:
del state["payments"]["o1"]
print("C3 payment refunded")
else:
print("C3 nothing to refund")
def run_saga(steps, state):
compensations = []
try:
for action, compensation in steps:
# Register the compensation before running the step.
compensations.append(compensation)
action(state)
except Exception:
for compensation in reversed(compensations):
compensation(state)
return False
return True
if __name__ == "__main__":
state = {"orders": {}, "stock": {"book": 5}, "reserved": False, "payments": {}}
steps = [
(create_order, cancel_order),
(reserve_stock, release_stock),
(charge_payment, refund_payment),
]
ok = run_saga(steps, state)
print("saga succeeded" if ok else "saga failed", "final state:", state)
What does a saga not give you?
A saga trades the guarantees of one transaction for availability and loose coupling. Plan for these costs, all listed in the AWS and Azure guidance:
- Eventual consistency. Local transactions run one after another, so data is briefly inconsistent. Set business expectations accordingly, or use a data store with strong consistency where you cannot accept that.
- No isolation. Concurrent sagas can read stale or half-finished data. AWS recommends semantic locking, for example marking an order as pending so other flows treat it with care.
- Idempotency. Participants must tolerate repeated execution after crashes or orchestrator failures.
- Complexity. Compensations and retries add code to design, test and maintain. AWS warns that the pattern is difficult to debug and gets harder as services are added.
- Observability. Detailed logging and tracing become essential as participants grow.
- Latency. Compensations add response time in long sagas. AWS advises avoiding synchronous calls in that case.
When is a saga the right choice?
Consider a saga when:
- you need consistency across several microservices without tight coupling,
- transactions are long-lived and you do not want other services blocked while one runs long,
- you must be able to roll back when an operation in the sequence fails,
- your data stores, such as some NoSQL databases, do not offer ACID transactions or 2PC.
If the whole operation fits in one service and one database, use a normal local transaction instead.
For tooling, AWS documents an orchestration sample built on Step Functions standard workflows. Temporal provides SDK examples in Python, Go, Java and TypeScript. Eventuate Tram Sagas is an orchestration framework for Java microservices that use JDBC/JPA; its CreateOrderSaga example creates an order, reserves credit in the customer service, then approves the order, with a compensating step that rejects the order if credit cannot be reserved. For parameters and setup, read each product's own documentation page.
Glossary
- Saga
- A sequence of local transactions across services, with compensating transactions that undo completed steps if a later step fails.
- Local transaction
- A transaction that completes atomically inside one service and its own database, then triggers the next step by event or message.
- Compensating transaction
- A transaction with the opposite business effect of an earlier step, used to reverse its changes.
- Two-phase commit (2PC)
- A protocol with a prepare phase and a commit phase run by a coordinator; not practical across independent databases in a database-per-service design.
- Pivot transaction
- The point of no return in a saga: after it succeeds, the remaining steps must complete.
- Retryable transaction
- An idempotent step after the pivot that can be repeated until the saga reaches its final state.
- Choreography
- Saga coordination where services publish and subscribe to events with no central controller.
- Orchestration
- Saga coordination where a central orchestrator tells each participant what to do and reacts to its reply.
- Semantic locking
- An application-level marker, such as a pending status, that warns concurrent flows that a saga is still in progress.
- Idempotency
- The property that repeating an operation has the same effect as running it once.
Frequently asked questions
Is a saga the same as a distributed transaction?
No. A saga maintains consistency across services without using distributed transactions. It commits each local transaction immediately and uses compensating transactions to reverse earlier steps if a later one fails.
Why not use two-phase commit between microservices?
With a database per service, each transaction spans several databases and there is no single controller that can run a 2PC-style process. Temporal also notes that 2PC does not scale well and creates tight coupling.
Is a compensating transaction the same as a rollback?
No. The original step has already committed in its own database. A compensating transaction is a new transaction with the opposite effect, such as cancelling an order or releasing reserved stock.
When do you retry instead of compensate?
Retry (forward recovery) after platform-level failures such as infrastructure problems. Compensate (backward recovery) after application-level failures such as an invalid payment.
Does a saga give you isolation?
No. Concurrent sagas can see stale or intermediate data. AWS recommends semantic locking to handle this.
Should I choose choreography or orchestration?
Choreography suits few participants and simple workflows with no single point of failure. Orchestration suits many participants and loose coupling, but the orchestrator can become a single point of failure.
Next steps
- Map one of your own multi-service workflows into steps, compensations and a pivot transaction.
- Decide whether choreography or orchestration fits the number of participants you have.
- Read the AWS orchestration page to see a Step Functions implementation.
- Read the Temporal page to see compensations registered in workflow code in several languages.
- Read the Eventuate Tram Sagas page if you build on Java with JDBC/JPA.
References
From Grokem: https://www.grokem.com/programming/software-architecture/microservices/saga-pattern-explained-transactions-across-microservices-without-2pc








