ACID, honestly
atomicity, consistency, isolation, durability. what each letter promises, and what it quietly doesn't.
- address
- everything.transactions.acid
- part
- part 11, transactions and isolation
- chapter
- chapter 1 of 11
- reading time
- 7 min
ACID is four promises about transactions. atomicity: all of it or none of it. consistency: every commit leaves the data valid, but only by the rules you wrote down. isolation: concurrent transactions don't trip over each other, to a degree you choose. durability: once COMMIT returns, a crash can't take it back. two of them postgres keeps completely. the other two are partly your job.
the picture
you know how ACID gets recited like one thing, a seal of quality on a database? it's four different promises, kept by four different parts of the system, and each one has an edge where it stops.
think of a bank transfer as a sealed envelope. atomicity: the envelope is delivered whole or not at all. consistency: the bank won't accept an envelope that breaks its rules, but only the rules printed on the form. isolation: two clerks working at once don't mix up each other's envelopes, mostly. durability: once the bank stamps your receipt, a fire in the back office doesn't undo it.
- A
- atomicity · all of a transaction's changes happen, or none of them do.
- kept by one commit record in the WAL; an aborted transaction's versions are just ignored (the commit log)
- doesn't cover that your app notices the failure and tries again.
- C
- consistency · a commit takes the data from one valid state to another.
- kept by the constraints you declare (CHECK, UNIQUE, foreign keys), and your code for every rule you didn't
- doesn't cover rules nobody told the database, like alice + bob = 200.00.
- I
- isolation · transactions running at once don't see or wreck each other's half-done work.
- kept by snapshots, row locks, and SSI at SERIALIZABLE
- doesn't cover that the default level stops every anomaly. READ COMMITTED doesn't.
- D
- durability · once COMMIT returns, the change survives a crash.
- kept by the WAL, flushed to disk before COMMIT returns, and crash recovery
- doesn't cover a dead disk, a DROP TABLE, or synchronous_commit = off.
atomicity: all or nothing
a transfer is two updates. if the second one fails, the first must not count. bank's CHECK says a balance can't go negative, and carol has 0.00:
BEGIN;
UPDATE accounts SET balance = balance + 50.00 WHERE id = 1;
UPDATE accounts SET balance = balance - 50.00 WHERE id = 3;
COMMIT;
SELECT id, owner, balance FROM accounts WHERE id IN (1, 3) ORDER BY id;| idint4 | ownertext | balancenumeric |
|---|---|---|
| 1 | alice | 100.00 |
| 3 | carol | 0.00 |
alice's +50.00 ran (UPDATE 1) and is gone anyway. the COMMIT came back as ROLLBACK, because the transaction was already aborted by the error. nothing had to be undone, either: postgres just records that the transaction aborted, and every reader skips the versions it wrote (an UPDATE is an INSERT shows the leftover row).
consistency: your rules, written down
the C is the odd one out. the database can only keep data "valid" by rules it knows: types, NOT NULL, CHECK, UNIQUE, foreign keys, triggers. above, the CHECK did that job. but bank's real rule, that alice and bob hold 200.00 between them, isn't written anywhere the database can see:
UPDATE accounts SET balance = balance + 50.00 WHERE id = 1;
SELECT sum(balance) AS alice_and_bob FROM accounts WHERE id IN (1, 2);| alice_and_bobnumeric |
|---|
| 250.00 |
250.00, committed, no complaint. that's the honest version of the C: the database keeps the constraints you declare, and your code keeps everything else. the other three letters are what make it possible for your code to do that: a transaction that goes from valid to valid won't be half-applied (A), disturbed (I), or forgotten (D).
isolation: it depends
the I promises that concurrent transactions don't see each other's half-finished work. postgres keeps that part at every level: it never shows uncommitted data. but full isolation means the result is the same as running the transactions one at a time, and at the default level, READ COMMITTED, it isn't always: lost updates, read skew and write skew all get through. only SERIALIZABLE promises the textbook I, and it keeps the promise by sometimes failing a transaction with 40001. isolation levels is the menu.
durability: COMMIT means it
when COMMIT returns, the transaction's WAL record is already flushed to disk. the table pages it changed may still be only in memory; after a crash, postgres replays the WAL to put them back. this capture runs one transfer to the end, starts a second one, and kills the server in the middle:
-- T1: committed: alice 50.00, bob 150.00
/* T2 */ BEGIN;
/* T2 */ UPDATE accounts SET balance = balance - 30.00 WHERE id = 2;
-- pg_ctl stop -m immediate, then start
SELECT id, owner, balance FROM accounts ORDER BY id; -- bob = ?| id | owner | balance |
|---|---|---|
| 1 | alice | 100.00 |
| 2 | bob | 100.00 |
T1 moves 50.00 from alice to bob.
- LOG: database system was interrupted; last known up at …
- LOG: database system was not properly shut down; automatic recovery in progress
- LOG: redo starts at 0/1622868
- LOG: redo done at 0/1624A88
- LOG: checkpoint complete: wrote 2 buffers (0.0%), wrote 3 SLRU buffers; 0 WAL file(s) added, 0 removed, 0 recycled; write=<t> s, sync=<t> s, total=<t> s; sync files=4, longest=<t> s, average=<t> s; distance=8 kB, estimate=8 kB; lsn=0/1624AB0, redo lsn=0/1624AB0
- LOG: database system is ready to accept connections
durability has edges too. this was a process crash: the operating system kept running, and so did the disk. postgres's promise rests on the disk telling the truth when it says a write is done (fsync). it doesn't cover a dead disk (that's backups and replicas), a DROP TABLE you committed on purpose, or synchronous_commit = off, which lets COMMIT return before the flush and can lose the last few transactions in a crash (what COMMIT waits for).
keylines
- atomicity: a failed statement aborts the whole transaction. COMMIT becomes ROLLBACK.
- consistency is only as good as the constraints you declare.
- isolation at the default level is partial. SERIALIZABLE is the full promise.
- durability: COMMIT returns after the WAL is on disk. a crash replays it.
- A and D are postgres's job. C and I are shared with your code.