A distributed transaction is a transaction whose operations touch two or more independent resources, such as separate database servers, shards or a database and a message broker, yet must succeed or fail as one atomic unit. In databases, it extends the guarantees of ACID across network boundaries, so that no participant commits its part of the work while another participant rolls back.
The concept matters because modern systems rarely keep all data in one place. Sharded databases, microservices and globally replicated stores all face the same question: how to keep several machines in agreement about the outcome of a change when any of them, or the network between them, can fail at any moment.
How a distributed transaction reaches agreement
A distributed transaction relies on a coordinator (also called a transaction manager) that tracks every participant and drives them to a common decision. Each participant, or resource manager, performs its local work, holds the necessary locks and records its state in a durable log such as the write-ahead log.
The classic protocol is two-phase commit (2PC). In the prepare phase, the coordinator asks every participant whether it can commit; each one writes its changes durably and votes yes or no. In the commit phase, the coordinator records the decision and tells everyone to commit if all votes were yes, or to roll back if any vote was no.
| Role | Responsibility | Durable state it keeps |
|---|---|---|
| Coordinator | Collects votes and announces the outcome | Decision record per transaction |
| Participant | Executes local work and votes | Prepared changes and held locks |
| Application | Defines transaction boundaries | Usually none beyond its request |
| Recovery process | Resolves in-doubt transactions after failures | Reads coordinator and participant logs |
The critical moment is the prepared state. Once a participant votes yes, it has promised to commit if asked and can no longer decide alone. It must keep its locks until the coordinator answers, which is why a failure at this point leaves the participant in doubt.
Protocols and standards for distributed transactions
Several protocols implement distributed transactions, and they differ mainly in how they handle coordinator failure and how many network round trips they require. The choice determines both latency and availability during faults.
| Protocol | Core idea | Main limitation |
|---|---|---|
| Two-phase commit | Prepare votes, then a single decision | Blocks if the coordinator fails after prepare |
| Three-phase commit | Adds a pre-commit step to avoid blocking | Assumes bounded network delays, rarely used |
| Paxos or Raft commit | Replicates the decision through consensus | More messages and complexity |
| Presumed abort 2PC | Treats missing decision records as rollback | Optimization only, still blocking |
| Saga pattern | Chains local transactions with compensations | No isolation between steps |
The X/Open XA standard defines the interface between a transaction manager and resource managers and is supported by PostgreSQL, MySQL, Oracle, SQL Server and many message brokers. Java applications reach it through the Java Transaction API (JTA), and Windows environments historically used the Microsoft Distributed Transaction Coordinator.
Newer distributed SQL systems such as Google Spanner, CockroachDB and YugabyteDB run two-phase commit on top of consensus-replicated shards. Because each participant and the coordinator state are themselves replicated through Paxos or Raft, the loss of a single machine no longer blocks the protocol, which removes the classic weakness of 2PC.
Distributed transactions compared with local transactions
A distributed transaction offers the same logical guarantees as a local transaction, but it pays for them with extra latency, longer lock durations and new failure modes. The differences shape when architects accept distributed transactions and when they design around them.
| Aspect | Local transaction | Distributed transaction |
|---|---|---|
| Commit latency | One log flush | At least two network round trips plus flushes |
| Lock duration | Until local commit | Until the global decision arrives |
| Failure modes | Crash and restart | Partial failure, network partition, in-doubt state |
| Isolation | Managed by one engine | Requires coordinated snapshots or clocks |
| Throughput impact | Low | Moderate to high under contention |
Isolation is the subtle part. Achieving a globally consistent snapshot requires either a central timestamp service or synchronized clocks; Spanner uses tightly bounded clock uncertainty for this purpose, while other systems use hybrid logical clocks. Without such a mechanism, MVCC on separate nodes can expose anomalies that a single database would prevent.
When to use a distributed transaction
A distributed transaction is appropriate when strict atomicity across systems is a business requirement and the participants can tolerate the latency. Where eventual consistency is acceptable, lighter patterns usually deliver better availability and throughput.
- Use 2PC or XA for low-volume, high-value operations, such as moving funds between ledgers held in separate databases.
- Use a distributed SQL database when data must be sharded but the application expects ordinary ACID semantics.
- Use the saga pattern for long-running business processes across microservices, accepting compensating actions instead of rollback.
- Use the transactional outbox to update a database and publish an event reliably without involving the broker in a 2PC.
- Redesign data ownership when the same two systems need distributed transactions for most requests, because that indicates a poor partition boundary.
A practical rule is to minimize the number of participants and keep transactions short. Every additional participant adds a failure point and a vote, and every millisecond spent in the prepared state extends lock contention for other transactions.
Risks and common mistakes
The most serious risk is the blocking problem: if the coordinator crashes after participants have prepared, those participants hold locks until it recovers. In-doubt transactions can freeze hot rows for minutes or hours, and operators sometimes resolve them manually, which risks heuristic outcomes where participants disagree.
Other frequent mistakes include assuming that XA support makes a broker and database fully consistent under all failures, leaving prepared transactions orphaned after testing, and ignoring timeouts. In PostgreSQL, forgotten prepared transactions also hold back VACUUM and can lead to table bloat and transaction ID wraparound warnings, so monitoring the pg_prepared_xacts view is essential.
Finally, teams sometimes treat the saga pattern as a drop-in replacement. Sagas trade isolation for availability, so intermediate states are visible to other transactions, and every compensating action must be designed, tested and made idempotent.
Understanding it simply
Imagine two banks agreeing to move money between them. Neither bank should take the money out unless the other is ready to put it in. A distributed transaction works like a referee who asks both sides, “Are you ready?” and only says “Go” when everyone has answered yes.
If anyone says no, or does not answer, nobody goes ahead. The result is all or nothing across every system, even though the systems are separate machines that can fail on their own.
Applied in practice
A payment platform stores customer balances in one database cluster and merchant settlement records in another. When a customer pays a merchant, both records must change together. The engineering team uses XA transactions coordinated by a JTA transaction manager, so the debit and the credit either both commit or both roll back.
Under normal load, each payment adds about 10 to 20 milliseconds of commit latency, which the business accepts. During a planned failover test, the team kills the coordinator after the prepare phase and observes that rows stay locked until recovery completes. The result is a revised runbook: the coordinator log moves to replicated storage, prepared-transaction timeouts are monitored, and an alert fires if any transaction stays in doubt for more than 30 seconds.