Skip to content
Commitwise
Menu

Explain transaction isolation levels and the anomalies they prevent.

BackendSenior

In shortIsolation levels trade consistency for concurrency: Read Committed prevents dirty reads, Repeatable Read also prevents non-repeatable reads, and Serializable prevents all anomalies including phantoms and write skew. Most databases default to Read Committed, so application code must handle the rest.

Model answer

Anomalies:

  • Dirty read: reading another transaction’s uncommitted change.
  • Non-repeatable read: reading the same row twice and getting different values.
  • Phantom: re-running a query and getting new rows.
  • Lost update: two read-modify-write cycles where one overwrites the other.
  • Write skew: two transactions read overlapping data, each writes something different, and together they break an invariant (two doctors both go off call because each saw the other on call).

Levels (SQL standard, with PostgreSQL notes):

LevelPreventsNotes
Read CommittedDirty readsDefault in PostgreSQL, Oracle, SQL Server. Each statement sees a fresh snapshot.
Repeatable Read+ non-repeatable readsIn PostgreSQL a snapshot per transaction (also no phantoms); concurrent updates to the same row fail with a serialization error. Default in MySQL InnoDB.
Serializable+ phantoms, write skewPostgreSQL uses SSI: transactions may fail and must be retried.

In practice: keep Read Committed as the default and protect specific invariants explicitly: optimistic locking with version columns, SELECT … FOR UPDATE on the rows the decision depends on, atomic conditional updates, unique and check constraints, or Serializable for the few transactions that need it, with a retry loop for serialization failures.

What they're looking for

The anomalies (dirty read, non-repeatable read, phantom, lost update, write skew), what each level prevents in practice (including PostgreSQL's MVCC behavior), the defaults, and how to protect invariants without Serializable everywhere.

Common mistakes

  • Assuming the default level prevents lost updates or write skew.
  • Switching everything to Serializable without handling serialization failures.

More backend questions

Esc

Type to search the whole site.