Course map

Topic 069 / Core

ACID properties

Chapter 5.4 · Engines, Durability, Transactions, and Concurrency

What and why

ACID names four properties a transaction should have: Atomicity, Consistency, Isolation, Durability. It is the precise vocabulary for "this group of writes either all happened or none did, didn't corrupt the data's invariants, didn't interfere with concurrent transactions, and survives a crash." Builds on: → 68 (write-ahead logging and durability).

Diagram

Letter Guarantee Provided by
Atomicity all-or-nothing rollback on error, commit/abort as one unit
Consistency application invariants hold constraints (→ 59) plus correct application logic
Isolation concurrent transactions don't corrupt each other isolation levels (→ 70)
Durability a committed write survives a crash write-ahead log (→ 68)

How it works

  1. Atomicity: a transaction's writes commit together or not at all; a failure midway rolls back everything already done in it.
  2. Consistency here is an application-level promise — the database enforces the constraints it's given (foreign keys, checks), but the invariant itself (e.g., "an account balance is never negative") is defined by the application.
  3. Isolation bounds what a transaction can see of other concurrent transactions' uncommitted or interleaved work (→ 70).
  4. Durability is what the write-ahead log provides: once committed, a write survives a crash.
  5. All four apply within the boundary of one transaction — a boundary the application draws deliberately (→ 70), not one statement or one request by accident.

Practical example

BEGIN;
UPDATE account SET balance = balance - 100 WHERE id = 1;
UPDATE account SET balance = balance + 100 WHERE id = 2;  -- suppose this violates a CHECK
COMMIT;

If the second update violates a constraint, the whole transaction rolls back — the first account never loses the 100, because atomicity treats both statements as one unit.

When to use it

  • Use when: a state change must be all-or-nothing — money, inventory, bookings.
  • Avoid when: wrapping an operation that includes a call to an external system (a payment API, an email send) inside the database transaction — the database can roll back its own writes, not the external call (→ 139 handles these cross-system cases).
  • Failure scenarios: a transaction that calls an external API, then rolls back the database on a later error, leaving the external call's effect un-compensated.
  • Simpler alternative: a read-only report needs no explicit transaction beyond the database's default read behavior.

Practice

(15 min, lab Postgres). Wrap a two-step transfer in BEGIN/COMMIT, force a constraint violation partway through (e.g., a CHECK (balance >= 0)), and confirm the entire transaction rolled back with no partial state left behind. Done when the failed run leaves both accounts exactly as they started.

Shared lab setup

Key takeaway

ACID is the transaction's contract: atomic, invariant-respecting, isolated, durable — as a unit the application defines. Done when you can: name which mechanism provides each of the four letters. Used next in → 70.

References

None — this topic makes no external claims.

Source directory

Search the guide