ACID
A transfer is two statements, and a lot can happen between them. Cut the power half-way, break a rule, withdraw twice at the same moment, then pull the plug right after COMMIT, and see what each of the four letters saves you from.
What you'll take away
- Explain why a power cut mid-transfer loses money without
BEGIN … COMMIT - Use a
CHECKso one broken rule rolls the whole transaction back - Stop a lost update with
FOR UPDATE, one guardedUPDATE, or SERIALIZABLE - Know why a commit survives a pulled plug, and what turning fsync off risks
The 1-minute version
Tap a point and the picture below plays that step.
Try every event with "No transaction" first, then switch and press it again. Watch the vanished and made-up money below. One plate is 100. It behaves like PostgreSQL; the amounts are made up
A transfer is two UPDATEs with no transaction, and the power goes after the first. What do you find when it comes back?
The picture: the database is the big building, and on its roof stand Mai's and Beam's balances as stacks of gold plates, one plate for every 100. The sessions are the small buildings in front (T1, and T2 when two run at once), each with the cash its machine has paid out lying in front of it. The tower at the back is the WAL, the log on disk that lights up when a COMMIT is written. Dashed plates are a change nobody else can see yet (not committed); red plates are money lost or paid out of nowhere; the district goes dark when the power is cut. It follows PostgreSQL's behaviour; the amounts are made up.
Compare them all: ACID
The same story in every tile: Mai sends Beam 300 and the power goes between the two statements; then two machines withdraw 600 from Mai's account at the same moment. See which loses money, which pays out too much, and which gets through both.
Use it when One statement that stands on its own, like a single UPDATE … SET n = n + 1
Skip it when Work of several statements that belong together, like a transfer: fail half-way and half of it stays
A model of PostgreSQL's behaviour, made-up amounts. Vanished: lost in a half-done transfer. Made up: paid out beyond what the balance went down. Press any tile to see it large above, with when it fits.
The four letters
| Letter | Promise | Saves you from |
|---|---|---|
| Atomicity | All of the transaction or none of it | Half a transfer after a crash |
| Consistency | Every constraint holds after each commit | A negative balance, an order with no customer |
| Isolation | Transactions at once don't trample each other | Two withdrawals that both see the old balance |
| Durability | Committed means kept, power cut or not | An "OK" that disappears |
A transaction is not a lock
BEGIN … COMMIT makes the work all-or-nothing; it doesn't stop two transactions reading the same balance. At READ COMMITTED (PostgreSQL's default) and REPEATABLE READ (MySQL's), read-then-write still loses updates. Lock what you read (FOR UPDATE), let SERIALIZABLE turn one back and retry, or do the check and the write in one statement.
Keep transactions short
A transaction holds its locks until it ends. Don't call another service or wait for a person inside one; gather what you need first, then BEGIN, write, COMMIT.
In real code
-- PostgreSQL 16: a transfer that is all or nothing, a rule the database keeps, and a withdrawal that can't race.
CREATE TABLE accounts (id text PRIMARY KEY, balance numeric(12, 2) NOT NULL CHECK (balance >= 0));
INSERT INTO accounts VALUES ('mai', 1000), ('beam', 200);
-- 1. Atomicity: both UPDATEs, or neither.
BEGIN;
UPDATE accounts SET balance = balance - 300 WHERE id = 'mai';
UPDATE accounts SET balance = balance + 300 WHERE id = 'beam';
COMMIT;
SELECT 'after the transfer: ' || string_agg(id || ' ' || balance, ', ' ORDER BY id) FROM accounts;
-- 2. Something fails half-way (here we say so with ROLLBACK; a crash does the same on restart): nothing stays.
BEGIN;
UPDATE accounts SET balance = balance - 300 WHERE id = 'mai';
ROLLBACK;
SELECT 'after the rollback: ' || string_agg(id || ' ' || balance, ', ' ORDER BY id) FROM accounts;
-- 3. Consistency: CHECK (balance >= 0) refuses an overdraft, and Beam's credit just before it is undone too.
-- A DO block with an exception handler, so this file can show the error and carry on.
CREATE TEMP TABLE said (line text);
DO $$
BEGIN
UPDATE accounts SET balance = balance + 1500 WHERE id = 'beam';
UPDATE accounts SET balance = balance - 1500 WHERE id = 'mai';
EXCEPTION WHEN check_violation THEN
INSERT INTO said VALUES ('refused: ' || SQLERRM);
END $$;
SELECT line FROM said;
SELECT 'after the refusal: ' || string_agg(id || ' ' || balance, ', ' ORDER BY id) FROM accounts;
-- 4. Isolation: lock the row while you decide. A second session's SELECT … FOR UPDATE on 'mai' would wait here.
BEGIN;
SELECT 'locked, balance ' || balance FROM accounts WHERE id = 'mai' FOR UPDATE;
UPDATE accounts SET balance = balance - 600 WHERE id = 'mai' AND balance >= 600;
COMMIT;
SELECT 'after the withdrawal: ' || string_agg(id || ' ' || balance, ', ' ORDER BY id) FROM accounts;
psql -f transfer.pg.sql (checked against PostgreSQL 16 in Docker)It printed:after the transfer: beam 500.00, mai 700.00 after the rollback: beam 500.00, mai 700.00 refused: new row for relation "accounts" violates check constraint "accounts_balance_check" after the refusal: beam 500.00, mai 700.00 locked, balance 700.00 after the withdrawal: beam 500.00, mai 100.00
The same four in each engine: a transfer in one transaction, one that fails half-way and rolls back, an overdraft the CHECK refuses (with the credit before it undone too), and a withdrawal that locks the row while it decides. Really run: PostgreSQL 16 and MySQL 8.4 in Docker, SQLite with its CLI; below is what each printed. SQLite's CLI stops at the first error, so its overdraft is caught by a guarded UPDATE instead.
Common questions
What does ACID stand for?
Atomicity: all of the transaction or none of it. Consistency: every constraint still holds after each commit. Isolation: transactions running at once don't trample each other, as far as the level you pick allows. Durability: once committed, it stays, power cut or not.
What happens if the power goes mid-transfer with no transaction?
With no BEGIN, each statement commits the moment it ends. Mai's debit is already committed and Beam's credit never ran, so after the restart 300 has simply gone. The database had no way to know the two statements belong together.
What does the C in ACID really mean?
The rules you declared in the database, like CHECK, FOREIGN KEY and UNIQUE, hold at every commit. A statement that breaks one fails and the whole transaction rolls back, including what ran before it. Rules you never wrote down, the database can't keep for you.
I'm inside a transaction. How can it still pay out twice?
At READ COMMITTED both machines read 1,000 and decide it's enough. Each writes the balance it worked out from its read, 400, and pays 600: 1,200 paid out, the balance down by only 600. That's a lost update. Had T1 not committed yet, T2's UPDATE would just wait, then write 400 all the same.
FOR UPDATE, SERIALIZABLE or one UPDATE: which should I use?
If the check fits in a WHERE, one statement does it: UPDATE … SET balance = balance - 600 WHERE balance >= 600. If the app has to read and decide first, use FOR UPDATE. If the rule spans many rows and locking by hand gets messy, use SERIALIZABLE, and have the app retry on error 40001.
What's the default isolation level?
PostgreSQL starts at READ COMMITTED; MySQL (InnoDB) at REPEATABLE READ. At either one, reading a value, working it out in the app and writing it back can still lose an update, so lock the row or do it in one statement.
COMMIT said done and the power went right after. Is the data still there?
Yes. The database writes the WAL to disk before it says COMMIT is done, and replays it on restart. The exception is turning off fsync or synchronous_commit for speed: then the last few commits can be lost.
Why should transactions be short?
A transaction holds its locks until it ends. Call another service or wait for a person inside one and everyone who needs those rows waits too. Gather what you need first, then BEGIN, write and COMMIT.
Next lesson
CAP theoremLevel: Deep
One database makes the second withdrawal wait for the new balance; copies cut off by the network can't: sell the last seat twice, or refuse.