Skip to content
Contents/CH 21
Transactions & Recovery/Intermediate · 14 min

Transactions & ACID

A transaction is a unit of work that either happens completely or not at all. ACID is the promise that keeps a database trustworthy.

In plain English

A bank transfer is all-or-nothing: the money leaves one account and arrives in the other, even after a crash.

The one idea

A transaction is an all-or-nothing unit that moves the database from one consistent state to another.

Money, inventory, and bookings are not allowed to be half-updated. ACID is the promise that a crash or a concurrent reader cannot leave a partial fact behind.

By the end

  1. 01Define a transaction and its boundaries.
  2. 02Explain atomicity, consistency, isolation, and durability.
  3. 03Contrast commit and rollback.
  4. 04See ACID in a bank-transfer example.
01

What a transaction is

A transaction groups several operations into one logical unit. It begins with a start marker and ends with COMMIT or ROLLBACK. The DBMS must make the whole group behave as if it ran alone and either fully succeeded or never happened.

chapter-21.db
BEGIN;
UPDATE Account SET balance = balance - 100 WHERE id = 1;
UPDATE Account SET balance = balance + 100 WHERE id = 2;
COMMIT;
02

ACID

Atomicity: all operations succeed or none do. Consistency: the transaction moves the database from one valid state to another. Isolation: concurrent transactions do not see each other's uncommitted work. Durability: once committed, the change survives a crash. Each property needs a different mechanism: logging, constraints, concurrency control, and stable storage.

03

Commit and rollback

COMMIT makes the transaction durable and visible. ROLLBACK undoes its changes. A transaction may also abort automatically on a constraint violation, deadlock, or crash. The recovery system uses the log to undo uncommitted work and redo committed work.

Worked example3 steps

Move 100 from A to B

Account A has 500 and account B has 300. A transfer debits A and credits B.

01

Debit A by 100.

A becomes 400 inside the transaction, not yet durable.

02

Crash before crediting B.

Recovery rolls back the debit; A returns to 500.

03

Retry and commit both writes.

A is 400, B is 400, and the total is unchanged.

ConclusionAtomicity protects the pair of writes. Consistency protects the balance rule. Isolation hides the intermediate state. Durability survives the crash.

The trade-off

No free correctness.

Gain

A correctness boundary that applications can rely on.

Cost

Locks, logging, fsync, and coordination all cost throughput.

Verdict

Use transactions for any multi-step invariant; relax isolation only with a measured anomaly budget.

Recap

What to keep.

Takeaways

  • +A transaction is an all-or-nothing unit of work.
  • +Atomicity is all or nothing; consistency preserves invariants.
  • +Isolation hides uncommitted changes; durability survives crashes.
  • +COMMIT makes changes permanent; ROLLBACK undoes them.
  • +Recovery uses logs to redo and undo.

Traps

  • !Leaving a transaction open and holding locks.
  • !Assuming COMMIT is free; it forces durability work.
  • !Confusing consistency with isolation.

Focus

  • →Explain each ACID property with a bank transfer.
  • →Know what COMMIT and ROLLBACK do.
  • →Name the mechanism behind durability.

Check yourself

Before you turn the page.

0 / 2 answered
01

Which ACID property ensures a committed change survives a crash?

02

What does ROLLBACK do?

DB//COSMOS

A field manual for database systems: model the domain, normalize the facts, query the relations, and protect the result.

Edition 2026 · 32 chaptersTopic catalogInterview prep