DEV Community

Cover image for Two-phase commit for system design interviews, explained
Gowtham Potureddi
Gowtham Potureddi

Posted on Edited on

Two-phase commit for system design interviews, explained

Two-phase commit for system design interviews, explained

Design anything that writes to two databases at once — move money between two account stores, update an order service and an inventory service in the same step — and you hit the atomic-commit problem: you need both writes to happen, or neither, but they live on separate nodes that can crash or disconnect independently. Two-phase commit is the classic protocol that makes those writes atomic. It puts a coordinator in charge of a set of participants and runs the commit in two rounds: first it asks everyone to prepare and vote, then — only if every vote is yes — it tells everyone to commit. It is a core system design interview concept because distributed atomicity is a real requirement, 2PC is the textbook answer, and understanding exactly how it votes, logs, and gets stuck is what the follow-ups are built to test.

The reason it comes up is that atomicity is easy inside one database — the storage engine gives you commit-or-rollback for free — and it disappears the moment a transaction spans two of them. There is no shared transaction log, no shared lock manager, and no way to roll back a commit that already landed on another node. The 2pc protocol rebuilds atomicity by hand: a voting phase where each participant does the work, writes it durably, and promises it can commit, and a decision phase where the coordinator makes the all-or-nothing call. This guide walks what a strong candidate names out loud: the two phases themselves, the coordinator and participant roles and the write-ahead logging that makes the protocol recoverable, the failure modes — blocking, the single point of failure, and the uncertainty window — and where 3PC and sagas fit.

Title card for a system design interview explainer on two-phase commit, showing a coordinator sending a prepare request to three participant databases that vote yes, then a commit request that finalizes all three, with a note that one no-vote forces a global abort, on a dark citrus-and-lavender background.

Why two-phase commit comes up in system design interviews

2PC rarely gets its own prompt. It appears inside a design — a payment transfer, an order system that touches two services, a design where "both writes must land together" is a stated requirement — the moment the interviewer asks how you keep two nodes consistent in one step. You say "I'll use a distributed transaction," and the follow-up is the trap: how does that transaction actually commit across two machines, what happens if one votes yes and then the coordinator dies, and how long are locks held? 2PC is a favourite probe because "make it transactional" is the easy half, and explaining the prepare/commit handshake and its blocking failure mode is where real distributed-systems understanding shows.

The interviewer rarely lets "wrap it in a distributed transaction" stand on its own. "A participant voted 'yes,' then the coordinator crashed before it sent commit or abort. The participant is holding its locks and doesn't know the outcome. What can it safely do?" That question — the uncertainty window, where a prepared participant is stuck and cannot unilaterally decide — is the one a memorised "use 2PC" answer can never survive.

The rounds on Zynter are modelled on how the loop runs, not scraped from any candidate's recall. What matters for prep is the shape: you name 2PC, and the next sentence attacks a held lock, a dead coordinator, or a participant stranded in the prepared state.

What the interviewer is really testing

A 2PC answer is read on the same four dimensions as any design decision. Problem solving — do you reach for 2PC because you need true atomicity across nodes, and can you say what each phase buys you? Communication — can you explain the prepare-then-commit handshake in one clean pass? Technical execution — do you know participants log and hold locks at prepare time, that the coordinator's decision is durable, and exactly how a crash strands a prepared participant? Time management — did you spend your words on the voting and the failure modes, not on naming a transaction manager product? Reading this guide teaches the protocol; it does not teach you to defend it while someone crashes the coordinator mid-flow.

The problem: an atomic commit across multiple nodes

Start with why this is hard, because 2PC only makes sense against the alternative. Inside one database, a multi-row write is one ACID transaction: the engine either commits all of it or rolls all of it back, using a single write-ahead log and a single lock manager. Split the write across two nodes — two databases, or two services each with their own store — and that guarantee is gone. Each node can commit or crash on its own, and nothing coordinates them.

The naive approach fails in an obvious way: write to node A, then write to node B. If A commits and then B crashes, you are left half-done, and you cannot un-commit A — the commit is durable, the locks are released, other transactions may have already read the result. You need a protocol that keeps every node undecided and holding its changes until it is certain all of them can commit. That is exactly what the two phases give you.

