Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Describe the four SQL isolation levels and the anomalies each allows. What are the default levels in PostgreSQL and MySQL InnoDB?

Try it yourself

Isolation levels: what a second read sees

Step through two transactions and see exactly when read committed and repeatable read give different answers.

Read this example as text

T1 at read committed

  1. T1 begins. Transaction T1 starts with isolation level READ COMMITTED. The row holds balance 100.
  2. T1 reads the balance. T1 reads 100. Under read committed each statement looks afresh at what is committed when the statement starts.
  3. T2 changes the row. A second transaction, T2, sets the balance to 150 but has not committed. The database keeps both versions: 100 is still the committed one.
  4. T1 reads again, before T2 commits. T1 reads 100 again. Neither level shows another transaction's uncommitted change, so there is no dirty read. T1 does not wait for T2 either.
  5. T2 commits. T2 commits. The committed balance is now 150.
  6. T1 reads a third time. T1 reads 150. The same query, inside one transaction, returned 100 and then 150. This is a non-repeatable read, and read committed allows it.
  7. T1 commits. T1 ends. A transaction that starts now reads 150 at either level.

T1 read 100, 100, then 150: a non-repeatable read.

T1 at repeatable read

  1. T1 begins. Transaction T1 starts with isolation level REPEATABLE READ. The row holds balance 100.
  2. T1 reads the balance. T1 reads 100. Under repeatable read this first read fixes T1's snapshot: T1 will keep seeing the data as it was committed at this moment.
  3. T2 changes the row. A second transaction, T2, sets the balance to 150 but has not committed. The database keeps both versions: 100 is still the committed one.
  4. T1 reads again, before T2 commits. T1 reads 100 again. Neither level shows another transaction's uncommitted change, so there is no dirty read. T1 does not wait for T2 either.
  5. T2 commits. T2 commits. The committed balance is now 150.
  6. T1 reads a third time. T1 still reads 100. Its snapshot was fixed at its first read, so T2's commit is invisible to it. The read is repeatable.
  7. T1 commits. T1 ends. A transaction that starts now reads 150 at either level.

T1 read 100 three times: its reads were repeatable.

What this example leaves out
  • Modelled on PostgreSQL and MySQL InnoDB, where a plain SELECT reads a snapshot and never waits for a writer.
  • SQL Server's default read committed uses locks instead: there, T1's second read would wait for T2 to finish.
  • Only reads are compared. What happens when T1 also writes the row (a lost update, or an error in PostgreSQL) is not modelled.

READ UNCOMMITTED allows dirty reads; READ COMMITTED prevents them but still allows non-repeatable reads and phantoms; REPEATABLE READ prevents non-repeatable reads, and although the standard still allows phantoms there, snapshot-based implementations such as PostgreSQL's and InnoDB's consistent reads prevent them in practice; SERIALIZABLE prevents all of these, including write skew. PostgreSQL, Oracle and SQL Server default to READ COMMITTED, while MySQL InnoDB defaults to REPEATABLE READ. In Spring we choose a level with @Transactional(isolation = ...), and Isolation.DEFAULT means the database's own setting.

Why this question is asked

Isolation is the I in ACID, and it is the property that decides what one transaction can see of another that runs at the same time. Picking a level is a trade-off between correctness and concurrency, and the same Spring code can behave differently on PostgreSQL and MySQL just because their defaults differ. Interviewers ask this as a foundation question, and a good answer names each anomaly with an example rather than reciting a table. In this answer, we will define the anomalies with small examples, map them to the four levels, look at how real databases implement the levels, and finish with how Spring sets them.

1A Quick Recap: What Isolation Protects Against

If transactions ran one after the other, each would see a database that nobody else was changing. Real databases run many at once for speed, and the isolation level says how much of that concurrency each transaction is allowed to notice. The SQL standard describes the levels by the anomalies, the odd effects, that each one permits. So the first step is to understand the anomalies.

2The Anomalies, One by One

A dirty read is reading data another transaction has written but not committed. If that transaction then rolls back, we acted on a value that never officially existed.

A non-repeatable read is reading the same row twice in one transaction and getting different values, because another transaction updated it and committed in between.

A phantom read is running the same query with a condition twice and getting a different set of rows, because another transaction inserted or deleted rows that match the condition.

Write skew is subtler: two transactions read the same data, each makes a decision based on it, and each writes a different row. Neither overwrote the other, yet together they break a rule, such as 'at least one doctor on call'. The standard's definitions do not name it, but true SERIALIZABLE must prevent it.

SQL
-- Non-repeatable read at READ COMMITTED
-- Session A -- Session B
BEGIN;
SELECT balance FROM account WHERE id = 1; -- 100
UPDATE account SET balance = 70 WHERE id = 1;
COMMIT;
SELECT balance FROM account WHERE id = 1; -- 70: same row, new value
COMMIT;

Notice that nothing went wrong in either session on its own. The anomaly is only visible because A looked twice. If A had computed something from the first value and written it back, it would have overwritten B's change, which is called a lost update.

3The Four Levels and What Each Allows

Each level up removes one more anomaly. The table shows the standard's view, with a note on how snapshot databases behave at REPEATABLE READ.

