Skip to the content

Lesson 224 of 296

Isolation levels

What you will learn

  • What can go wrong when transactions run at the same time
  • Dirty reads, non-repeatable reads, phantoms, lost updates and write skew
  • The four isolation levels
  • How PostgreSQL implements them (MVCC)
  • Retrying serialisation failures
On this page

Why isolation levels?

Perfect isolation (as if transactions ran one at a time) is slower, because transactions must wait or be cancelled. Databases offer levels: weaker levels allow more concurrency but permit certain anomalies.

The anomalies

AnomalyWhat happens
Dirty readReading another transaction's uncommitted changes, which might be rolled back
Non-repeatable readReading the same row twice in one transaction and getting different values, because another transaction committed a change in between
Phantom readRunning the same query twice and getting different rows (new rows appeared)
Lost updateTwo transactions read the same value, both write a new value based on it, and one write overwrites the other
Write skewTwo transactions read overlapping data, each makes a change that is valid on its own, but together they break a rule

The four standard levels

LevelDirty readNon-repeatable readPhantom read
Read UncommittedPossible (PostgreSQL treats it as Read Committed)PossiblePossible
Read Committed (PostgreSQL's default)NoPossiblePossible
Repeatable ReadNoNoPossible by the standard; not in PostgreSQL
SerializableNoNoNo; also prevents write skew

MySQL's InnoDB defaults to Repeatable Read; PostgreSQL, Oracle and SQL Server default to Read Committed.

Seen on a real database

These were run with two connections to a real PostgreSQL server, using an accounts table where Aisha has 10000 pence.

Non-repeatable read under Read Committed (the default): transaction A reads Aisha's balance (10000); another session updates it to 7000 and commits; A reads again in the same transaction and gets 7000. Each statement sees the latest committed data.

Repeatable Read keeps a snapshot: at BEGIN ISOLATION LEVEL REPEATABLE READ, transaction A reads 10000; another session commits 7000; A reads again and still sees 10000, its snapshot from the first query. When A then tries to update that row, PostgreSQL refuses: could not serialize access due to concurrent update (error code 40001). A must retry.

Write skew, the on-call doctors example: the rule is "at least one doctor must be on call". Alice and Bob are both on call. Two transactions start at the same time; each counts on-call doctors (2), so each lets one doctor go off call: A for Alice, B for Bob. They update different rows, so no row conflict occurs.

LevelResult in the test
Repeatable ReadBoth commits succeed. 0 doctors on call. The rule is broken
SerializableA commits; B fails with could not serialize access due to read/write dependencies among transactions (40001). 1 doctor on call. Correct

MVCC: how PostgreSQL does it

PostgreSQL uses Multi-Version Concurrency Control: an UPDATE writes a new version of the row instead of overwriting it, and each transaction sees the versions that were committed when its snapshot was taken. So readers never block writers and writers never block readers. Old versions are cleaned up later by VACUUM (autovacuum runs automatically).

  • Read Committed takes a new snapshot for each statement.
  • Repeatable Read takes one snapshot for the whole transaction.
  • Serializable adds tracking of read and write dependencies (Serializable Snapshot Isolation) and cancels a transaction when it detects a pattern that could not happen in a one-at-a-time order.

Retrying

Under Repeatable Read and Serializable, the database may cancel your transaction with error code 40001 (serialisation failure); deadlocks give 40P01 (Lesson 225). This is normal: the application must retry the whole transaction, a few times, with a short backoff (Lesson 204). ORMs and libraries can do this for you.

Choosing a level

  • Read Committed (default) is right for most work, combined with atomic updates and the right locks (Lesson 225).
  • Repeatable Read for reports that must see one consistent snapshot across several queries.
  • Serializable for complex business rules across several rows (bookings, quotas, on-call rules), when you would rather retry than reason about every locking case.

How to say it in an interview

"Concurrent transactions can cause dirty reads, non-repeatable reads, phantoms, lost updates and write skew. The isolation levels are Read Uncommitted, Read Committed, which is PostgreSQL's default and sees the latest committed data per statement, Repeatable Read, which uses one snapshot for the whole transaction, and Serializable, which also prevents write skew. PostgreSQL implements this with MVCC, so readers and writers do not block each other. Higher levels can abort transactions with serialisation failures, error 40001, so the application must retry the whole transaction. I tested write skew with the on-call doctors example: Repeatable Read let both doctors go off call, Serializable aborted one."

Key words

WordMeaning
Isolation levelHow much concurrent transactions are shielded from each other
Dirty readReading uncommitted data
Non-repeatable readThe same row reads differently within a transaction
Phantom readA repeated query returns new rows
Write skewSeparate valid changes that together break a rule
MVCCKeeping several row versions so readers and writers do not block
SnapshotThe committed state a transaction sees
Serialisation failureThe database aborts a transaction to keep it correct (40001)

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.