In a monolith, the order, the payment, and the stock reservation are written in one database transaction: all or nothing. Once the system is split into services, each has its own database and there is no shared transaction any more. If the payment succeeds but the reservation fails, the data diverges and something has to fix it. The Saga and Outbox patterns solve this without distributed locks.
Why not one distributed transaction.
Two-phase commit (2PC, XA) can run one transaction across several databases. Microservices almost never use it:
- Every participant holds locks until the coordinator decides. A slow or failed service blocks all the others.
- External APIs and payment providers cannot take part in two-phase commit, and neither can most message brokers.
- The services become coupled at runtime again, which defeats the reason for splitting them.
Instead of atomicity you get eventual consistency: the data may diverge for a while, but the system brings it back to a consistent state on its own.
A saga: a chain of local transactions.
A saga is a sequence of steps, each performed as a local transaction in one service. If a step fails, the saga runs compensating actions for the completed steps in reverse order. Garcia-Molina and Salem described the pattern in 1987 for long-lived database transactions; in microservices it became the standard way to keep data consistent.
01 /
Create the order
The order service creates the order with the status “awaiting payment”. Compensation: cancel the order.
02 /
Charge the payment
The payment service charges the customer. Compensation: refund the payment.
03 /
Reserve the stock
The warehouse reserves the items. If they are out of stock, the saga refunds the payment and cancels the order.
04 /
Confirm the order
The order moves to “paid”, and the saga completes.
The “awaiting payment” status is there because a saga has no isolation like a database transaction. Until the saga completes, other operations see the intermediate state, and the status tells them the order may still be cancelled.
Orchestration or choreography.
01 / OPTION
Choreography
Each service listens to events and decides what to do next. There is no central component, which works well for two or three steps. With more steps it becomes hard to tell where the process is and who is responsible for compensation.
02 / OPTION
Orchestration
A separate component (the orchestrator) keeps the saga’s state, calls the services in turn, and runs compensations on failure. The process logic lives in one place.
03 / OPTION
Workflow engine
You can write the orchestrator yourself on top of a database and a queue, or use an engine such as Temporal, which stores state, retries steps, and survives restarts.
How to choose
Choreography is enough for two or three steps without complex compensations. If there are more steps, or the process waits hours for people and external systems, use an orchestrator.
Compensations and retries.
A compensation does not roll data back; it performs the opposite business action: a refund, a released reservation, a cancellation email. So it is designed together with the step.
- Idempotency. A step or a compensation may run again after a failure or a timeout. A retry must not charge the customer twice; the API idempotency guide covers this in detail.
- Steps that cannot be undone. A sent email or a shipped parcel cannot be recalled. Such steps go at the end of the saga, after every check that might fail.
- Failed compensations. A compensation is retried until it succeeds. If it never does, the process is handed to a person with a clear status and the history of steps.
- Timeouts. No response does not mean failure: the payment may have gone through. Before compensating, check the operation’s status with the service.
Outbox: events must not get lost.
The order service writes the order to its database and publishes an event to the broker. These are two separate actions. If the process crashes between them, the order exists but the event does not, and the saga stalls. Publishing before writing creates the opposite problem.
The transactional outbox solves this: the event is written to an outbox table in the same transaction as the order. A separate process reads the table, publishes the events to the broker, and marks them as sent.
- The outbox can be read by polling the table or through the database change log (CDC). For Kafka there is a ready-made option: Debezium with the Outbox Event Router transform.
- Delivery is at least once: after a failure an event may be sent again. The consumer stores the IDs of processed events (an inbox table) and skips duplicates.
- Event order is preserved per entity. In Kafka, the order ID is used as the message key for that.
- Sent outbox rows are deleted or archived; otherwise the table grows without limit.
Sagas on Temporal.
Temporal records the execution history of a process and resumes it from the same point after a restart. A saga is written as ordinary code: steps are called as activities with a retry policy, and compensations are registered as steps complete and run on failure. The engine handles state, timers, and retries, so the orchestrator needs no status tables or queue of its own.
Temporal pays off when there are many processes, they are long, or they wait for external events and people. For a single short two-step saga, an orchestrator built on the database is enough.
What to bring to the first conversation.
You do not need a diagram. Describe the process where data diverges: which services take part, what happens on failure today, and how inconsistencies are found and fixed.
- What are the steps of the process, and in which services and databases do they run?
- How do the services exchange data: synchronous calls or a message broker, and which one?
- How often does data diverge, and what does it cost: manual reconciliation, refunds, customer complaints?