$ man 2pc # all nodes commit, or none do

Distributed Transactions

A distributed transaction is a group of changes that spans more than one database, shard or service and must all succeed or all fail. Moving 100 from Alice’s account on one shard to Sam’s account on another is the classic example: debiting Alice without crediting Sam is not acceptable, even if a machine crashes halfway.

Inside one database this is plain ACID: the write-ahead log makes the commit atomic. Across machines there is no shared log, so the machines need a protocol to agree on one outcome.

Two-phase commit (2PC)

2PC adds a coordinator that asks every participant to get ready, then tells all of them the single decision.

$ ./transfer --from alice@nodeA --to sam@nodeB --amount 100
Two-phase commit, and what happens when the coordinator dies
COORDINATOR txn 81: Alice → Sam 100 log: (empty) NODE A Alice = 500 idle NODE B Sam = 200 idle PREPARE ↓ ↓ PREPARE YES ↑ ↑ YES Alice = 400 ? PREPARED · row locked Sam = 300 ? PREPARED · row locked log: COMMIT txn 81 CRASHED IN DOUBT · lock held IN DOUBT · lock held other writes to Alice and Sam wait… COORDINATOR restarted, reads log log: COMMIT txn 81 COMMIT ↓ ↓ COMMIT Alice = 400 COMMITTED · unlocked Sam = 300 COMMITTED · unlocked
NO.STEPFROMTOTYPEINFO
101coordinatorA, BPREPAREcan you commit txn 81?
202A, BcoordinatorVOTE YESchanges on disk, rows locked, promise to obey
303coordinatorits logDECIDECOMMIT written to disk
403coordinator-CRASHdecision never sent · A and B blocked
504coordinatorA, BCOMMITrecovered from log · locks released
  1. Transfer 100 from Alice (Node A) to Sam (Node B). A coordinator drives the commit.
  2. Phase 1: the coordinator sends PREPARE to every participant.
  3. Each participant writes its changes durably, locks the rows and votes YES. A YES is a promise: "I will commit if told to, even after a crash."
  4. The coordinator logs COMMIT, then crashes before telling anyone. The participants are in doubt: they promised, so they cannot abort alone, and they do not know the decision. Locks stay held and other writes block.
  5. Phase 2: the coordinator restarts, reads COMMIT from its log and resends it. Both nodes commit and release their locks. Atomic, but only after waiting.

That waiting is the key weakness of 2PC: it is atomic but blocking. A dead coordinator can freeze rows on healthy machines for as long as it stays down. 2PC also needs every participant to vote, so one slow node slows every transaction.

Three-phase commit (3PC) tries to remove the blocking with an extra round, but it assumes network delays are bounded, which real networks do not guarantee. It is taught often and used rarely.

How modern systems commit across machines

Approach Idea Atomic? Where you meet it
2PC / XA Coordinator + prepare/commit rounds Yes, blocking XA in Java/.NET, PostgreSQL PREPARE TRANSACTION, MySQL XA
2PC over consensus groups Each participant and the coordinator state are replicated with Raft or Paxos, so a single crash does not block Yes Google Spanner, CockroachDB, YugabyteDB, TiDB
Saga A chain of local transactions, each with a compensating action (“refund” undoes “charge”) No, eventually consistent Microservice workflows, Temporal, AWS Step Functions
Transactional outbox Write the business row and an “event to publish” row in one local transaction; a relay or CDC publishes it Yes for DB + event Debezium outbox, event-driven services
Kafka transactions Producer writes to several partitions and commits offsets atomically via a transaction coordinator Yes, within Kafka Exactly-once Kafka Streams and Flink sinks

Spanner also uses TrueTime (GPS and atomic clocks with a bounded error) to give every transaction a globally meaningful timestamp. See Clock.

When to use what

  • Same database, many shards: let a database built for it (Spanner, CockroachDB, YugabyteDB, Citus) handle commit. Do not build your own.
  • Database + message broker: use the outbox pattern, not a “write to DB, then publish to Kafka” dual write.
  • Several services owned by different teams: use a saga with idempotent steps and compensations. 2PC across services couples their availability.
  • Data pipelines: make sinks idempotent (upsert by key, or overwrite a partition) so a retried job never double-writes.

Common mistakes

  • Dual writes. Writing to the database and then to Kafka (or a cache) without a transaction. A crash in between leaves them disagreeing forever.
  • Forgotten prepared transactions. An orphaned PREPARE TRANSACTION in PostgreSQL holds locks and blocks vacuum until someone commits or rolls it back.
  • Sagas without compensation for every step. If step 4 fails, steps 1 to 3 must be undone, and the undo must itself be retryable.
  • Assuming exactly-once delivery. Networks deliver at-least-once; exactly-once effects come from idempotency plus transactions.

Learning Resources

Books

  • Designing Data-Intensive Applications (2nd ed.), Martin Kleppmann and Chris Riccomini: chapters on transactions and consistency/consensus
  • Database Internals, Alex Petrov: chapter 13, distributed transactions

Papers

Articles and videos