Two-Phase Commit Explained: How Distributed Transactions Stay Consistent

Two-phase commit explained in plain terms means understanding how several databases can agree to make the same change together, or to make no change at all. The central question is what happens when a transaction spans more than one machine, and the answer is a protocol in which a coordinator first asks every participant whether it is ready, and only then tells them all to commit.

A two-phase commit is a protocol for atomic transactions across distributed systems, guaranteeing that a transaction either completes on every participating database or is rolled back on all of them. It is the classic answer to the problem of keeping data consistent when a single logical change touches several separate stores, each of which could fail independently.

The idea is simple: nobody commits until everybody agrees. In the first phase the coordinator asks each participant to prepare, which means writing the change to durable storage and promising it can be applied. In the second phase, once every participant has agreed, the coordinator tells them all to commit, and the change becomes permanent everywhere at once. This article explains what two-phase commit is, how it works, and where it is used.

Key takeaways

  • Two-phase commit keeps a transaction atomic across several databases.
  • Phase one asks every participant to prepare and promise it can commit.
  • Phase two tells all participants to commit, or to roll back together.
  • The coordinator is a single point of failure while a transaction is open.
  • It is widely used in traditional databases and enterprise systems.

What two-phase commit is

Two-phase commit, usually shortened to 2PC, is a protocol that coordinates a transaction across a group of participants, each holding part of the data. A single transaction might move money between accounts held in different databases, update stock in one store and an order in another, or write the same record to a primary and a replica, and the protocol makes sure that either all of those changes happen or none of them do.

The protocol has two roles. The coordinator is the process that manages the transaction, sending messages to the participants and collecting their answers. The participants are the databases or services that hold the data, and each one must be able to prepare a change, hold it in a recoverable state, and then either commit it or discard it when the coordinator decides.

Two-phase commit exists because partial completion is the worst possible outcome for a transaction. If money left one account but never arrived in the other, or an order was created but the stock was never reserved, the data would be wrong in a way that no later check could easily repair. Our guide to ACID explains the atomicity guarantee that two-phase commit enforces across machines, and our guide to distributed systems explains the wider setting in which several machines must act as one.

How two-phase commit works

The transaction begins when an application asks the coordinator to start one, and the coordinator records the list of participants involved. Each participant then performs its part of the work in memory and on disk, but does not yet make the change visible, holding it in a prepared state that can be either applied or discarded.

In the first phase, called the prepare phase, the coordinator sends a prepare message to every participant. Each participant checks that it can complete its part, writes the change to its transaction log so that it survives a crash, and replies with either a yes or a no. A yes is a promise: the participant guarantees it will be able to commit when asked, even if it crashes and restarts in the meantime.

The coordinator collects the votes. If every participant voted yes, the decision is to commit, and the coordinator writes that decision to its own log before telling anyone, so that the outcome is durable even if the coordinator itself fails at this point. If any participant voted no, or failed to answer in time, the decision is to abort, and the change is discarded everywhere.

In the second phase, called the commit phase, the coordinator sends the decision to every participant. On a commit, each participant applies the prepared change, makes it visible, and acknowledges. On an abort, each participant discards the prepared change and acknowledges. The coordinator waits for the acknowledgements, and once it has them all, the transaction is finished.

Recovery is built into the design through the transaction logs. If a participant crashes after voting yes, it recovers by reading its log, discovering that it promised to commit, and asking the coordinator for the decision. If the coordinator crashes, a new coordinator can take over, read the log, and complete the transaction, either committing it if the decision was recorded or aborting it if it was not.

A dark server room with glowing green lights on racks of machines, one central rack sending signals to the others
A dark server room with glowing green lights on racks of machines, one central rack sending signals to the others, shows how two-phase commit works: one coordinator, and every participant agreeing before anything is final.

Comparing two-phase commit with other approaches

Two-phase commit is one of several ways to keep distributed work consistent, and each was built with a different emphasis. Three-phase commit adds a step to reduce blocking, sagas break a transaction into separate steps with compensating actions, and single-database transactions avoid the problem entirely by keeping the data in one place.

The table below sets out the main contrasts between the leading approaches.

Aspect Two-phase commit Three-phase commit Saga pattern
Guarantee Atomic, all or nothing Atomic, all or nothing Eventual, with compensation
Blocking risk Yes, if coordinator fails Lower, but not zero No blocking
Availability Lower during failures Higher than 2PC Highest of the three
Complexity Moderate High High, needs compensations
Best for Traditional databases Systems needing less blocking Long-lived business processes

