Distributed Transactions and Two-Phase Commit
Distributed transactions make one logical change across multiple independent data stores or services. Two-phase commit coordinates those participants so they all commit or all roll back, even though failures can interrupt the decision process.
itDatabases and data storage | OpenSkills.info
Course pathWalk it in order
Look it upDip in anytime
Go furtherLeaves this page
Don't Panic
Don't Panic — Distributed Transactions and Two-Phase Commit
A distributed transaction is for work that crosses independent resources when a half-finished result is unacceptable. Think of a debit in one database and a credit in another. Two ordinary commits can leave the money in a state that has developed opinions. The job is not to make messages arrive together. The job is to make every participant eventually follow one outcome: commit or rollback.
The reassuringly administrative two-phase commit, or 2PC, asks each participant for a durable promise. An application starts the work. A coordinator asks each resource manager whether its local branch can commit. A yes vote means more than “looks healthy”: the participant wrote enough recovery information to survive a crash and entered the prepared state.
Then comes the decision. One required no vote aborts the transaction. All yes votes permit the coordinator to record commit durably and tell everyone. Participants can hear that decision at different times. This is fine. Atomicity is agreement on one recorded result, not a tiny synchronized trumpet fanfare.
The surprise is that prepared is neither committed nor safely abortable. A prepared participant that loses contact with the coordinator can be in doubt. It keeps its promise, and often its locks, while recovery finds authoritative evidence. A timeout means “investigate,” not “pick whichever answer would make the dashboard calmer.” PostgreSQL, MySQL, and Oracle expose prepared or pending work because somebody must find the decision without inventing it.
2PC is not consensus. Atomic commitment needs every required yes vote. Consensus replicates a decision or log with quorum rules. Modern distributed databases often use both: consensus keeps each shard available across replica failures, while a commit protocol coordinates the shards touched by one transaction.
Use 2PC for short work with a hard all-or-nothing invariant and participants that can prepare and recover. For long business processes, user interaction, or workflows that can converge later, a saga, transactional outbox, or idempotent reconciliation changes the cost into a more manageable shape. Nothing magical occurred; the guarantee changed.
Read the intro for the protocol and failure boundaries. Use the slides for coordinator, participant, and consensus relationships. Keep the cheatsheet nearby for recovery surfaces and decision rules. The practice reference and exercise let you see a prepared transaction outlive its original session, which is less alarming when it happens on purpose.
Where this skill leads
Relevant careers
See how this topic contributes to broader role-level skill maps.
Sources
- https://dev.mysql.com/doc/refman/8.4/en/xa.html
Supports
- Global transactions, transaction managers, resource managers, and branches
- Prepare and decision phases, unanimous commit rule, and one-phase optimization
- MySQL XA support through InnoDB
- https://dev.mysql.com/doc/refman/8.4/en/xa-states.html
Supports
- ACTIVE, IDLE, and PREPARED XA states
- XA START, END, PREPARE, COMMIT, ROLLBACK, and RECOVER behavior
- Detached prepared transactions and recovery visibility
- https://pubs.opengroup.org/onlinepubs/009680699/toc.pdf
Supports
- X Open distributed transaction processing model
- Application, transaction-manager, and resource-manager roles
- XA branch identifiers, completion, and recovery interfaces
- December 1991 publication milestone
- https://www.postgresql.org/docs/current/two-phase.html
Supports
- PostgreSQL prepared transaction commands and intended external coordination
- Short prepared duration guidance
- WAL, `pg_twophase`, and `pg_prepared_xacts` recovery surfaces
- https://www.postgresql.org/docs/current/sql-prepare-transaction.html
Supports
- Durable prepared state after `PREPARE TRANSACTION`
- Completion from another session after restart
- Operational cautions about long-lived prepared transactions
- https://www.postgresql.org/docs/current/view-pg-prepared-xacts.html
Supports
- Inspection of prepared transaction identifiers, owners, databases, and prepare times
- https://docs.oracle.com/database/121/ADMIN/ds_txns.htm
Supports
- In-doubt transaction causes and retained locks
- Automatic RECO resolution
- `DBA_2PC_PENDING`, `DBA_2PC_NEIGHBORS`, and forced outcomes
- https://arxiv.org/abs/cs/0408036
Supports
- Atomic commitment validity and classic 2PC blocking
- Difference between transaction commit and consensus
- Paxos Commit coordinator replication and quorum progress
- August 2004 publication milestone
- https://tikv.org/deep-dive/distributed-transaction/introduction/
Supports
- Distributed transaction definition and ACID properties
- Two-phase commit as a common atomic completion algorithm
- TiKV use of a Percolator-derived 2PC model
- https://tikv.org/deep-dive/distributed-transaction/distributed-algorithms/
Supports
- Primary and secondary locks, timestamps, commit, and recovery in TiKV
- Separation of isolation mechanisms from atomic completion
- https://research.google.com/archive/spanner-osdi2012.pdf
Supports
- Externally consistent distributed transactions
- Two-phase commit across participant groups and Paxos within groups
- October 2012 publication milestone
- https://www.cockroachlabs.com/blog/parallel-commits/
Supports
- Parallel Commits staging and recovery rule
- Reduced cross-range transaction commit latency
- November 2019 publication milestone
- https://www.narayana.io/docs/product/index.html
Supports
- JTA, JTS, XA-resource enlistment, and recovery modules
- Transaction log records and replay of completion
- Compensating transaction material for long-running work
- https://www.narayana.io/documentation/
Supports
- Narayana learner documentation destination
- Awesome Links rationale for transaction-manager study
- Narayana product landscape placement
- https://shardingsphere.apache.org/document/current/en/features/transaction/
Supports
- LOCAL, XA, and BASE transaction modes
- XA fit for short transactions and its concurrency cost
- ShardingSphere product and Awesome Links placement
- https://palantir.github.io/atlasdb/html/transactions/transaction_protocol.html
Supports
- AtlasDB MVCC transaction protocol and Percolator influence
- Start timestamps, write conflicts, and transaction status records
- Awesome Links rationale
- https://learn.microsoft.com/en-us/azure/architecture/patterns/saga
Supports
- Saga sequence of local transactions
- Compensating actions and long-running workflow fit
- Difference between compensation and atomic rollback
- https://www.vldb.org/dblp/db/conf/ac/Gray78.html
Supports
- 1978 publication of Notes on Data Base Operating Systems
- Transaction management, locking, and recovery foundations
- https://bwlampson.site/21-CrashRecovery/WebPage.html
Supports
- June 1979 crash-recovery report
- Atomic transactions across independent computers
- Two-phase commit terminology in early distributed recovery work
- https://doi.org/10.1145/582318.582339
Supports
- April 1981 Nonblocking Commit Protocols publication
- Conditions and designs for commit progress after failures
- https://doi.org/10.1145/7239.7266
Supports
- December 1986 R star transaction-management publication
- Presumed Abort and Presumed Commit extensions
- Reduced messages and log writes under presumed outcomes
- https://www.postgresql.org/docs/8.1/release-8-1.html
Supports
- November 2005 PostgreSQL 8.1 release date
- Addition of two-phase commit commands and `pg_prepared_xacts`
- https://github.com/sindresorhus/awesome
Supports
- Starting catalog used for required awesome-list discovery
- Discovery of database, Java, and microservices awesome lists
- https://github.com/mgramin/awesome-db-tools
Supports
- Discovery of Apache ShardingSphere as a distributed SQL transaction engine
- https://github.com/YunaiV/awesome-java
Supports
- Discovery of Narayana in the Distributed Transactions section
- https://github.com/mfornos/awesome-microservices
Supports
- Discovery of AtlasDB and TiKV as transactional storage projects
- https://www.oracle.com/database/
Supports
- Oracle Database product landscape entry and homepage
- https://www.microsoft.com/en-us/sql-server/
Supports
- SQL Server product landscape entry and homepage
- https://learn.microsoft.com/en-us/host-integration-server/core/two-phase-commit2
Supports
- Microsoft Distributed Transaction Coordinator role in 2PC
- SQL Server and external transactional participant arrangement
- https://www.postgresql.org/
Supports
- PostgreSQL product landscape entry, homepage, and open-source status
- https://www.mysql.com/
Supports
- MySQL product landscape entry and homepage
- https://cloud.google.com/spanner
Supports
- Google Cloud Spanner product landscape entry and homepage
- https://www.cockroachlabs.com/product/
Supports
- CockroachDB product landscape entry and homepage
- https://www.cockroachlabs.com/docs/stable/architecture/transaction-layer/
Supports
- CockroachDB cross-range atomic commit and Parallel Commits placement
- https://www.yugabyte.com/yugabytedb/
Supports
- YugabyteDB product landscape entry and homepage
- https://docs.yugabyte.com/stable/architecture/transactions/
Supports
- Transaction status records, intents, cleanup, and tablet participation
- https://www.pingcap.com/tidb/
Supports
- TiDB and TiKV product landscape entry and homepage
- https://www.narayana.io/
Supports
- Narayana product landscape entry and homepage
- https://shardingsphere.apache.org/
Supports
- Apache ShardingSphere product landscape entry and homepage
