Knowledge Guide
HomeDatabasesTransactions & Concurrency Control

Isolation Levels & Anomalies

Why isolation levels exist

Run transactions concurrently and they can corrupt each other's view of the data. SQL defines four isolation levels — each a trade between correctness and concurrency. Knowing your engine's default (it is usually not the safest) is a real production skill.

The four anomalies

T1 reads balance 100, T2 updates it to 20 and commits, T1 reads again and gets 20 — a non-repeatable read
T1 reads balance 100, T2 updates it to 20 and commits, T1 reads again and gets 20 — a non-repeatable read

Level → which anomalies it prevents

Isolation levelDirty readNon-repeatablePhantom
Read Uncommittedpossiblepossiblepossible
Read Committed (Postgres default)preventedpossiblepossible
Repeatable Read (MySQL/InnoDB default)preventedpreventedpossible*
Serializablepreventedpreventedprevented

*InnoDB's next-key locks actually stop most phantoms at Repeatable Read; Postgres needs Serializable. Higher isolation = fewer anomalies but more blocking/aborts. Most apps run at Read Committed and handle the rest explicitly.

Fixing a lost update (the common one)

-- pessimistic: lock the row for the duration
BEGIN;
SELECT balance FROM accounts WHERE id='A' FOR UPDATE;  -- others block here
UPDATE accounts SET balance = balance - 100 WHERE id='A';
COMMIT;

-- optimistic: detect the conflict with a version column
UPDATE accounts SET balance=?, version=version+1 WHERE id='A' AND version=?;
-- if 0 rows updated, someone else changed it -> re-read and retry  (this is CAS, in SQL)

Pitfalls

Takeaways


Re-authored for this guide; non-repeatable-read timeline hand-authored as SVG. Follows DDIA ch. 7 and PostgreSQL transaction-isolation docs. See also: Transactions & ACID, MVCC & Locking, and (Concurrency) Race Conditions.

🤖 Don't fully get this? Learn it with Claude

Stuck on Isolation Levels & Anomalies? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Isolation Levels & Anomalies** (Databases) and want to truly understand it. Explain Isolation Levels & Anomalies from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Isolation Levels & Anomalies** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Isolation Levels & Anomalies** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Isolation Levels & Anomalies** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes