Catalog My learning Blog
System Design Roadmap: 100-Day Course for Engineers · Phase 7 · Lesson 66 · 4 min read

Two-Phase Commit (2PC) Explained

On this page
  1. One business operation, two databases
  2. How two-phase commit works
  3. The blocking problem with 2PC
  4. Where 2PC still makes sense
  5. Why 2PC doesn't fit microservices
  6. Check yourself

One business operation, two databases

Moving money from an account in one database to an account in another must not end with the money debited but not credited. A single database transaction can't span both. You need either a protocol that makes the two commit together, or a design that tolerates them committing separately. The classic protocol is two-phase commit (2PC).

How two-phase commit works

A coordinator drives the transaction across participants (the databases or resource managers):

Phase 1, prepare. The coordinator asks each participant: "can you commit?" Each participant does the work, writes it durably to its log, takes the locks it needs, and votes yes (now promising it can commit no matter what) or no.

Phase 2, commit or abort. If every vote was yes, the coordinator writes its decision to its own log and tells everyone to commit. If any vote was no, or a participant didn't answer, it tells everyone to abort.

Code
coordinator -> A: prepare      A: locks rows, logs, "yes"
coordinator -> B: prepare      B: locks rows, logs, "yes"
coordinator: logs "commit"
coordinator -> A: commit       A: commits, releases locks
coordinator -> B: commit       B: commits, releases locks
Two-phase commit holds locks on every participant until all votes are in; a saga commits each step locally and undoes earlier steps with compensations.
Two-phase commit holds locks on every participant until all votes are in; a saga commits each step locally and undoes earlier steps with compensations.

The blocking problem with 2PC

After voting yes, a participant has given up the right to decide alone. It must wait for the coordinator's decision, holding its locks. If the coordinator crashes at that moment:

  • the participant can't commit (maybe another participant voted no),
  • it can't abort (maybe the coordinator already told others to commit),
  • so it waits, with rows locked, until the coordinator recovers.

Those locked rows block other transactions. One crashed coordinator can stall unrelated work across several databases. Every step also adds round trips, and the slowest participant sets the pace. In CAP terms (Day 5), 2PC chooses consistency and gives up availability.

Variants reduce the risk: making the coordinator itself highly available by replicating its log with consensus (Day 62) is how distributed databases such as Spanner and CockroachDB commit transactions across their internal shards.

Where 2PC still makes sense

  • Inside a distributed database, which runs a consensus-backed form of 2PC for you across its own shards. You just write BEGIN ... COMMIT.
  • Between a few resources in one trusted environment that support the XA standard, such as a database and a message broker, accepting the operational cost. Microsoft's MSDTC plays this role in Windows environments and is a frequent source of operational pain.

.NET developers often meet 2PC by accident. A TransactionScope that opens connections to two different databases, or sometimes two connections to the same one, is silently promoted to a distributed transaction coordinated by MSDTC. On .NET Framework that "just works" until a firewall blocks MSDTC in production. Modern .NET supports distributed transactions only on Windows, from .NET 7, so the same code throws on Linux containers. If you see a TransactionScope around calls to two databases, treat it as a design decision someone made without noticing.

PostgreSQL has its own version: PREPARE TRANSACTION. It's off by default (max_prepared_transactions = 0), and for good reason. A prepared transaction whose coordinator never returns keeps its locks indefinitely, even across database restarts, until someone commits or rolls it back by hand.

Why 2PC doesn't fit microservices

Services have their own databases, often different products, many of which (and most message brokers and HTTP APIs) don't support 2PC at all. Holding locks across service boundaries while waiting on network calls also couples availability tightly. Between services, use:

  • Sagas (Day 49): local transactions with compensations, eventually consistent.
  • The transactional outbox (Day 50): commit data and an event atomically in one database, deliver the event reliably afterwards.
  • Better boundaries (Day 52): keep data that must change atomically inside one service.
Two-phase commitSaga
ConsistencyAtomic across participantsEventual; intermediate states visible
Locks heldAcross the whole protocolOnly within each local step
Coordinator failureCan block participantsSteps resume or compensate
Requires participant supportYes (XA or built in)No; ordinary local transactions

Check yourself

That it can commit if told to, even after a crash, so it has logged the work durably and holds the locks until the coordinator decides.

Two connections promote the scope to a distributed transaction, which modern .NET supports only on Windows. Redesign with an outbox or saga, or keep the data in one database.

Participants that voted yes must keep their locks until they learn the decision, so locked rows stay unavailable until the coordinator recovers.

Services' stores and brokers often don't support 2PC, and holding locks across network calls couples availability; sagas use local transactions and compensations instead.

Inside one database system that manages its own participants, such as a distributed SQL database coordinating its shards, or between a database and a message broker that both support XA in a tightly controlled environment. Between independently owned services over a network, its blocking behavior usually outweighs its guarantees.