Transaction Isolation Levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable)
Choose isolation levels that balance correctness and throughput.
In plain words
It's a setting for how much one transaction is protected from others changing data at the same time. More protection means fewer surprises, but more waiting or retrying.
Think of it like…
Editing a shared document: Read Uncommitted shows other people's half-typed sentences; Read Committed shows only saved versions, but they can change between your glances; Repeatable Read freezes your view of the document when you start; Serializable makes it as if everyone took turns.
Where you’ve seen it
Two users book the last seat. Under Read Committed both read seats_left = 1, both decrement it, and the flight is overbooked. Fixes: SELECT … FOR UPDATE, an atomic UPDATE … SET seats_left = seats_left - 1 WHERE seats_left > 0, or Serializable with retries.
Words you’ll hear
- Dirty read
- Reading another transaction's uncommitted change.
- Non-repeatable read
- Reading the same row twice in one transaction and getting different values.
- Phantom read
- Re-running a range query and getting new rows.
- Serialization failure
- An error (SQLSTATE 40001) that the app must handle by retrying the transaction.
Visual diagram
Theory & example queries
The anomalies each level prevents
Read Uncommitted allows dirty reads. Read Committed prevents them, but each statement sees a fresh snapshot, so reading a row twice in one transaction can give different values (a non-repeatable read) and repeating a range query can return new rows (a phantom).
Repeatable Read gives the whole transaction one consistent snapshot, so values don't change underneath you; in PostgreSQL it also prevents phantoms. Serializable guarantees the outcome equals some one-at-a-time ordering of the transactions, which also rules out write skew.
BEGIN ISOLATION LEVEL REPEATABLE READ;SELECT …;UPDATE …;COMMIT;PostgreSQL is the lesson baseline unless noted otherwise.
Open a transaction so changes can be checked and committed or rolled back together.
Representative transaction sequence
Note: The ellipses stand for a concrete workload. At REPEATABLE READ, the exact rows and affected count depend on that workload; the sample shows one successful UPDATE.
Key takeaway: A higher level means fewer anomalies and more waiting or retries.
What databases actually do
Defaults differ: PostgreSQL, SQL Server and Oracle use Read Committed; MySQL InnoDB uses Repeatable Read. PostgreSQL implements Repeatable Read and Serializable with snapshots (MVCC), so instead of blocking it aborts a conflicting transaction with a serialization error, which your code must retry.
A higher isolation level isn't the only tool. For a single hot row, SELECT … FOR UPDATE or an atomic conditional UPDATE usually fixes the race at Read Committed with less overhead than switching the whole app to Serializable.
UPDATE flightsSET seats_left = seats_left - 1WHERE id = 42 AND seats_left > 0;PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
Representative atomic decrement
Note: Assumes flight 42 has a seat available. A concurrent or already sold-out attempt reports UPDATE 0, which is the sold-out signal.
then check the affected row count: 0 means sold out.
Key takeaway: Know your database's default, and treat Serializable as "be ready to retry".
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Name the anomaly you're worried about
Is it a dirty read, a value changing mid-transaction, new rows appearing, or two transactions overwriting each other (a lost update or write skew)?
two users, same row, same moment
When to use it & trade-offs
What it is
Isolation levels control what a transaction can see of other transactions running at the same time. The SQL standard defines four: Read Uncommitted, Read Committed, Repeatable Read and Serializable. Each one prevents more anomalies (dirty reads, non-repeatable reads, phantoms) at the cost of more blocking or retries.
Why it matters: Many concurrency bugs (double-booked seats, lost balance updates, reports that don't add up) come from running at a weaker isolation level than the code assumes. Interviewers ask about it to see whether you can reason about two users hitting the same row at the same moment.
When to reach for it
- Booking, inventory and balance updates where two users can collide
- Reports that read several tables and must be internally consistent
- Explaining why a "check, then insert" pattern is racy
When to avoid or be careful
- Switching the whole app to Serializable without retry logic
- Read Uncommitted or NOLOCK for anything financial
- Assuming the same level behaves identically in MySQL and PostgreSQL
The four isolation levels
Strengths: Never waits for writers.
Watch out: Dirty reads: you can see data that is later rolled back.
Strengths: No dirty reads, little blocking.
Watch out: Non-repeatable reads, phantoms and lost updates are still possible.
Strengths: Values don't change mid-transaction (and no phantoms in PostgreSQL).
Watch out: Write skew is still possible; in PostgreSQL, conflicting updates fail and need a retry.
Strengths: Same outcome as running transactions one at a time.
Watch out: The most overhead, and the app must retry serialization failures.
Interview signals & numbers to know
When this topic comes up
- “Two users book the last seat”
- “What is a phantom read?”
- Lost update
- A race condition inside a transaction
- “What's the default isolation level?”
Benchmarks & rules of thumb
- 4
- standard isolation levels
- Read Committed
- default in PostgreSQL, SQL Server, Oracle
- Repeatable Read
- default in MySQL InnoDB
- 40001
- serialization failure SQLSTATE: retry
Common mistakes & follow-ups
Common interview mistakes
- Assuming a transaction alone prevents races. At Read Committed, "read, then write" is still racy.
- Raising the isolation level without retrying serialization failures.
- Mixing up non-repeatable reads (changed values) with phantom reads (new rows).
Likely interviewer follow-ups
- Two transactions both read a balance of 100 and each subtract 30. What happens at Read Committed, and how do you prevent it?
- What is write skew, and which isolation level prevents it?
- Why does PostgreSQL return serialization errors instead of making transactions wait?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Transaction Isolation Levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) with hands-on SQL practice and interview Q&A.