Part 3 of 4 · 2 min read
The four letters
Atomicity, consistency, isolation and durability, each stated as one working guarantee.
The four letters
Think of a contract with four separate clauses, each protecting against one specific way the deal could go wrong.
In plain words: atomicity means a transaction's writes all happen, or none do. There is no partial result left behind. Consistency means a transaction only moves the database from one valid state to another valid state, respecting every constraint declared on it. Isolation means a transaction behaves as if it ran alone, even when other transactions are running against the same data at the same time. Durability means once a transaction is confirmed, its effect survives a crash immediately afterward.
A worked example. In the bank transfer: atomicity stops the money from disappearing if the system crashes between the two updates. Consistency stops either account's balance from becoming invalid, such as going below a constraint that forbids negative balances. Isolation stops a second transaction from reading the bookstore's balance at the exact moment it is mid-transfer and seeing an inconsistent half-finished number. Durability means that once the transfer is confirmed, a power failure one second later does not undo it.
The key lines. The same transfer, written as an explicit transaction:
BEGIN;
UPDATE accounts SET balance = balance - 2000 WHERE account_id = 'bookstore';
UPDATE accounts SET balance = balance + 2000 WHERE account_id = 'supplier';
COMMIT;
Atomicity, consistency, isolation, durability
Atomicity: all of a transaction's writes happen, or none do. Consistency: a transaction only moves the database between states that satisfy its declared rules. Isolation: concurrent transactions behave as if run one at a time. Durability: a committed transaction's effect survives a crash.
Example: COMMIT in the transfer above is the point where all four guarantees take hold together. Before it, nothing has happened yet in a way other transactions can see.
For the curious Consistency here is not the C in CAP
The consistency in ACID means a transaction respects the database's own declared rules, such as constraints. A different, unrelated use of the word "consistency," in the CAP theorem covered much later in this subject, means something about how replicas of data agree with each other. The two are easy to confuse because they share a word, not a meaning.
Quick notes
- A transaction groups one or more operations so they take effect as a single unit, started with BEGIN and finished with COMMIT.
- Atomicity: all of a transaction's writes happen, or none do.
- Consistency: a transaction only moves the database between states that satisfy its declared rules.
- Isolation: concurrent transactions behave as if run one at a time.
- Durability: a committed transaction's effect survives a crash.
- ACID's "consistency" is unrelated to the different, same-named idea used later in the CAP theorem.