skip to content

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.
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.
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.
doesn't cover that the default level stops every anomaly. READ COMMITTED doesn't.
D
durability · once COMMIT returns, the change survives a crash.
doesn't cover a dead disk, a DROP TABLE, or synchronous_commit = off.
drawn by hand
the four letters: what each promises, what keeps the promise in postgres, and what it doesn't cover.

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:

sqldb=bank
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;
output, from postgres 18.6
#1 · BEGIN
#2 · UPDATE 1
#3 · ERROR 23514
ERROR 23514: new row for relation "accounts" violates check constraint "accounts_balance_check"
DETAIL: Failing row contains (3, carol, 0.00, -50.00, EUR, 0, 2023-01-01 09:10:00+00).
#4 · ROLLBACK
#5 · SELECT 2
idint4ownertextbalancenumeric
1alice100.00
3carol0.00
(2 rows)

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:

sqldb=bank
UPDATE accounts SET balance = balance + 50.00 WHERE id = 1;
SELECT sum(balance) AS alice_and_bob FROM accounts WHERE id IN (1, 2);
output, from postgres 18.6
#1 · UPDATE 1
#2 · SELECT 1
alice_and_bobnumeric
250.00
(1 row)

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:

predict the output
T2 had taken 30.00 from bob but not committed when the server was killed. after the restart, what does bob have?
sql
-- 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 = ?
T1
snapshot 759:759:
T2
·
1
BEGIN; UPDATE accounts SET balance = balance - 50.00 WHERE id = 1; UPDATE accounts SET balance = balance + 50.00 WHERE id = 2→ UPDATE 1
committed in the database
idownerbalance
1alice100.00
2bob100.00

T1 moves 50.00 from alice to bob.

step 1/7
real output, a postgres 18.6 server · acid-crash
step through. the table is what a new transaction would see: T1's transfer appears at its COMMIT, T2's half never does, and the restart replays the WAL without changing the answer.
server log
  1. LOG: database system was interrupted; last known up at …
  2. LOG: database system was not properly shut down; automatic recovery in progress
  3. LOG: redo starts at 0/1622868
  4. LOG: redo done at 0/1624A88
  5. 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
  6. LOG: database system is ready to accept connections
the server's log at the restart: it notices it wasn't shut down cleanly, and replays the WAL from the last checkpoint's redo point.

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.