All or nothing

Think of it when one action needs several writes, and a half-finished set would be wrong.

In everyday life

A trade at a market stall: coins go one way and goods go the other, together. If either side pulls back, both keep what they had, and nobody ends up with only half the trade.

One case

A library’s system lends a book in two writes: record a loan for Mia, then mark the copy “on loan”. At 10:14 the server crashes between the two writes. The loan exists, but the copy still shows as available, and at 10:20 another borrower reserves the book that Mia already has.

All or nothing
The ideaHow data is stored
All or nothing

A group of changes that the database applies completely or not at all. If one step fails, every step is undone.

How data is stored, Foundations

Every case

Any action with more than one write can stop in the middle: a crash, a timeout, a bug. Without a transaction you get a state that no correct program would create. With one, the database keeps either the old state or the new state, never a mix of the two. It covers only the writes inside that one database: a card payment or an email handled by another company stays outside it.

The word

Engineers call this

transactionA group of changes that the database applies completely or not at all. If one step fails, every step is undone.

When several changes to one database must happen together, put them in one transaction, so a failure leaves nothing half done.

Where this idea leads

Skills that encode it

  • principle-make-operations-idempotent pstack“Write to a temporary path and rename it into place”, so a crash never leaves a half-written file.