Skip to content
learn
  1. Learn
  2. High Level System Design
  3. Distributed Transactions and Workflows

System Design HLD-10.01

Two-phase commit

How two-phase commit makes a transaction atomic across databases, why a coordinator crash leaves participants blocked, and why microservices avoid it.

Advanced 12 min Last reviewed

The big idea

Inside one database, a transaction is atomic: transfer 50 from account A to account B, and either both rows change or neither does. Now put account A in one database and account B in another. Each database can commit its own half, but nothing stops one from committing while the other fails. You need a protocol that makes several independent databases agree on a single outcome: everyone commits, or everyone aborts.

Two-phase commit (2PC)A protocol that commits a transaction atomically across several participants: first every participant promises it can commit (prepare), then a coordinator tells all of them to commit or all to abort. is that protocol. A coordinatorThe process that runs a two-phase commit: it collects votes from participants and decides and announces commit or abort. first asks every participant "can you commit?" and only when all say yes does it tell them to commit. The catch is in the middle: once a participant has said yes, it has given up the right to decide on its own.

Think of a wedding ceremony. The officiant asks each person "do you?" and both say "I do". At that point neither can walk away alone; they are waiting for the officiant to pronounce the result. If the officiant faints right then, both stand at the altar, unable to leave and unable to proceed.

How it works

Step 1: Phase one, prepare

The application does its work on each participant inside a local transaction, then asks the coordinator to commit. The coordinator sends prepare to every participant. Each participant checks it can commit (constraints pass, no conflicts), writes everything needed to commit to durable storage, keeps its locks, and votes:

  • Yes means a binding promise: "whatever happens, even if I crash and restart, I will be able to commit if told to."
  • No means it will abort, and it can do so immediately.

Step 2: Phase two, commit or abort

If every vote is yes, the coordinator writes commit to its own log (this is the moment the transaction is decided) and sends commit to all participants. If any vote is no, or a participant does not answer in time, it sends abort. Participants apply the decision, release their locks and acknowledge. Coordinator and participants retry until every acknowledgement arrives.

Two-phase commit with a coordinator crash after prepare Sequence diagram with a coordinator and two participant databases. The coordinator sends PREPARE to both; both vote YES, having made their changes durable and kept their locks. The coordinator then crashes before announcing a decision. Both participants are in doubt, holding locks. After the coordinator recovers, it sends COMMIT to both. Coordinator Orders DB participant Payments DB participant PREPARE PREPARE YES (durable, rows locked) YES (durable, rows locked) CRASH before writing the decision both wait, holding locks: in doubt after recovery: COMMIT after recovery: COMMIT Figure 1. Both participants vote yes and then the coordinator crashes. Having promised to commit, neither may abort alone, so both hold their locks, in doubt, until the coordinator returns. Step through the timeline.
  1. PREPARE The coordinator asks the orders database whether it can commit its part.
  2. PREPARE It asks the payments database the same question.
  3. YES (durable, rows locked) Orders writes its changes durably, keeps its locks and promises to commit if told to.
  4. YES (durable, rows locked) Payments does the same. Both are now prepared and can no longer decide on their own.
  5. CRASH before writing the decision The coordinator crashes after collecting the votes but before announcing the decision.
  6. both wait, holding locks: in doubt Both participants are in doubt. They promised to commit if told to, so they cannot abort on their own, and they do not know whether the other side voted yes. Their locks block other transactions until the coordinator recovers.
  7. after recovery: COMMIT When the coordinator restarts, it reads its log, decides, and sends the decision. Only then are the locks released.
  8. after recovery: COMMIT Both participants commit, and the transaction is atomic after all, but everyone waited for the coordinator.

Step 3: The blocking problem

Look at the moment after both participants voted yes and the coordinator vanished. A participant cannot commit, because maybe the other side voted no and the coordinator decided abort. It cannot abort, because maybe the coordinator decided commit and the other side already committed. It is in doubtThe state of a prepared participant in two-phase commit that has voted yes but has not learned the decision, so it can neither commit nor abort on its own., and it must keep its locks until it learns the decision. Every other transaction that touches those rows waits too.

2pc.pyruns with python3 (standard library)
class Participant:
    def __init__(self, name, can_commit=True):
        self.name, self.can_commit, self.state = name, can_commit, "idle"
    def prepare(self):
        # Vote YES only after making the change durable and locking the rows it touches.
        self.state = "prepared (rows locked)" if self.can_commit else "aborted"
        return self.can_commit
    def finish(self, decision):
        self.state = decision + "ted" if decision == "commit" else "aborted"

def two_phase_commit(parts, crash_after_prepare=False):
    votes = [p.prepare() for p in parts]                 # phase 1: prepare (voting)
    decision = "commit" if all(votes) else "abort"
    if crash_after_prepare:
        return "coordinator crashed before sending the decision"
    for p in parts:                                      # phase 2: commit or abort
        p.finish(decision)
    return decision

