Backend & Node.js · APIs, data and failure
What are database isolation levels and the anomalies they prevent?
Short answer
Isolation levels trade concurrency for consistency. They control dirty reads, non-repeatable reads, phantom reads and write skew, with serializable giving the strongest guarantee and the most aborts.
The SQL standard defines read uncommitted, read committed, repeatable read and serializable. Real databases implement them differently. Postgres repeatable read uses a snapshot and prevents non-repeatable reads and phantoms, but it can still allow write skew unless you use serializable. MySQL InnoDB defaults to repeatable read with gap locking.
Anomalies to know
- Dirty read: reading another transaction's uncommitted data
- Non-repeatable read: the same row returns different values within one transaction
- Phantom read: a re-run query returns new rows that match the predicate
- Lost update: two transactions overwrite each other's change
- Write skew: two transactions read a shared condition and each writes a different row, violating an invariant
BEGIN ISOLATION LEVEL SERIALIZABLE;
SELECT count(*) FROM on_call
WHERE shift_id = 12 AND active = true;
UPDATE on_call SET active = false
WHERE shift_id = 12 AND employee_id = 7;
COMMIT;The right level depends on the invariant. Read committed is a common default for simple web workloads. If a business rule spans multiple rows, such as keeping at least one person on call, a stronger level or an explicit lock is needed. Serializable is safest but requires retry logic and can reduce throughput.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What is write skew?
- 2How does Postgres repeatable read differ from serializable?
- 3Why do serializable transactions need retries?
- 4When is read committed sufficient?
Say it in the interview
Isolation levels trade concurrency against the anomalies a transaction can see. Read committed avoids dirty reads but allows non-repeatable reads and phantoms. Repeatable read uses a snapshot and blocks some of those, while serializable is the strongest but can abort transactions, so the application must retry. I pick the level from the invariant I need to protect, and I check the actual behavior of the database rather than relying on the standard names.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Backend & Node.js mock interview and get scored feedback on depth, clarity and confidence.