Stop 12 · Transactions & ConcurrencyTopics · 4/9

Concurrency Problems (Dirty, Non-Repeatable and Phantom Reads)

Part of the SQL Roadmap

In short

Your query works perfectly when you test it alone. Then a hundred users run things at the same time and numbers start going wrong. These four problems are why.

When transactions run at the same time, a dirty read sees uncommitted data, a non-repeatable read sees a row change between two reads, and a phantom read sees new rows appear. Lost updates happen when two writers overwrite each other.

Two jaadus stand on either side of a fridge door, both scribbling on the same paper list held by a magnet, one rubbing out marks the other has just made.

Think of it like this

The flatmates' fridge list

Four flatmates in Pune share one shopping list stuck on the fridge. You read a note that Kabir is still scribbling and later erases (dirty read). You check 'milk: 1', look again and it says 2 (non-repeatable read). You count five items, recount and find seven (phantom). You and Ishaan both change 'eggs: 6' to 'eggs: 12', and one change silently replaces the other (lost update).

Watch it happen

A non-repeatable read and a phantom

Txn A (report)
Database
Txn B (app)

SELECT stock WHERE id = 7

1/5Ananya's report reads product 7. Stock is 10.

The big ideas

What you need to know

Dirty read

Reading another transaction's change before it commits. If it rolls back, you used data that never existed.

Non-repeatable read

Reading the same row twice in one transaction and getting different values, because someone committed in between.

Phantom read

Running the same filter twice and getting new or missing rows, because someone inserted or deleted in between.

Lost update

Two transactions read the same value, both write a new value based on it, and one change is lost.

See it in code

Walk through the code

sql
1-- Session A (Ananya)2BEGIN;3SELECT stock FROM products WHERE product_id = 7;   -- sees 104 5-- Session B (Rohit), at the same moment6BEGIN;7SELECT stock FROM products WHERE product_id = 7;   -- also sees 108UPDATE products SET stock = 9 WHERE product_id = 7;9COMMIT;10 11-- Session A continues with its old value12UPDATE products SET stock = 9 WHERE product_id = 7;13COMMIT;   -- two units sold, stock says 9: a lost update14 15-- Safer: let the database do the maths on the current value16UPDATE products SET stock = stock - 1 WHERE product_id = 7;

1/4Ananya's session reads stock 10 and plans to sell one.

Open two psql or pgAdmin windows to try this yourself. PostgreSQL never allows dirty reads, even if you ask for READ UNCOMMITTED. MySQL does allow them at that level.

Save yourself the pain

Common mistakes

Oops

Reading a value in the app, calculating, then writing the result back.

Do this instead

Do the change in one statement, like SET stock = stock - 1, or lock the row first with SELECT ... FOR UPDATE.

Oops

Assuming two SELECTs in one transaction always see the same data.

Do this instead

At READ COMMITTED, each statement sees the latest commits. Use REPEATABLE READ if the report needs one steady snapshot.

Your mission

Cause a lost update

~20 min

Quick check

A transaction runs SELECT COUNT(*) FROM orders WHERE city = 'Kochi' twice and gets 40, then 41. What is this called?

Go deeper

  1. 1Open two client sessions on the same table and reproduce a non-repeatable read.
  2. 2Write a one-line example of each anomaly in your own words.
  3. 3Note which anomalies each isolation level allows.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge