CSE Roadmap

Core tier · about 4 hours · 7 parts

Part 2 of 7 · 2 min read

Why one commit per node is not enough

A transaction across nodes must commit everywhere or nowhere.

Think of a wedding where each partner is asked "do you?" in separate rooms and told to sign at once. One signs, the other's pen runs out of ink. Now one is married and the other is not. A ceremony must first make sure that both will sign, and only then tell both to sign.

On a single node, the DDIA book explains, atomic commit comes down to one thing: the order in which data is written to disk. The commit record is written, and at that moment the transaction is committed, even if the machine crashes next. A single device makes the commit atomic.

With several nodes (for example a transaction touching two shards, or an index entry on another node than the row), sending "commit" to all of them separately is not enough. The book lists what can go wrong:

  • Some nodes find a conflict and must abort, while others commit.
  • Some commit requests are lost in the network and time out, while others arrive.
  • Some nodes crash before the commit record is written and roll back on recovery, while others commit.

Once a node has committed, it cannot take that back, because other transactions may already have read the data. A commit must be irrevocable. So a node may commit only when it is certain that every other node will also commit.

Atomic commit

Atomic commit is the problem of making all nodes in a transaction either all commit or all abort, even if messages are lost or nodes crash.

Example: a transfer debits an account on shard 1 and credits an account on shard 2. If shard 1 commits and shard 2 aborts, 100 dollars vanish. Atomic commit prevents that.

The book mentions that effects of a committed transaction can be undone later by a compensating transaction, but to the database that is a separate transaction, so correctness across the two is the application's problem. Sagas (section 4) are built on this.

Check yourself:

  • Why can a node not simply commit when it is ready and hope others do too?
  • Why must a commit never be retracted?