Two-Phase Commit and Its Limits
On this page
All or nothing across two databases
Moving $100 from an account in database A to one in database B needs both changes or neither. Each database has local transactions, but nothing ties them together. Commit A, then crash before committing B, and $100 disappears.
Two-phase commit (2PC) coordinates the outcome. One node acts as coordinator; the databases are participants.
How two-phase commit works
Phase 1: prepare.
- The coordinator sends Prepare to every participant.
- Each participant does all the work, writes it durably to its log, keeps its locks, and votes Yes (it promises it can commit, even after a crash) or No.
Phase 2: commit or abort.
- If every vote is Yes, the coordinator writes commit to its own log, then tells everyone to commit. If any vote is No or times out, it tells everyone to abort.
- Participants apply the decision, release locks and acknowledge.
The coordinator's logged decision is the moment the transaction commits. After that, it must keep retrying until every participant has applied it.
The weak point: in-doubt participants
A participant that voted Yes has given up its right to decide. It can't commit alone (another participant may have voted No), and it can't abort alone (the coordinator may already have decided commit). If the coordinator crashes after collecting votes, these participants are in doubt: they wait, holding locks, until the coordinator recovers.
coordinator: Prepare -> A, B
A: work done, logged, locks held -> Yes
B: work done, logged, locks held -> Yes
coordinator: crashes before sending the decision
A, B: in doubt; rows for both accounts stay locked
other transactions touching those rows: blockedThat's why 2PC is called a blocking protocol. Every client waiting on those rows waits too. Operators sometimes resolve in-doubt transactions by hand (PostgreSQL lists them in pg_prepared_xacts, and COMMIT PREPARED or ROLLBACK PREPARED settles each one).
Two-phase commit in PostgreSQL
PostgreSQL exposes the participant side directly. PREPARE TRANSACTION is phase 1: it writes the transaction durably, keeps its locks, and detaches it from your session, so it survives even if your connection or the server dies. Here the coordinator is a script holding a connection to each database:
-- on bank_a
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 'alice';
PREPARE TRANSACTION 'transfer-981-a'; -- vote yes
-- on bank_b
BEGIN;
UPDATE accounts SET balance = balance + 100 WHERE id = 'bob';
PREPARE TRANSACTION 'transfer-981-b'; -- vote yes
-- phase 2, after the coordinator has logged its decision
COMMIT PREPARED 'transfer-981-a'; -- on bank_a
COMMIT PREPARED 'transfer-981-b'; -- on bank_bTested on PostgreSQL 16, with the server killed (pg_ctl stop -m immediate) between the two phases:
after restart, pg_prepared_xacts: transfer-981-a | bank_a
transfer-981-b | bank_b
UPDATE alice from another session: ERROR: canceling statement due to lock timeout
after COMMIT PREPARED on both: alice 400, bob 200The prepared transactions survived the crash and still held their row locks: the in-doubt state from the diagram, now visible. Three practical notes. max_prepared_transactions is 0 by default, so you have to raise it before any of this works. Transaction names are unique across the whole server, not per database, so include the participant in the name. And a forgotten prepared transaction holds its locks and stops vacuum from cleaning up old row versions, so alert on any row in pg_prepared_xacts older than a few minutes.
Other costs of 2PC
- Latency: at least two round trips, plus durable log writes at every step, while locks are held throughout.
- Availability: the transaction needs every participant up; any one failing aborts it. With more participants, failures get more likely.
- Heterogeneous support: every participant must support prepare (XA in the Java world). Many services, queues and SaaS APIs don't.
Where 2PC still works
2PC isn't wrong; it's narrow. It works well when the participants are few, inside one trusted system, and the coordinator itself is replicated:
- Inside distributed databases. Spanner, CockroachDB and YugabyteDB run 2PC across shards, but each participant and the transaction record are replicated with Paxos or Raft. A crashed coordinator's decision survives on its replicas, which removes most of the blocking problem.
- Kafka transactions use a two-phase commit between the transaction coordinator and partitions (Day 21).
Three-phase commit is sometimes offered as the fix for blocking: it adds a pre-commit round so participants can finish without the coordinator. But it assumes messages arrive within a known time, and during a network partition the two sides can reach different decisions. That's why real systems replicate the coordinator with consensus instead.
Across independent microservices, each owning its database, 2PC is usually the wrong choice. Sagas (Day 23) and the outbox pattern take its place.
Check yourself
It doesn't know the outcome: committing could conflict with another participant's No, and aborting could conflict with a commit the coordinator already logged.
A participant that voted Yes must keep its locks and wait until it learns the decision. If the coordinator crashes, those participants stay in doubt, blocking other transactions on the same data, until the coordinator recovers.
The coordinator and each participant are themselves Paxos-replicated groups, so their state, including the commit decision, survives a single machine failure. A crash triggers a failover inside the group instead of leaving participants blocked.
It writes the transaction durably and keeps its locks without committing; after a restart it's still listed in pg_prepared_xacts, holding those locks, until someone commits or rolls it back.