The sentence that scores: across separate nodes there's no shared log or lock manager, and you can't roll back a commit that already landed, so you need a protocol where every participant prepares and holds its work uncommitted until a coordinator confirms that all of them are ready — then they all commit together. Framing the problem as "nobody commits until everybody can" is what motivates the handshake.

The protocol: a prepare/voting phase, then a commit/abort phase

Two-phase commit runs as a handshake between one coordinator and the participants (the nodes that hold data the transaction touches). It is literally two round-trips:

  • Phase 1 — prepare / voting. The coordinator sends a prepare request to every participant. Each participant does the transaction's work locally, writes the changes to its write-ahead log so they survive a crash, holds the locks on the affected rows, and then votes: yes ("prepared" — I can commit this whenever you say) or no ("abort" — I can't). Voting yes is a binding promise: once a participant is prepared, it must be able to commit even if it crashes and restarts.
  • Phase 2 — commit / abort. The coordinator collects the votes. If every participant voted yes, it durably records the decision commit and sends a commit message to all; each participant finalizes its changes and releases its locks. If any participant voted no (or timed out), the coordinator records abort and tells everyone to roll back the prepared work and release locks. One no-vote forces a global abort — that is the all-or-nothing guarantee.

The key idea to state: the magic is in the prepared state between the phases. After voting yes, a participant has done the work but not committed — it is durably holding the change, ready to go either way, and it has surrendered the right to decide on its own. The sentence that scores: phase one asks every participant to do the work, log it, lock it, and vote; phase two commits only if the vote was unanimous, otherwise it aborts — so no participant ever commits until the coordinator knows all of them can.

A diagram of the two-phase commit protocol, showing phase one where a coordinator sends a prepare request to three participants that each write to their log, hold locks, and vote yes, and phase two where the coordinator sends commit to all three so they finalize and release locks, with a branch showing that a single no-vote makes the coordinator send abort to everyone instead.

System Design
HLD · Core concept
Walk both 2PC phases — prepare, vote, commit or abort — out loud

Practise on Zynter →

Coordinator, participants, and the write-ahead log that makes it recoverable

The two phases only give atomicity because every important step is logged before it is acted on, so a crash at any point has a defined recovery. This is the part strong candidates name without being asked:

  • Each participant writes a prepared record to its write-ahead log before it votes yes. If it crashes after voting and restarts, it reads that record, knows it is in the prepared state, re-acquires its locks, and asks the coordinator what the decision was. It never guesses.
  • The coordinator writes its decision — commit or abort — to its own log before it sends Phase 2 messages. That log record is the single point of truth for the outcome. If the coordinator crashes after deciding, it reads the record on restart and re-sends the decision to any participant still waiting.
  • Recovery is "ask the log." A restarting participant replays to the prepared state and queries the coordinator; a restarting coordinator replays its decision and re-announces it. Messages are retried until acknowledged, so the protocol tolerates lost messages and crashes — as long as the nodes eventually come back.

The sentence that scores: 2PC is recoverable because the prepared state and the commit decision are both durably logged before they're acted on — a participant that crashes after voting yes re-reads its log, re-takes its locks, and asks the coordinator for the outcome rather than deciding for itself. The logging is what separates 2PC from the naive "just write to both" that cannot recover.

The failure modes: blocking, the coordinator as a single point of failure, and the uncertainty window

2PC is correct, but it is not free, and naming its sharp edges is what separates a real answer from a diagram:

  • Blocking (the big one). A participant that has voted yes is stuck in the prepared state holding its locks until the coordinator tells it the outcome. It cannot commit (maybe someone else voted no) and cannot abort (maybe everyone else voted yes) — it has given up the right to decide. So if the coordinator is slow or unreachable, that participant blocks, holding locks the whole time, and anything that needs those rows waits behind it. 2PC trades availability for atomicity.
  • The coordinator is a single point of failure. If the coordinator crashes after participants have prepared but before it sends the decision, every prepared participant is stranded — holding locks, unable to proceed — until the coordinator recovers and reads its log. The protocol does not let participants safely break the tie among themselves, so the whole transaction waits on one node.
  • The uncertainty window. The interval between "I voted yes" and "I heard the decision" is the uncertainty window. In it, a prepared participant knows it can commit but not whether it should. It can't time out and abort, because the coordinator may have already decided commit and told others. That window — unbounded if the coordinator stays down — is the exact gap the interviewer's follow-up targets.