Isolation levels and anomalies
LevelDirty readNon-repeatable readPhantomWrite skew
READ UNCOMMITTEDAllowedAllowedAllowedAllowed
READ COMMITTEDPreventedAllowedAllowedAllowed
REPEATABLE READPreventedPreventedStandard allows; snapshots preventAllowed
SERIALIZABLEPreventedPreventedPreventedPrevented

The standard sets minimum guarantees, so databases are allowed to do better, and most do. PostgreSQL treats READ UNCOMMITTED as READ COMMITTED, so dirty reads never happen there. Both PostgreSQL and InnoDB implement REPEATABLE READ with snapshots, which also hide phantoms for plain reads. What a snapshot does not stop is write skew, which is why SERIALIZABLE still matters.

4How Real Databases Implement the Levels

PostgreSQL, Oracle and SQL Server use READ COMMITTED as their default, and in PostgreSQL each statement sees a fresh snapshot of committed data. MySQL's InnoDB uses REPEATABLE READ by default: plain SELECTs read from a snapshot taken at the first read in the transaction, while locking reads and updates use next-key locks, a combination of row and gap locks, to keep other transactions from inserting into ranges we read.

At SERIALIZABLE, PostgreSQL uses serializable snapshot isolation: transactions still run on snapshots, but the database tracks read and write dependencies between them and aborts one with SQLSTATE 40001 when they could not have happened one after the other. InnoDB's SERIALIZABLE turns plain SELECTs into locking reads, so readers and writers block each other more. In both cases, the application must be ready to retry or wait.

SQL
-- check or set the level
SHOW transaction_isolation; -- PostgreSQL: read committed
SELECT @@transaction_isolation; -- MySQL: REPEATABLE-READ
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; -- for the current transaction

5Setting the Level in Spring

In Spring we set the level per method with the isolation attribute of @Transactional. The default, Isolation.DEFAULT, means Spring does not touch the connection's level, so we get whatever the database or the pool is configured with.

Java
@Transactional(isolation = Isolation.SERIALIZABLE)
public void goOffCall(long shiftId, long doctorId) { ... }
// Spring calls connection.setTransactionIsolation(TRANSACTION_SERIALIZABLE) at the start
// and restores the previous level when the transaction ends
@Transactional // Isolation.DEFAULT
public void placeOrder(Order o) { ... } // READ COMMITTED on PostgreSQL, REPEATABLE READ on MySQL

There is a catch: the isolation level only applies when the method starts a new transaction. If it joins an existing one with REQUIRED, the outer transaction's level wins, and by default Spring does not complain about the mismatch. JpaTransactionManager also needs a JPA dialect that supports custom isolation levels, which Hibernate's does; check your setup if you see an error about it.

6Follow-up questions

Which level would you use by default?

The database's default, READ COMMITTED on PostgreSQL, together with explicit concurrency control where it matters: optimistic locking with a version column, unique constraints, or SELECT ... FOR UPDATE. I would raise individual transactions to SERIALIZABLE only for rules that span several rows.

Does a higher level make the application slower?

It can. Higher levels mean more locking or more aborted transactions under contention, which means waits and retries. The cost depends on the workload, so it should be measured, but it is rarely a reason to avoid SERIALIZABLE for the few transactions that need it.

What is a lost update and which level prevents it?

Two transactions read the same value, both compute a new one and both write it, so one change is lost. PostgreSQL's REPEATABLE READ detects it and fails the second writer; at READ COMMITTED, and in InnoDB's REPEATABLE READ, we need a version column, FOR UPDATE, or an atomic update such as SET stock = stock - 1.

7Conclusion

In this answer, we defined dirty reads, non-repeatable reads, phantoms and write skew, and saw that READ UNCOMMITTED allows them all, READ COMMITTED stops dirty reads, REPEATABLE READ stops non-repeatable reads and in snapshot implementations phantoms too, and SERIALIZABLE stops everything including write skew. PostgreSQL, Oracle and SQL Server default to READ COMMITTED and InnoDB to REPEATABLE READ, and Spring sets the level with the isolation attribute, where DEFAULT keeps the database's own setting.

What to say in the interviewAbout 80 seconds aloud

Open with

The levels are defined by which anomalies they allow. A dirty read is seeing another transaction's uncommitted data. A non-repeatable read is reading a row twice and getting different values because someone committed an update in between.

Then, if they want more

A phantom is re-running a query and getting new or missing rows. READ UNCOMMITTED allows all of those. READ COMMITTED prevents dirty reads but allows the other two. REPEATABLE READ prevents non-repeatable reads; the standard still allows phantoms, but snapshot-based implementations like PostgreSQL and InnoDB's consistent reads prevent them in practice.

SERIALIZABLE prevents all of them and also write skew, where two transactions read the same data and write different rows, breaking a rule together. For defaults, PostgreSQL, Oracle and SQL Server use READ COMMITTED, and MySQL InnoDB uses REPEATABLE READ, which matters because the same code can behave differently.

In Spring I set it with the isolation attribute on @Transactional, for example Isolation.SERIALIZABLE; DEFAULT means Spring leaves the database's level alone. One thing to remember is that the level only applies when the method starts the transaction, not when it joins one. And at SERIALIZABLE, or REPEATABLE READ on PostgreSQL, the application must retry on serialization failures.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Describe the four SQL isolation levels and the anomalies each allows. What are the...": https://scrivio-interview-library.netlify.app/databases/transactions/59ee3c3a1d/