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.
| NO. | STEP | FROM | TO | TYPE | INFO |
|---|---|---|---|---|---|
| 1 | 01 | coordinator | A, B | PREPARE | can you commit txn 81? |
| 2 | 02 | A, B | coordinator | VOTE YES | changes on disk, rows locked, promise to obey |
| 3 | 03 | coordinator | its log | DECIDE | COMMIT written to disk |
| 4 | 03 | coordinator | - | CRASH | decision never sent · A and B blocked |
| 5 | 04 | coordinator | A, B | COMMIT | recovered from log · locks released |
- Transfer 100 from Alice (Node A) to Sam (Node B). A coordinator drives the commit.
- Phase 1: the coordinator sends PREPARE to every participant.
- 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."
- 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.
- 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 TRANSACTIONin 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
- Consensus on Transaction Commit: Jim Gray and Leslie Lamport, why 2PC blocks and how Paxos Commit fixes it
- Spanner: Google’s Globally-Distributed Database
- Sagas: Garcia-Molina and Salem, 1987
Articles and videos
- Pattern: Saga and Pattern: Transactional outbox, Chris Richardson
- Distributed Systems 7.1: Two-phase commit, Martin Kleppmann
- Handle Distributed Transactions: 2PC, 3PC, Saga