The point to state plainly: 2PC gives you atomicity by making participants hold uncommitted, locked work until a central coordinator decides — so a slow or dead coordinator blocks prepared participants indefinitely, and the coordinator itself is a single point of failure. Blocking and the uncertainty window are the follow-ups that decide the round.

A diagram of the two-phase commit blocking failure mode, showing three participants that have voted yes and are holding locks in the prepared state, while the coordinator has crashed after collecting the votes but before sending the decision, leaving the participants stuck in an uncertainty window — unable to commit or abort on their own and blocking other transactions that need the locked rows.

System Design
HLD · Core concept
Defend the uncertainty window when the coordinator dies — out loud

Practise on Zynter →

3PC, and why people reach for sagas instead

Once you can name the blocking problem, the natural follow-up is "how do you fix it?" — and there are two honest answers.

  • Three-phase commit (3PC) inserts a pre-commit phase between vote and commit. After everyone votes yes, the coordinator first broadcasts pre-commit (everyone acknowledges they're ready), and only then commit. The extra round means a participant stuck without the coordinator can infer the likely outcome from whether it reached pre-commit, which lets it time out and decide instead of blocking forever. The cost is an extra round-trip of latency, and — the sentence interviewers want — 3PC is still not safe under a network partition: a partition can split participants into groups that reach different decisions, so in practice 3PC is rarely used.
  • Sagas and the outbox pattern are what people reach for in real microservices. Instead of holding locks across nodes and depending on a coordinator, a saga runs a sequence of local transactions that each commit immediately and uses compensating transactions to undo on failure — trading strict atomicity and isolation for availability and scale. It is the practical answer when the "nodes" are separate services or third-party APIs that don't even support a prepare/commit protocol.

The sentence that scores: 3PC adds a pre-commit phase to reduce blocking, at the cost of an extra round and still no safety under partitions — so in practice, rather than make the coordinator protocol more complex, people give up cross-node atomicity and use a saga with compensations instead. Knowing when 2PC is still right — a small number of trusted nodes inside one system, strong atomicity required — versus when a saga wins, is the judgment signal.

A comparison diagram of two-phase commit versus three-phase commit versus the saga pattern, showing 2PC with a prepare and commit phase that blocks on a dead coordinator, 3PC adding a pre-commit phase to reduce blocking but still unsafe under network partitions, and a saga as a sequence of local transactions with compensations that gives up strict atomicity for availability, with a note on when to reach for each.

How to talk about two-phase commit in an interview

The probes from every section rhyme. Master these five and you can defend 2PC in any design that needs a cross-node atomic write, not just this checklist.

  • "How does 2PC actually commit across two nodes?" → A coordinator runs two phases: prepare (each participant does the work, logs it, holds locks, and votes) then commit only if every vote is yes, otherwise abort — so nobody commits until all can. (communication)
  • "A participant voted yes, then the coordinator died — now what?" → It's stuck in the prepared state holding locks in the uncertainty window; it can't unilaterally commit or abort, so it re-reads its log and waits for the recovered coordinator's decision. (technical execution)
  • "What's the main cost of 2PC?" → Blocking: prepared participants hold locks until the decision arrives, and the coordinator is a single point of failure, so it trades availability for atomicity. (problem solving)
  • "Does 3PC fix it?" → It adds a pre-commit phase to reduce blocking, but costs an extra round and is still unsafe under network partitions, so it's rarely used in practice. (technical execution)
  • "When would you not use 2PC?" → When nodes are separate services or external APIs, or when availability matters more than strict atomicity — reach for a saga with compensating transactions instead. (problem solving)

Each maps to a debrief dimension. Reading them teaches the answers; it does not teach you to deliver them while someone crashes the coordinator mid-flow — that only comes from saying them out loud.

How to prepare: a short plan

You do not need a new topic for every design. You need the 2PC vocabulary automatic, so when a design needs a cross-node atomic write you reach for the right structure — and its failure story — without thinking.

  • Day 1: Learn the two phases cold — prepare/vote then commit/abort — and the prepared state in between, until you can draw the coordinator-and-participants handshake from memory.
  • Day 2: Drill the recoverability and the failure modes — the write-ahead logging, blocking on held locks, the coordinator single point of failure, and the uncertainty window. These are where the follow-ups live.
  • Day 3: Place 2PC inside a real design (a money transfer, a two-service write): name the coordinator, walk a crash at each step, then explain when you'd switch to a saga. Then do it out loud, timed, with someone killing the coordinator mid-flow.

This explainer pairs with the broader distributed transactions and saga write-ups — 2PC, 3PC and sagas are the three answers to the same atomic-commit problem, so getting the protocol automatic pays off across the whole topic.

Frequently asked questions

What is two-phase commit?

Two-phase commit (2PC) is a protocol for making a transaction atomic across multiple nodes — several databases, or several services with their own stores — so that all of them commit or none of them do. A single coordinator drives the whole thing in two rounds. In the first phase it asks every participant to prepare: each one does the transaction's work locally, writes it to its write-ahead log, holds the locks on the affected rows, and votes yes or no. In the second phase the coordinator commits only if every participant voted yes, telling them all to finalize and release their locks; if any voted no, it tells everyone to abort. The point of the prepared state between the phases is that a participant has done the work but not committed, so it can still go either way once the coordinator makes the all-or-nothing decision.

What happens in each phase of 2PC?

Phase one is the prepare or voting phase: the coordinator sends a prepare request, and each participant executes its part of the transaction, logs the changes durably so they survive a crash, acquires and holds its locks, and then votes — yes means "prepared, I can commit this whenever you say," no means "I have to abort." Voting yes is a binding promise the participant must honour even across a restart. Phase two is the commit or abort phase: the coordinator collects the votes, durably records a single decision, and sends it to everyone. If the vote was unanimous yes, everyone commits and releases locks; if even one participant voted no or timed out, everyone aborts and rolls back the prepared work. No participant ever commits before the coordinator confirms that all of them are ready.

Why is two-phase commit a blocking protocol?

Because once a participant votes yes, it is stuck in the prepared state holding its locks until the coordinator tells it the outcome, and it cannot decide on its own. It can't commit, because another participant might have voted no; it can't abort, because everyone else might have voted yes and the coordinator might have already committed. So if the coordinator is slow, unreachable, or has crashed after collecting votes but before sending the decision, every prepared participant blocks — holding locks the whole time — and any other transaction that needs those rows waits behind it. This uncertainty window, which is unbounded if the coordinator stays down, is why 2PC trades availability for atomicity, and it's the failure mode interviewers probe first.

How does 3PC differ from 2PC?

Three-phase commit adds a pre-commit phase between the vote and the commit. After every participant votes yes, the coordinator first broadcasts a pre-commit message that participants acknowledge, and only then sends the final commit. That extra round gives participants enough information to infer the likely outcome if the coordinator disappears — so instead of blocking forever in the uncertainty window, a stranded participant can time out and decide — which reduces the blocking problem that 2PC suffers from. The trade-offs are an extra round-trip of latency and, more importantly, that 3PC is still not safe under a network partition: a partition can split the participants into groups that reach conflicting decisions. Because of that, 3PC is rarely used in practice, and teams more often drop cross-node atomicity in favour of a saga.

How do I practise a system design interview out loud?

Reading about two-phase commit is not the same as defending the uncertainty window while an interviewer crashes your coordinator mid-flow. On Zynter, the HLD track runs live voice rounds modelled on how real system design loops run — you talk through a design (like a money transfer or a two-service write) on a shared canvas, and a voice interviewer follows up one question at a time ("how does it commit across two nodes?", "the coordinator just died after the votes — now what?", "why not just use a saga?"), then gives you a debrief on problem solving, communication, technical execution and time management. The score is a practice signal, not a hiring decision.

Practise it on Zynter

You can say "use two-phase commit." The round is won on the prepared state and the dead coordinator.

Pick an HLD system design round with a cross-node atomic write, lay out the coordinator and participants on the canvas, and defend it out loud to a voice interviewer that keeps asking "how does it commit across two nodes?" and "a participant voted yes, then the coordinator crashed — now what?" — then read the debrief. Zynter.ai is on-demand mock interviews modelled on real company loops, with 150+ rounds across 20 companies including the HLD system design track (Sep 2026).

Practise an HLD system design round →
Browse all interview tracks

Top comments (0)