Distributed Transactions: Choosing a Coordination Pattern
Choose between a local ACID transaction, two-phase commit, and a saga by mapping the data boundary, failure behavior, and recovery needs.
ACID is a set of transaction guarantees, not a single-machine deployment model. This page is a decision hub for operations that cross database boundaries: use a local transaction when one database owns the invariant, consider two-phase commit when participants can prepare and blocking is acceptable, and use a saga for long-running workflows that need explicit compensations. CAP and consistency models describe related but different guarantees.
Distributed Transactions: Choosing a Coordination Pattern
A business operation can touch several rows in one database or several independent services. The first case usually belongs in a database transaction. The second needs an explicit failure and recovery plan.
ACID describes atomicity, consistency, isolation, and durability. It does not require the database to run on one machine: distributed databases can provide ACID transactions. The consistency property means a transaction preserves the invariants the application and database define; it does not mean every read always returns the newest value under every isolation level.
Start with the data boundary
Ask where the invariant lives and who owns its source of truth.
| Situation | Starting point | Main question |
|---|---|---|
| One database owns the data | A local ACID transaction | Can the invariant be enforced with one transaction and the database’s supported isolation level? |
| One database spans several nodes | The database’s native transaction | What atomicity, isolation, and durability guarantees does this database provide for this topology? |
| Several transactional participants must commit together | Two-phase commit (2PC), if supported | Can participants prepare durably, and can the system tolerate blocking while the coordinator recovers? |
| A long-running workflow crosses services | Saga | What intermediate states can users observe, and what compensating action is valid after each step? |
Do not choose a pattern because a database is labeled SQL or NoSQL. Check the product’s transaction boundaries, read/write concerns, failure behavior, and supported topology. For example, MongoDB transactions provide multi-document ACID transactions, with the precise guarantees depending on configuration and topology.
Two-phase commit
In 2PC, a coordinator asks each participant to prepare. A participant that votes yes records enough state to commit later and holds the resources required by the transaction. If every participant votes yes, the coordinator records and sends the commit decision; otherwise it sends abort.
2PC provides an atomic outcome across participating resources when they follow the protocol and recover from durable logs. Its failure mode matters: after a participant has prepared, it cannot safely guess the outcome. If the coordinator becomes unavailable before participants learn the decision, they may block and hold resources until the coordinator or a termination protocol resolves the in-doubt transaction. That can reduce availability and consume locks or connections. The coordinator log, participant recovery, timeouts, and operator runbook are part of the design.
Use 2PC only when all participants support the protocol, the transaction is bounded, and the business needs a single atomic outcome strongly enough to accept the blocking and operational cost. See the dedicated Two-Phase Commit guide for message flow and recovery cases.
Sagas
A saga sequences local transactions across services. Each completed step publishes or records the next action. If a later step fails, the workflow runs compensating actions for completed steps.
A compensation is a new business action, not a rollback of history. A payment refund, for example, may fail or require review. The system should make each step idempotent, record progress, retry safely, and expose a clear state while the workflow is in progress. Sagas fit workflows that can tolerate intermediate states and need to continue through independent service failures. They do not provide the isolation or atomic commit of a database transaction.
See the Saga Pattern guide for choreography, orchestration, idempotency, and compensation design.
Keep the guarantees separate
ACID and BASE are not opposite modes where one means single-machine consistency and the other means distributed availability. ACID names transaction properties. BASE is a descriptive design lens for systems that remain available while state converges; individual products can combine transactions, replication, and different consistency levels.
CAP concerns the choice a system makes when a network partition prevents communication: preserve linearizable consistency for an operation or accept it without that guarantee. Consistency models compare the read/write guarantees offered by different systems. Neither concept tells you whether a multi-service business operation should use 2PC or a saga.
A practical selection checklist
Before implementing cross-service coordination, write down:
- Which data invariant must hold, and which component owns it?
- Which participants can commit locally, and can any of them prepare durably?
- What should callers see while work is pending or partially complete?
- Which failures are retried, and how are duplicate requests made safe?
- If using 2PC, how are coordinator logs and in-doubt participants recovered?
- If using a saga, what compensation is valid, and what happens if compensation fails?
- How will operators identify stalled work and resume or resolve it?
For a seat reservation followed by payment, a single service may use a local transaction if it owns both records. If inventory and payment belong to separate services, a saga can reserve inventory, authorize payment, then confirm the reservation; failed steps need explicit release or refund actions. Use 2PC only if both systems participate in a compatible transaction protocol and the blocking behavior is acceptable.
Further reading
- Two-Phase Commit — protocol assumptions, coordinator failure, and recovery.
- Saga Pattern — orchestration, choreography, retries, and compensation.
- CAP Theorem — what a partition means for consistency and availability.
- Consistency Models — read/write guarantees and their trade-offs.
Conclusion
Choose a transaction boundary from the invariant and the data owners. A local transaction is usually the simplest choice when one database owns the data. 2PC coordinates an atomic decision across prepared participants but can block during recovery. A saga trades atomic commit for a sequence of visible, recoverable business steps.
Category
Related Posts
The Outbox Pattern for Reliable Event Publishing
Learn how the transactional outbox prevents lost events, how relays publish them, and how idempotent consumers handle duplicates in distributed systems.
TCC: Try-Confirm-Cancel Pattern for Distributed Transactions
Learn the Try-Confirm-Cancel pattern for distributed transactions. Explore how TCC differs from 2PC and saga, with implementation examples and real-world use cases.
Two-Phase Commit Protocol Explained
Learn the two-phase commit protocol for distributed transactions: prepare and commit phases, coordinator role, failure handling, and trade-offs.