The practical result is that two-phase commit is the right choice when strict atomicity matters and the participants are reliable, three-phase commit suits systems that cannot tolerate blocking, and sagas suit long-running processes spread across many services.

Where two-phase commit is used

Relational databases use two-phase commit for distributed transactions. When a transaction in one database updates data held in another, for example across two servers in a cluster, the database engine runs the protocol behind the scenes, so that the application sees a single atomic transaction even though the work is spread across machines.

Enterprise systems use two-phase commit through transaction managers. A Java application server, for example, can coordinate a transaction that writes to a database and sends a message to a queue, using the XA standard to make both resources act as participants, so that the message is sent only if the database write commits.

Banking and finance rely on the protocol where correctness cannot be compromised. A transfer between accounts in different systems must never lose money or create it from nothing, and two-phase commit gives that guarantee, even though it costs some availability when a participant is slow or unreachable.

Message queues use the protocol to link message delivery with database work. A service that processes an order can write the order to the database and acknowledge the incoming message in one atomic transaction, so that a crash cannot cause the same order to be processed twice or lost entirely.

Cloud databases sometimes use a variant of the protocol internally. A distributed database that shards its data across nodes must commit a transaction that touches several shards atomically, and while many modern systems use other methods, the prepare-and-commit shape of two-phase commit is still the pattern they follow.

Strengths and limits

The greatest strength of two-phase commit is the strength of its guarantee. Atomicity across several machines is a hard problem, and the protocol solves it with a simple, well-understood sequence of messages that any database engineer can reason about, test, and trust in production.

It is also a strength that the protocol is standardised and widely supported. The XA interface lets different databases, queues, and application servers take part in the same transaction, so a team can build a system from several products and still get atomic behaviour across all of them.

The limits are real, though. The coordinator is a single point of failure: if it crashes after participants have prepared but before it sends the decision, those participants are blocked, holding locks and waiting, unable to decide on their own. The protocol is also slow, since every transaction needs several rounds of messages, and it does not scale well to large numbers of participants or to services spread across distant data centres.

Two-phase commit checklist

  • Use two-phase commit only when strict atomicity is truly required.
  • Keep the number of participants small to limit blocking.
  • Make sure every participant writes its prepared state to disk.
  • Set a timeout so a slow participant does not block others forever.
  • Test recovery from a coordinator crash during the commit phase.
  • Test recovery from a participant crash after voting yes.
  • Watch for long-running transactions holding locks on participants.
  • Log the coordinator decision before sending it to participants.
  • Consider a saga when the transaction spans many services.

Frequently asked questions

What is two-phase commit in simple terms?

It is a protocol that lets several databases agree to make the same change together. First every participant promises it is ready, and only then does the coordinator tell them all to commit, so the change either happens everywhere or nowhere.

What are the two phases?

The first phase is prepare, in which the coordinator asks each participant whether it can commit, and each one votes yes or no after writing the change to its log. The second phase is commit, in which the coordinator sends the final decision, and every participant applies it or discards the change.

What happens if the coordinator crashes?

Participants that have already prepared are blocked, holding their locks until a new coordinator takes over. The new coordinator reads the log, commits the transaction if the decision was recorded, or aborts it if it was not, and the participants then follow that decision.

Why is two-phase commit slow?

Every transaction needs at least two rounds of messages between the coordinator and each participant, plus durable writes to transaction logs at each step. That overhead is the price of the atomicity guarantee, and it grows as more participants are added.

What is the difference between two-phase and three-phase commit?

Three-phase commit adds a pre-commit step between prepare and commit, so that participants can recover without blocking if the coordinator fails. It reduces blocking but adds another round of messages, and it is more complex to implement correctly.

When should I not use two-phase commit?

When the transaction spans many services, when the services are spread across distant data centres, or when availability matters more than strict atomicity. In those cases a saga, or an eventually consistent design, is usually a better fit.

The bottom line

Two-phase commit explained in one line is this: ask everyone first, and only commit when every participant has promised it can.

It is the classic way to keep a transaction atomic across several machines, and while it trades availability for correctness, there are systems, above all in finance, where that trade is exactly the right one.

This article is general information about how two-phase commit works, not advice about designing transactions for a particular system.

Latest articles

Scroll to Top