When a single business operation has to touch multiple databases, you have two real options for keeping them consistent — and they trade correctness for availability in opposite directions
By The Coding Gopher
Published: 2026-10-02
URL: https://thecodinggopher.substack.com/p/saga-pattern-vs-two-phase-commit
A single “place order” operation might need to reserve inventory in one service, charge a card in another, and schedule a shipment in a third — each backed by its own database. In a monolith with one database, this is a single transaction: it either all commits or all rolls back, and the database guarantees that for you. Once those three pieces live behind separate services with separate databases, that guarantee disappears. There is no single database to ask for atomicity anymore.
This is the distributed transaction problem, and there are two well-established shapes for a solution: force the participants to coordinate atomically before committing anything (Two-Phase Commit), or let each participant commit locally and immediately, with a plan to undo it afterward if something downstream fails (the Saga pattern). They’re not interchangeable — they sit on opposite sides of a real tradeoff, and picking the wrong one causes very different kinds of pain.
The short version: 2PC keeps everything consistent by making everyone wait. Sagas keep everything moving by allowing things to be temporarily wrong.
The problem, precisely
In a single database, a transaction gives you atomicity: every statement in it commits, or none do. Across multiple databases — even multiple instances of the same database engine — there’s no shared commit log to make that atomic. Each database can only commit its own local transaction. The question both patterns answer is: how do you get transaction-like behavior across systems that can each only commit for themselves?
Join a community of senior engineers. Subscribe and get FREE access to System Design deep dives.
Two-Phase Commit (2PC)
2PC solves this by introducing a coordinator that all participants defer to, and splitting the commit into two rounds so no participant commits until everyone has agreed to.
Phase 1 — Prepare (vote): the coordinator asks every participant, “can you commit this?” Each participant does everything short of actually committing — validates constraints, acquires locks, writes to a durable log — and replies “yes” (ready and able to commit) or “no.”
Phase 2 — Commit or abort: if every participant voted yes, the coordinator tells all of them to commit. If even one voted no, it tells all of them to abort. Either way, every participant follows the same instruction — and that uniformity is what makes the outcome atomic.
Two-phase commit (2PC) is a distributed algorithm that ensures all nodes in a database or system either successfully commit a transaction or abort it together.
What this buys you: real atomicity and strong consistency. When 2PC completes, every participant is in the same state — either all committed or all rolled back — and no other transaction can observe a partial result, because every participant holds its locks until phase 2 resolves.
What it costs you:
-
Blocking. Every participant holds locks on the affected rows from the moment it votes yes until it receives the final decision. If the coordinator crashes after collecting votes but before broadcasting the decision, participants are stuck: they can’t safely commit or abort on their own, because they don’t know what the others were told. This is the in-doubt transaction problem, and it’s the single biggest reason 2PC has a poor operational reputation.
-
Synchronous and tightly coupled. Every participant must be online and responsive for the whole protocol to complete. One slow or unreachable participant stalls the entire transaction — and holds everyone else’s locks while it does.
-
Latency scales with the slowest participant. The transaction isn’t done until the last vote and the last ack arrive. Two network round trips to every participant, gated on the slowest one, on every write.
The Two-Phase Commit (2PC) protocol is a distributed consensus algorithm designed to ensure atomic transaction commitment across multiple database nodes or participants. In the Prepare Phase (Phase 1), a coordinator node asks all participating nodes if they are ready to commit the transaction; each participant executes the transaction locally up to the point of committing, writes its state to a write-ahead log, and votes either "Yes" (ready) or "No" (failed/aborted). In the Commit Phase (Phase 2), the coordinator evaluates the votes: if all participants voted "Yes", the coordinator broadcasts a global commit command, prompting everyone to permanently apply the changes; if any participant voted "No" or timed out, the coordinator broadcasts a global abort command, forcing all nodes to roll back their local changes to maintain data consistency.
The modern footnote: 2PC’s blocking problem is well known, and there have been decades of attempts to fix it. Three-Phase Commit adds a round to make it non-blocking in theory, but its assumptions don’t hold under real network partitions, so it never caught on. The approach that actually won is to fold the commit decision into a consensus protocol— Paxos or Raft — so the “coordinator” is a replicated, fault-tolerant group rather than a single point of failure. Google’s Spanner is the canonical example: it runs 2PC, but each participant is itself a Paxos group, which removes the “coordinator died and now everyone is stuck” failure mode. If you’re using a distributed SQL database today, this is likely what’s happening under the hood.
Where you’ll actually meet it: inside distributed relational databases keeping replicas or shards consistent (the basis of the XA standard, PREPARE TRANSACTION in PostgreSQL, JTA transactions in Java EE). It’s a sound tool within a system you control end-to-end. It’s a poor fit for coordinating independent microservices over a network — which is exactly the environment most backend systems now live in, and precisely the gap the Saga pattern was designed to fill.
Build Redis, Git, Docker & Claude Code From Scratch
Want to become a better software engineer?
Trusted by engineers at Google, NVIDIA, and Anthropic, CodeCrafters helps you rebuild technologies like Redis, Git, Docker, and Claude Code from scratch so you can deeply understand how real systems work.
We’ve partnered with CodeCrafters to give all readers an exclusive 40% lifetime discount.
💡 Engineers may expense platforms like this through their team’s learning budget.
Start Building & Unlock 40% Off
The Saga Pattern
A saga breaks the operation into a sequence of local transactions, each committed immediately and independently by its own service. If a later step fails, the saga doesn’t roll back in the database sense — it runs compensating transactionsthat semantically undo the effects of the steps that already succeeded.
The saga pattern is a design pattern that manages distributed transactions across multiple microservices by breaking them down into a sequence of smaller, independent local transactions.