LESSON 01 · The contract

Know what a transaction promises

A transaction makes a group of writes succeed or fail together, but the database can only protect the rules it has been told about.

SYSTEM VIEW

The interleaving

Diagram view
Ana—
Ben—
Total—

Watch $50 move from Ana to Ben through crashes, a log flush, a bug and a lost reply, and see which promises the database keeps.

  1. Moving $50 from Ana to Ben takes two writes, here sent as separate autocommit statements. The debit commits on its own, then the Transfer job crashes before the credit. Ana has $150 and Ben still $100: $50 has vanished.

    Should be
    $300 in all
    Is
    $250

    What changed

    • Ana $200 → $150, committed alone
    • Ben’s credit, never sent
    • $50 of the $300
  2. Replay the transfer as one transaction. The job crashes after the debit, before COMMIT, so the database discards it: Ana still has $200 and the job can safely retry. That is atomicity, a promise about failure, not concurrency; keeping concurrent transactions apart is isolation.

    Ana
    still $200
    Retry
    safe

    What changed

    • Both writes in one transaction
    • Job crashed before COMMIT
    • The uncommitted debit
  3. The retry runs the whole transaction. Its writes, then a commit record, join the write-ahead log (WAL), an append-only file, and the database acknowledges COMMIT only once the log is flushed to disk. Ana has $150 and Ben $150.

    COMMIT waits for
    the log flush

    What changed

    • Write-ahead log (WAL)
    • Ana −50, Ben +50, commit
    • Flushed, then acknowledged
  4. Now the database crashes, just after that acknowledgement. On restart it recovers from the flushed log, so Ana still has $150 and Ben $150. Durability has a limit: a failed disk with no replica can still lose a commit.

    Survives
    a crash and restart
    Can still lose it
    a failed disk, no replica

    What changed

    • Database crashed
    • Restarted from the flushed log
    • Ana $150 and Ben $150 kept
  5. The database enforces a check you declared: balances stay at or above zero. A bug now debits Ana $50 but credits Ben only $40. No declared rule breaks, so it commits and $10 is gone. Consistency is mostly your code’s job.

    Declared
    balance ≥ 0
    Not declared
    money is conserved

    What changed

    • Ana −50, Ben +40 committed
    • Every balance still ≥ 0
    • $10 of the $300
  6. With the bug fixed, the next transfer saves idempotency key 81, a unique ID, with its writes. Like every COMMIT, it waits for its flush, then commits, but the connection drops before the reply, so the job can’t tell whether it did.

    In the WAL
    key 81 and commit
    Reply lost
    outcome unknown

    What changed

    • Next transfer, key 81
    • The reply to COMMIT
    • Committed, but the job can’t tell
  7. The job retries with the same key. The first attempt saved key 81 under a unique constraint, so the retry’s insert of it fails as a duplicate: nothing changes, and the job knows the transfer is done. Ana keeps $50 instead of being debited twice.

    Without the key
    Ana $0
    With it
    Ana $50

    What changed

    • Retry with key 81
    • Seen as a repeat
    • Ana debited once

Space plays · ← → step · Home resets

THE IDEA

How it works

Moving $50 from Ana to Ben takes two writes: subtract from one row, add to another. If the server crashes between them, $50 has vanished. A transaction groups reads and writes into one unit that either commits (takes effect) or aborts (leaves no trace). ACID is the usual name for what that unit promises, and each letter is narrower than it sounds.

Atomicity means a failure before commit discards all of the transaction’s writes, so after that crash Ana still has her $200 and the application can safely try again. Isolation is the concurrency promise: transactions running together should not disturb each other, to a degree set by the isolation level. Durability means committed data survives a crash, usually because it reached a write-ahead log, an append-only file flushed to disk before the commit is acknowledged. A failed disk with no replica can still lose it.

Consistency is the odd one. It means your invariants hold, such as “transfers neither create nor destroy money.” The database enforces rules you declare, like a unique index or a check that balances stay at or above zero. It cannot know that debiting $50 and crediting $40 is wrong, and it will commit that. Consistency is mostly the application’s job, helped by atomicity and isolation.

These promises cost time: commits wait for log flushes, and conflicting work waits or retries. If the connection drops after you send COMMIT, you cannot tell whether it succeeded, so retried operations need an idempotency key, a unique ID that marks the second attempt as a repeat.

DECISION POINT

A refund job updates an order row and then writes a ledger row, as two separate autocommit statements. About once a month it crashes between them. A teammate proposes a nightly repair script. What would you do instead, and what would still be unsolved?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Atomicity is about undoing partial work after a failure, not about concurrency.
  • The database enforces only the rules you declare; the rest of consistency is your code.
  • A connection lost during COMMIT leaves the outcome unknown, so retries need idempotency.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
ApplyQUESTION 01

A bug in transfer code debits $50 and credits $40 in one transaction, and it commits. Which statement is accurate?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.