Dirty Read, Non-Repeatable Read and Phantom Read
Dirty reads, non-repeatable reads, phantom reads and lost updates, replayed step by step with two sessions on one table.
In this chapter
Dirty, non-repeatable and phantom reads, replayed step by step
Read anomalies: common questions
What is the difference between a non-repeatable read and a phantom read?
A non-repeatable read sees a changed value in a row it read before. A phantom read sees new rows appear in a range it queried before.
More on Transactions: isolation
Keep learning
Overview
chapters · transactions, isolation and locks Dirty, non-repeatable and phantom reads, replayed step by step A report's total disagrees with its own lines: no crash, just two transactions overlapping. Dirty read Saw uncommitted data. Same row, new value. Same condition, new rows. Lost update / skew Writes clash.
1. The running example: one sales table
Every anomaly uses this table. Correct total: $130. qty × price product 1 product 2 T1: BEGIN SELECT lines T2 changes table in between SELECT SUM should be 130 What T1 may see of T2's change decides the anomaly.
2. Dirty read: seeing work that is not committed
T1 report product 1 qty SELECT lines not committed 10 (15 pending) SELECT SUM reads pending row $25 never existed T1 printed Lab: A writes a balance, later rolls back. When does B see the 50? Can you get a dirty read? (only at read uncommitted) SQL Server read uncommitted Almost never wanted “No lock” report hints print totals that never existed.
3. Non-repeatable read: the same row, a new value
T1 report product 1 qty SELECT lines SELECT SUM disagrees with lines lines add up to total says Lab: switch between read committed and repeatable read. Nothing dirty Read committed: each statement sees latest commit. Not the same query twice SELECT rows, then SUM them, is enough.
4. Phantom read: the same condition, a new set of rows
T1 report SELECT lines products 1, 2 2 rows new row 3 rows SELECT SUM extra row counted from first query total says Lab: at repeatable read, compare the standard's minimum with a snapshot engine. Row locks on what you read Stops non-repeatable. The new row didn’t exist to lock. Cover the condition Range lock, or read a whole-database snapshot.
5. Changed value versus changed set
A row you read has a new value Non-repeatable read Rows matching your condition differ Phantom read (one change can cause both)
6. Anomalies about writes: lost update and write skew
T1 (+10 sold) T2 (+5 sold) product 1 qty SELECT qty → 10 expects $180 stale 10 + 5 qty if both kept qty stored total (T1 expected $180) Slip past read protection Neither is dirty, non-repeatable or phantom. Snapshot ≠ serializable Stops all 3 read anomalies, still allows write skew. Lost update at repeatable read (read-modify-write in app code) PostgreSQL (snapshot isolation) MySQL / InnoDB Second update of same row serialization error → retry UPDATE acts on latest committed row Lost update prevented? relative update locking read SELECT … FOR UPDATE version column Fixes: snapshot reads · lost update interview · levels in the next chapter
7. All four on one table
The sales report (correct total $130) under each anomaly What T2 does T1 total Why wrong Dirty read qty → 15, rolls back counted $25 that never existed qty → 15, commits total disagrees with lines inserts product 3 (10 × $1) a row appeared Lost update writes 15 over T1’s 20 read uncommitted read committed repeatable read
Remember
Saw uncommitted data. Committed change, re-read value differs. Committed change, condition returns new set. Phantoms beat row locks New row didn’t exist to lock. Write anomalies → next: the four levels and how they are built
More courses
- Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
- Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
- React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
- System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
- NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
- Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
- Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
- AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
- Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
- TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
- CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
- Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
- Docker — What an image is, why layers cache, and how containers find each other on a network.
- Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
- Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
- AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
- Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.