for label, parts, crash in [
    ("all vote yes", [Participant("orders"), Participant("payments")], False),
    ("payments votes no", [Participant("orders"), Participant("payments", can_commit=False)], False),
    ("coordinator crash", [Participant("orders"), Participant("payments")], True),
]:
    result = two_phase_commit(parts, crash)
    print(f"{label:<18} -> {result}")
    print("   ", ", ".join(f"{p.name}: {p.state}" for p in parts))

Output

all vote yes       -> commit
    orders: committed, payments: committed
payments votes no  -> abort
    orders: aborted, payments: aborted
coordinator crash  -> coordinator crashed before sending the decision
    orders: prepared (rows locked), payments: prepared (rows locked)

The first two runs end cleanly: all commit, or all abort when one participant votes no. In the third, both participants are left prepared with rows locked, and nothing in the protocol lets them get out without the coordinator. That is why 2PC is called a blocking protocol: the failure of one node can stall others indefinitely.

Step 4: Why 2PC is rare between microservices

2PC is used inside databases and between databases that support it, through the XA standard or commands such as PostgreSQL's PREPARE TRANSACTION. Between microservices it is rare, because:

  • Availability: every participant must be up to commit, and a coordinator outage blocks everyone. The more services join, the more likely one is down.
  • Latency and locks: locks are held across at least two network round trips, which hurts throughput under contention.
  • Support: message brokers, HTTP APIs, third-party services and many databases cannot take part in a 2PC at all. You cannot ask a payment provider to "prepare" a card charge.
  • Coupling: services must share a transaction manager and agree on a protocol, defeating the point of independent services.

So microservices usually accept eventual consistency and use sagas ( Sagas: choreography and orchestration ), the outbox pattern ( Transactional outbox and change data capture ) and idempotency instead.

Going deeper

Making the coordinator fault tolerant

The blocking problem comes from a single coordinator whose decision lives only in its own log. Systems such as Google Spanner remove it by replicating the coordinator and every participant with a consensus protocol ( Consensus: Raft ). The decision is recorded in a Paxos or Raft group, so a single machine failing does not block anyone: another replica takes over and knows the outcome. 2PC over consensus groups is how many distributed SQL databases implement cross-shard transactions.

Three-phase commit

Three-phase commit adds an extra phase so participants can decide without the coordinator. It only works if network delays are bounded, an assumption real networks break ( Why distributed systems are hard ), so it is rarely used in practice. Consensus-replicated coordinators are the practical answer.

Trade-offs

OptionWins whenLoses when
Two-phase commit (XA) A few databases in one trusted environment that support it, needing strict atomicity Availability and latency; blocks on coordinator failure; most services cannot participate
2PC over consensus groups (Spanner style) Distributed SQL databases with cross-shard transactions Built into the database; write latency of consensus rounds
Sagas with compensations Business flows across independent services and external APIs Atomicity: intermediate states are visible and must be compensated
Single database Data that must change together and can live together Independent scaling and ownership of that data

Common mistakes

  • Expecting 2PC to survive a coordinator crash without blocking.
  • Using distributed transactions across microservices and inheriting the availability of the least reliable one.
  • Forgetting that prepared transactions hold locks, causing pile-ups under load.
  • Trying to include an external API or a message broker in a 2PC that it does not support.
  • Splitting data across services that actually needs to change atomically; keeping it in one database is often simpler.

In the wild

Relational databases expose 2PC for use with external transaction managers: PostgreSQL's PREPARE TRANSACTION and the XA standard supported by MySQL and Java application servers. Google Spanner and distributed SQL databases inspired by it run two-phase commit across shards whose state is replicated with consensus. Kafka's transactions use a transaction coordinator with prepare and commit markers in a similar two-phase style.

Check yourself

  1. What does a participant promise when it votes YES in the prepare phase?

    Show answer

    B. That it can and will commit if told to, even after a crash A yes vote is binding: the participant made its changes durable and gave up the right to abort on its own.

  2. Both participants voted yes and the coordinator crashed. What can they safely do?

    Show answer

    C. Wait for the coordinator (or someone who knows the decision), holding their locks Either choice could contradict the coordinator's decision, so they are in doubt and must wait.

  3. Why is 2PC rare across microservices?

    Show answer

    B. Every service must be available and support the protocol, locks are held across round trips, and a coordinator outage blocks everyone It trades availability and independence for atomicity, which is usually the wrong trade between services.

  4. How do systems like Spanner avoid 2PC's blocking problem?

    Show answer

    B. They replicate the coordinator and participants with consensus, so the decision survives a machine failure With the decision stored in a consensus group, another replica can take over and finish the protocol.

Recap

  • 2PC makes several participants commit or abort together using a coordinator.
  • Prepare: every participant makes its changes durable, keeps locks and votes; commit: the coordinator announces the outcome.
  • A yes vote is binding, so if the coordinator dies, prepared participants are blocked, holding locks.
  • 2PC is used within and between databases, often with consensus-replicated coordinators, but rarely between microservices.
  • Across services, prefer sagas, outbox and idempotency, or keep tightly coupled data in one database.

Get new lessons by email

Get new posts on backend and AI internals. No spam, unsubscribe anytime.

Your email is only used to send new posts. Privacy