CMD Guide
HomeDatabasesTransactions & Concurrency Control

Transactions & ACID — Atomicity and the Write-Ahead Log

All-or-nothing, even when the server dies

Transfer $100 from A to B: debit A, then credit B. If the database crashes between those two steps, $100 has vanished. A transaction bundles statements so they either all commit or none do — the property called atomicity.

A transfer crashes after debiting A but before crediting B; without a transaction money is lost, with one the WAL undoes the debit on restart
A transfer crashes after debiting A but before crediting B; without a transaction money is lost, with one the WAL undoes the debit on restart

ACID, letter by letter (and who owns each letter)

BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 'A';
UPDATE accounts SET balance = balance + 100 WHERE id = 'B';
COMMIT;   -- both or neither; on error -> ROLLBACK

Isolation is not a slogan: anomalies and defaults

Without naming anomalies, "I" is marketing. Four classic read anomalies (full traces live on the Isolation Levels page; you need the names now):

AnomalyWhat goes wrongClosed by (typical)
Dirty readYou read another txn's uncommitted write; it may roll backRead Committed and above
Non-repeatable readSame row, read twice in one txn, two different committed valuesRepeatable Read / Snapshot Isolation
PhantomSame predicate, re-run SELECT, different set of rows (insert/delete in range)SI snapshot freezes classic phantom reads; lock engines use gap locks; true serializability needs SERIALIZABLE
Write skew / lost updateTwo txns read overlapping state, each writes a different row; both commit; invariant brokenSELECT FOR UPDATE, atomic UPDATE, or SERIALIZABLE (SSI)

Defaults matter — "ACID" does not mean "serializable by default":

So an app that "uses transactions" on Postgres still runs at RC unless you raise the level. Multi-statement money, inventory, or capacity checks need an explicit level (or SELECT … FOR UPDATE / single atomic UPDATE) — not faith in the word ACID.

How the database delivers atomicity + durability: the WAL

Before changing a data page, the DB appends the change to a write-ahead log and fsyncs that first. On COMMIT it only needs the log durable (sequential write = fast). On crash recovery it replays the log: REDO committed transactions, UNDO uncommitted ones — restoring exactly the all-or-nothing boundary.

Why both REDO and UNDO exist: STEAL / NO-FORCE

Recovery is not an arbitrary design — it is forced by two buffer-manager policy answers:

Real engines (Postgres, InnoDB, ARIES-style systems) choose STEAL + NO-FORCE for throughput. That single choice is why both REDO and UNDO appear on restart. FORCE would make REDO optional but slow every commit to random page I/O; NO-STEAL would make UNDO optional but starve the buffer pool under long writers. (Full ARIES trace and 2×2 matrix: the Transactions deep-dive recovery page.)

Pitfalls

Interview drills

  1. L1: Name A, C, I, D in one sentence each. Which are purely engine properties?
    Answer: A all-or-nothing, C valid state transition, I concurrent anomaly control, D commit survives crash. A/I/D engine; C shared (constraints + app).
  2. L2: Why does recovery need both REDO and UNDO?
    Answer: STEAL ⇒ uncommitted pages may be on disk ⇒ UNDO; NO-FORCE ⇒ committed pages may not be on disk ⇒ REDO.
  3. L3: Postgres defaults to READ COMMITTED. Can two statements in one transaction see different committed values of the same row?
    Answer: Yes — non-repeatable read is allowed at RC. Raise to REPEATABLE READ / SERIALIZABLE or use FOR UPDATE for critical rows.
  4. L4: Client got COMMIT success, then the primary's disk failed before a replica applied the change. Is durability violated?
    Answer: Single-node durability held if WAL was fsynced on the primary; cross-replica durability is a separate replication/sync-commit guarantee.

Takeaways


Re-authored for this guide; crash/atomicity diagram hand-authored as SVG. Follows Designing Data-Intensive Applications ch. 7, the ARIES recovery rationale (STEAL/NO-FORCE), and the PostgreSQL / InnoDB isolation defaults. See also: Isolation Levels & Anomalies, MVCC & Locking, Transactions deep dive (recovery/SSI).

🎯 STRICT STANDOUT: Why / mental model / when-not / worked / failure / hostile panel — Transactions & ACID — Atomicity and the Write-Ahead Log

Why this concept exists (judgment layer)

Entry page for transactions: A/I/D are engine mechanisms; C is shared; I is anomaly menu + default levels — not the slogan 'ACID means safe.' STEAL/NO-FORCE explains REDO+UNDO.

Mental model (install this intuition)

Atomicity = commit record all-or-nothing. Durability = WAL fsync. Isolation = level-selected anomaly control. Consistency = declared constraints + app invariants in one txn. Defaults: PG RC, MySQL InnoDB RR — neither is full serializable.

Worked example with numbers or traced steps

BEGIN; A-=100; crash; B+=100 never runs
Without txn: money gone. With WAL: UNDO debit on recovery
STEAL: dirty uncommitted pages may hit disk → need UNDO
NO-FORCE: commit does not flush all data pages → need REDO
PG RC: two SELECTs in one txn can see different committed values of same row

When NOT to use / named alternative

Do not wrap every read in long SERIALIZABLE transactions (lock/SSI abort cost). Do not assume multi-statement safety under autocommit. Single-row atomic UPDATE may beat multi-statement txn for simple counters.

Failure mode & ops fingerprint

Fingerprint: transfer as three autocommit statements; 'we use Postgres so we're serializable'; fsync off; long txn holds vacuum; write skew on two doctors on-call both going off duty under SI.

Hostile-panel drills (defend the decision)

Q1. Which ACID letters are purely engine?
Model answer: A, I (mechanism), D. C is shared: engine constraints + application transaction logic.

Q2. Why both REDO and UNDO?
Model answer: STEAL ⇒ uncommitted data may be on disk ⇒ UNDO losers. NO-FORCE ⇒ committed data may only be in WAL ⇒ REDO winners.

Q3. Postgres default and non-repeatable read?
Model answer: READ COMMITTED — yes, two statements can see different committed values; raise level or FOR UPDATE for critical sections.

Production judgment

Production reading of ACID: Atomicity is not “try/catch.” It is “all durable effects of this business action appear or none do” under crash. Durability is not “we wrote to Postgres.” It is “after commit returns, a power loss still leaves the effects.” Isolation is which concurrent histories you allow — and money systems that ignore write skew / lost update invent free inventory.

Tie to private labs: Ledger (balanced legs in one txn) · Idempotent payment (atomic claim) · WAL (durability mechanism) · Job queue (lease state durable).

Staff drill: Name one anomaly your isolation level still allows, and whether a payment checkout can hit it.

🤖 Don't fully get this? Learn it with Claude

Stuck on Transactions & ACID — Atomicity and the Write-Ahead Log? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Transactions & ACID — Atomicity and the Write-Ahead Log** (Databases) and want to truly understand it. Explain Transactions & ACID — Atomicity and the Write-Ahead Log from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Transactions & ACID — Atomicity and the Write-Ahead Log** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Transactions & ACID — Atomicity and the Write-Ahead Log** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Transactions & ACID — Atomicity and the Write-Ahead Log** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes