[reference] — from the canon (DDIA ch. 7 & 9). How to make an all-or-nothing operation span multiple nodes or services — the atomicity problem — and the isolation problem (how concurrent transactions interfere). This is the transaction cousin of consistency models: consistency is about single-object recency; isolation is about multi-object interleaving.

The kid version first

You want to move **50 leaves yours and doesn’t arrive in hers (money vanished) or arrives without leaving yours (money duplicated). Easy when both banks are on your desk. Hard when a room can get locked (a machine or network fails) right in the middle. Two ways people handle it:

  1. A referee counts everyone in. “Everyone ready to do their part? … Yes? … Okay — GO!” If anyone says “not ready,” nobody goes. (That’s Two-Phase Commit.) The danger: if the referee faints right after “ready?” but before “GO!”, everyone’s frozen holding their breath.
  2. Just do each step for real, and undo if needed. Take 50 in hers; if putting-in fails, put the $50 back in yours. No freezing, but for a moment an onlooker can see the money mid-flight. (That’s a Saga.)

Everything below is those two ideas, made precise — plus the other transaction question: when lots of kids read and write the shared banks at once, what confusing things are they allowed to see? (That’s isolation.)

Atomicity across nodes — Two-Phase Commit (2PC)

A single-database ACID transaction gives atomicity for free. Spread the data across nodes (sharding, or microservices each with their own DB) and “debit A on node 1, credit B on node 2, atomically” gets hard — either node can fail mid-way. 2PC is the classic protocol, run by a coordinator:

  1. Prepare phase — coordinator asks every participant “can you commit?” Each does the work tentatively, locks the data, writes it to durable storage, and votes yes (“I promise I can commit if told to”) or no. A yes-vote is a binding promise it cannot take back.
  2. Commit phase — if all voted yes, the coordinator writes its decision durably and tells everyone “commit”; if any voted no (or timed out), “abort.” Participants that promised must obey.

Atomicity — at a steep price:

  • It’s a blocking protocol (the fatal flaw). If the coordinator crashes after participants promised but before announcing the decision, participants are stuck: they’ve locked resources and promised, but can’t unilaterally decide (committing or aborting alone might contradict the coordinator’s decision, which some other participant may already have heard). They must wait for the coordinator to recover. 3PC adds a phase to reduce blocking but assumes bounded network delay, so it’s rarely used.
  • Latency & lock contention — multiple round-trips, and data stays locked across the whole protocol, crushing throughput.
  • Availability multiplies down — one slow/down participant blocks the entire transaction (availability multiplies).

Making 2PC less fragile: the blocking flaw is the coordinator being a single point of failure — so production systems make the coordinator itself a consensus group (its decision log replicated via Raft/Paxos), so a coordinator crash doesn’t freeze everyone. In fact atomic commit is itself a consensus problem — “get all participants to agree commit-or-abort.” 2PC is used within databases and via XA transaction managers, but across microservices it’s usually avoided.

Isolation — what concurrent transactions may see

Atomicity is “all-or-nothing per transaction.” Isolation is the other half: when many transactions run at once, which interleavings are allowed? Weaker isolation = faster + more concurrency, but more anomalies. The ladder (weak → strong), each level forbidding one more anomaly:

LevelForbidsAnomaly it still allows
Read Uncommitted(almost nothing)dirty reads — seeing another txn’s uncommitted change that may be rolled back
Read Committeddirty reads/writesnon-repeatable reads — re-reading a row gives a different value (someone committed in between)
Snapshot Isolation (Repeatable Read)non-repeatable readswrite skew & phantoms — two txns read an overlapping set, each makes a decision valid alone but not together
Serializableeverything — as if txns ran one-at-a-time(none) — the gold standard

Kid version of the anomalies: a dirty read is peeking at a change someone might still erase; a non-repeatable read is the price changing while you’re still shopping; write skew is two doctors each going off-call because they each saw the other was still on — individually fine, together nobody’s covering.

MVCC (Multi-Version Concurrency Control) is how modern databases give strong isolation without everyone blocking on locks: keep multiple versions of each row, and give each transaction a consistent snapshot (as of its start). Readers never block writers and vice-versa — they just read the version their snapshot points to. MVCC powers snapshot isolation and serializable-snapshot isolation in Postgres, Oracle, and the distributed databases in the next note. Serializability (isolation’s strongest) and linearizability (consistency’s strongest) together are strict serializability — what Spanner offers.

Sagas — the pragmatic alternative

Because 2PC is so costly, most cross-service systems use the saga pattern (data & integration patterns): a sequence of local transactions, each committing independently, with compensating transactions to undo earlier steps if a later one fails (refund, cancel, release).

The key difference: a saga gives eventual consistency, not atomic isolation. Nothing is globally locked; each step commits and is immediately visible, and failure triggers semantic rollbacks. So an observer can briefly see intermediate state (money debited, not yet credited) — the app must tolerate that. In exchange: no blocking, no distributed locks, independent service availability. For most business workflows, that trade is right. Sagas come in two shapes — orchestration (a central coordinator drives the steps) and choreography (services react to each other’s events).

Idempotency — the load-bearing requirement

Because messages get lost, duplicated, and retried, every step (saga or 2PC) must be idempotent — safe to apply more than once with the same effect. “Debit $50” applied twice on a retry is a bug; “apply debit with idempotency-key K, skip if K already seen” is safe. This is the idempotency-key pattern from the real pipeline — mandatory, not optional.

Exactly-once — the honest truth

Everyone wants “exactly-once” processing; at the messaging layer it’s impossible to guarantee (you can’t tell a lost message from a lost ack — Two Generals). What you actually build is at-least-once delivery + idempotent processing = effectively-once outcome. “Exactly-once is idempotency, not magic” is a mark of real understanding.

Key insight

Distributed transactions are two problems: atomicity (“both piggy banks change or neither”) and isolation (“what can concurrent transactions see”). For atomicity, 2PC gives all-or-nothing but blocks if the coordinator dies (fix: make the coordinator a consensus group — atomic commit is consensus); sagas trade atomic isolation for availability via local commits + compensations. For isolation, weaker levels allow more anomalies (dirty/non-repeatable reads, write skew); MVCC delivers strong isolation without lock contention via snapshots; serializable + linearizable = strict serializability. And underneath it all, everything must be idempotent, because “exactly-once” is just at-least-once + idempotency.

↑ Back to 04 — Distributed Systems