Notestransactions🌱 seedling

Distributed Transaction

Also: Distributed Transactions · Cross-Shard Transaction · Atomic Commit · Atomic Commitment

A transaction whose writes span several nodes (shards, databases or services) and must still be atomic: every participant commits or every participant aborts. It needs an atomic commit protocol, usually [[Two-Phase Commit]].

Why it exists

On one node, atomicity is one durable commit record. Across nodes there are several logs and nothing ties their outcomes together: commit on one shard, fail on the other, and the commit can't be taken back because others may already have read it.

How it works

  • Atomic commit: no participant commits until all are known to be able to. Two-Phase Commit does this with a prepare round and a logged decision.
  • Non-blocking variant: run 2PC over Consensus (Paxos or Raft groups), so the decision survives a coordinator crash.
  • Avoid it: pick shard keys so related rows share a shard; coordinate services with a saga and outboxes instead.

Trade-offs

Approach Gains Costs
2PC atomic and isolated blocking on coordinator loss; locks held across round trips
2PC over consensus non-blocking every phase is a consensus round
saga no cross-service locks no isolation; compensations to write
co-locate data no protocol at all constrains the data model and shard key

In practice

  • Spanner, CockroachDB run 2PC over Paxos/Raft; cross-shard transactions are serializable by default.
  • DynamoDB TransactWriteItems: up to 100 actions in one Region; two underlying writes per item.
  • Vitess defaults to best-effort MULTI (partial commits possible); atomic TWOPC is opt-in.

Pitfalls

  • Treating a Timeout as "it failed": the other side may have committed.
  • A shard key that splits rows usually written together turns every write into a distributed transaction.

Recall

These cards are also in your review deck, scheduled with spaced repetition.