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
Intro
Distributed Transactions and Two-Phase Commit
A distributed transaction is one transaction whose work spans more than one independently managed resource. A resource might be a database, a database shard, or a transactional message system. The transaction must finish with one atomic outcome: every participant commits, or every participant rolls back.
Two-phase commit, abbreviated 2PC, is the classic protocol for reaching that atomic outcome. A transaction manager acts as the coordinator. Each resource manager controls one participant, sometimes called a branch. The coordinator first asks every participant whether it can commit. It then records and announces the final decision.
This protocol solves atomic commitment. It does not solve every distributed-systems problem. It does not choose the correct business operation, provide isolation by itself, or keep an application available through every failure. Its guarantee depends on durable records, correct recovery, and participants that honor the protocol.
Why a local commit is not enough
Consider a transfer between two databases. One database debits an account. The other credits another account. If the first local commit succeeds and the second fails, the overall transfer is incomplete. Calling the second database again might help, but a timeout does not reveal whether its first attempt committed.
A distributed transaction gives both writes one global transaction identifier. The coordinator tracks the global transaction. Each resource manager tracks its local branch. The protocol delays the irreversible outcome until every branch reports that it is prepared.
The important invariant is atomicity, not simultaneous execution. Participants can receive the final decision at different times. They must eventually apply the same recorded decision.
The actors and durable state
Continue the course
This section is part of the paid course.
See pricing to subscribe, or log in if you already have access.
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
