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):
| Level | Prevents | Notes |
|---|---|---|
| Read Committed | Dirty reads | Default in PostgreSQL, Oracle, SQL Server. Each statement sees a fresh snapshot. |
| Repeatable Read | + non-repeatable reads | In 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 skew | PostgreSQL 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
- What is the N+1 query problem, and how do you find and fix it?Mid-level
- What does Spring Boot's auto-configuration do, and how do you change what it set up?Mid-level
- How does a Spring backend authenticate requests from a mobile app, and how do you make sure users only see their own data?Mid-level
- Your Spring Boot service in Kubernetes keeps restarting with exit code 137. How do you investigate and fix it?Senior