Skip to content

Isolation levels

Perfect isolation, where every transaction behaves as if it ran alone, costs something: transactions wait for each other, or some of them are aborted and have to be retried. So databases offer a dial. Each isolation level is a promise about which anomalies cannot happen, and a lower level lets more of them through in exchange for less waiting.

The SQL standard names four levels, from the weakest to the strongest: read uncommitted, read committed, repeatable read and serializable. They are a good vocabulary, as long as you remember that they are only names. What a name guarantees depends on how the database keeps transactions apart, and the lab models two ways.

Mental model. An isolation level is a list of anomalies the database promises to stop. Anything not on the list can still happen to you, whatever the name sounds like.

The lab follows two mechanisms. With locks, readers and writers take locks on rows, following the definitions in Berenson et al. With MVCC, a reader reads a committed version of a row and never waits for a writer, and a transaction that writes a row someone else changed is aborted. The next lesson explains both. Here is what each does to the five anomalies of the last lesson, in the labs’ schedules.

With locks. An entry says whether the anomaly happens in its lab at that level.

Anomaly Read uncommitted Read committed Repeatable read Serializable
Dirty read Happens No No No
Non-repeatable read Happens Happens No No
Phantom read Happens Happens Happens No
Lost update Happens Happens No No
Write skew Happens Happens No No

With MVCC.

Anomaly Read uncommitted Read committed Repeatable read Serializable
Dirty read No No No No
Non-repeatable read Happens Happens No No
Phantom read Happens Happens No No
Lost update Happens Happens No No
Write skew Happens Happens Happens No

Read the two tables side by side. Four things are worth noticing:

  1. The tables are not the same. At repeatable read, locks let a phantom through and stop write skew. MVCC does the opposite: no phantom, but write skew. The level name hides that difference.
  2. With MVCC, read uncommitted has no dirty read: it behaves like read committed. PostgreSQL’s documentation says so.
  3. Serializable is the only level with no anomaly in either table. How it gets there differs, as the next two sections show.
  4. A “No” can hide a cost. With locks at repeatable read, the lost update and the write skew are stopped by a deadlock that aborts T2, which the next lesson shows.

A level that stops an anomaly does not always do it by making a transaction wait. Sometimes it aborts one of them. Run the lost update again with row versions at repeatable read.

Lost update

An account holds 100. T1 and T2 each read the balance, add their deposit in the application, and write the result back.

What to watch Both deposits applied would make 150. Look at what the balance ends up as, and why.

Isolation level
How the database keeps transactions apart

A snapshot is taken at the first statement and every read uses it. If a row you write was changed by a transaction that committed after your snapshot, you are aborted: the first updater wins.

All 6 steps
  1. T1SELECT balance → aT1 reads balance and gets 100.
  2. T2SELECT balance → bT2 reads balance and gets 100.
  3. T1UPDATE balance = a + 30T1 writes 130 to balance. It is not committed yet.
  4. T1COMMITT1 commits.
  5. T2UPDATE balance = b + 20AbortedT2 cannot write balance: another transaction changed it and committed after T2’s snapshot. T2 is aborted with a serialization failure and has to be retried.
  6. T2COMMITNot runT2 was aborted, so this statement does not run.

State after this step

Rows
RowCommittedNot committed
balance130none
Transactions
TransactionStatusValues read so far
T1 Deposits 30Committeda = 100
T2 Deposits 20Abortedb = 100

Outcome

No anomaly happened in this run.

  • T2 was aborted by the database (serialization failure, concurrent update). An application would retry it.
  • Committed values at the end: balance = 130.
The same schedule at every level, with locks and with MVCC
Isolation levelLocksMVCC (row versions)
Read uncommittedLost updateLost update
Read committedLost updateLost update
Repeatable readNo anomalyT2 aborted: deadlockWaiting: T1No anomalyT2 aborted: serialization failure, concurrent update (shown above)
SerializableNo anomalyT2 aborted: deadlockWaiting: T1No anomalyT2 aborted: serialization failure, concurrent update

Model notes

Assumptions

  • The levels have the names of the SQL standard, but real databases differ. In PostgreSQL, read uncommitted behaves as read committed, and repeatable read is snapshot isolation: no phantom reads, but write skew is possible. In MySQL’s InnoDB, read uncommitted can read uncommitted data, and locking reads lock index ranges to stop phantoms. This lab shows a textbook model, not any one product.
  • Locks follows the locking definitions in Berenson et al., A Critique of ANSI SQL Isolation Levels (1995): write locks are held until the end at every level; read locks are absent at read uncommitted, released at once at read committed, held on rows until the end at repeatable read (a search is locked only while it runs), and held on rows and searches until the end at serializable.
  • MVCC follows PostgreSQL’s documentation: a read never waits and sees a committed version of a row, from a snapshot taken at the first statement at repeatable read and serializable, and two writers of the same row wait for each other. At repeatable read and serializable, the writer that waited is aborted if the first one commits.
  • Serializable with MVCC aborts a transaction at commit when committing it would make the dependencies between the committed transactions form a cycle. PostgreSQL’s serializable level uses a related method, and its documentation warns that it can raise errors that would not occur in a true serial execution.
  • A deadlock is resolved by aborting the transaction whose request closed the cycle. Databases choose differently (PostgreSQL’s documentation says which one is aborted is difficult to predict), and the application has to retry the transaction that was aborted. With locks, two transactions that each hold a read lock on a row and then both want to write it deadlock, which is what happens at repeatable read and serializable in the lost-update and write-skew schedules.

Simplifications

  • Each lab runs one fixed interleaving. A level that avoids an anomaly in this schedule can still allow it in another, and a transaction that has to wait changes the timing of the statements behind it.
  • A statement is one atomic step. There are no timeouts, no indexes and no gap or next-key locks, a statement waits until every transaction it waits for has finished, and an aborted transaction is not retried. Durability, the log and crashes are not modelled.
  • A search matches rows by the start of their name and, if one is given, an exact value. A lock on a search, and a conflict with it, looks at both the old and the new value of a row that is written.

Parameter values are illustrative. They are not benchmarks of any real product.

T2 tried to write a balance that T1 changed and committed after T2’s snapshot, so the database aborts T2 with a serialization failure. The balance ends at 130, and the application has to run T2 again. PostgreSQL documents the same behaviour at repeatable read: the error is “could not serialize access due to concurrent update”, and applications using this level must be prepared to retry transactions.

Serializable goes further. Run write skew at serializable with row versions.

Write skew

Alice and Bob are both on call (1 means on call). The rule is that at least one of them must stay on call. Each transaction counts who is on call and goes off call only if two are.

What to watch Each transaction checks the rule and each changes a different row. How many are on call at the end?

Isolation level
How the database keeps transactions apart

Like repeatable read, and a transaction is also aborted at commit if committing it would give a result that no one-at-a-time order could produce.

All 6 steps
  1. T1SELECT COUNT(*) WHERE on-call → nT1 counts the rows of on-call and gets 2.
  2. T2SELECT COUNT(*) WHERE on-call → mT2 counts the rows of on-call and gets 2.
  3. T1IF n ≥ 2: UPDATE alice = 0T1 writes 0 to alice. It is not committed yet.
  4. T2IF m ≥ 2: UPDATE bob = 0T2 writes 0 to bob. It is not committed yet.
  5. T1COMMITT1 commits.
  6. T2COMMITAbortedT2 cannot commit: committing it would give a result that no one-at-a-time order of the transactions could produce. T2 is aborted with a serialization failure and has to be retried.

State after this step

Rows
RowCommittedNot committed
alice0none
bob1none
Transactions
TransactionStatusValues read so far
T1 Alice goes off callCommittedn = 2
T2 Bob goes off callAbortedm = 2

Outcome

No anomaly happened in this run.

  • T2 was aborted by the database (serialization failure, read/write dependencies). An application would retry it.
  • Committed values at the end: alice = 0, bob = 1.
The same schedule at every level, with locks and with MVCC
Isolation levelLocksMVCC (row versions)
Read uncommittedWrite skewWrite skew
Read committedWrite skewWrite skew
Repeatable readNo anomalyT2 aborted: deadlockWaiting: T1Write skew
SerializableNo anomalyT2 aborted: deadlockWaiting: T1No anomalyT2 aborted: serialization failure, read/write dependencies (shown above)

Model notes

Assumptions

  • The levels have the names of the SQL standard, but real databases differ. In PostgreSQL, read uncommitted behaves as read committed, and repeatable read is snapshot isolation: no phantom reads, but write skew is possible. In MySQL’s InnoDB, read uncommitted can read uncommitted data, and locking reads lock index ranges to stop phantoms. This lab shows a textbook model, not any one product.
  • Locks follows the locking definitions in Berenson et al., A Critique of ANSI SQL Isolation Levels (1995): write locks are held until the end at every level; read locks are absent at read uncommitted, released at once at read committed, held on rows until the end at repeatable read (a search is locked only while it runs), and held on rows and searches until the end at serializable.
  • MVCC follows PostgreSQL’s documentation: a read never waits and sees a committed version of a row, from a snapshot taken at the first statement at repeatable read and serializable, and two writers of the same row wait for each other. At repeatable read and serializable, the writer that waited is aborted if the first one commits.
  • Serializable with MVCC aborts a transaction at commit when committing it would make the dependencies between the committed transactions form a cycle. PostgreSQL’s serializable level uses a related method, and its documentation warns that it can raise errors that would not occur in a true serial execution.
  • A deadlock is resolved by aborting the transaction whose request closed the cycle. Databases choose differently (PostgreSQL’s documentation says which one is aborted is difficult to predict), and the application has to retry the transaction that was aborted. With locks, two transactions that each hold a read lock on a row and then both want to write it deadlock, which is what happens at repeatable read and serializable in the lost-update and write-skew schedules.

Simplifications

  • Each lab runs one fixed interleaving. A level that avoids an anomaly in this schedule can still allow it in another, and a transaction that has to wait changes the timing of the statements behind it.
  • A statement is one atomic step. There are no timeouts, no indexes and no gap or next-key locks, a statement waits until every transaction it waits for has finished, and an aborted transaction is not retried. Durability, the log and crashes are not modelled.
  • A search matches rows by the start of their name and, if one is given, an exact value. A lock on a search, and a conflict with it, looks at both the old and the new value of a row that is written.

Parameter values are illustrative. They are not benchmarks of any real product.

Both transactions write a different row, so nothing conflicts at the moment of the write. At commit, the database sees that T1 and T2 each read what the other changed, and that no one-at-a-time order could give this result. It aborts T2, and 1 doctor stays on call. Retrying T2 would now count one person and leave them on call.

One more with MVCC: the phantom at repeatable read.

Phantom read

A table holds two bookings. T1 counts them, does some other work, and counts them again. T2 inserts a third booking and commits between the two counts.

What to watch Does T1's second count include a row that was not there the first time?

Isolation level
How the database keeps transactions apart

A snapshot is taken at the first statement and every read uses it. If a row you write was changed by a transaction that committed after your snapshot, you are aborted: the first updater wins.

All 5 steps
  1. T1SELECT COUNT(*) WHERE bookings → aT1 counts the rows of bookings and gets 2.
  2. T2INSERT booking-3 = 1T2 inserts booking-3 with the value 1. It is not committed yet.
  3. T2COMMITT2 commits.
  4. T1SELECT COUNT(*) WHERE bookings → bT1 counts the rows of bookings and gets 2.
  5. T1COMMITT1 commits.

State after this step

Rows
RowCommittedNot committed
booking-11none
booking-21none
booking-31none
Transactions
TransactionStatusValues read so far
T1 Counts the bookings twiceCommitteda = 2, b = 2
T2 Adds a bookingCommittednone

Outcome

No anomaly happened in this run.

  • The result is the same as running T1, T2 one at a time, in that order.
  • Committed values at the end: booking-1 = 1, booking-2 = 1, booking-3 = 1.
The same schedule at every level, with locks and with MVCC
Isolation levelLocksMVCC (row versions)
Read uncommittedPhantom readPhantom read
Read committedPhantom readPhantom read
Repeatable readPhantom readNo anomaly (shown above)
SerializableNo anomalyWaiting: T2No anomaly

Model notes

Assumptions

  • The levels have the names of the SQL standard, but real databases differ. In PostgreSQL, read uncommitted behaves as read committed, and repeatable read is snapshot isolation: no phantom reads, but write skew is possible. In MySQL’s InnoDB, read uncommitted can read uncommitted data, and locking reads lock index ranges to stop phantoms. This lab shows a textbook model, not any one product.
  • Locks follows the locking definitions in Berenson et al., A Critique of ANSI SQL Isolation Levels (1995): write locks are held until the end at every level; read locks are absent at read uncommitted, released at once at read committed, held on rows until the end at repeatable read (a search is locked only while it runs), and held on rows and searches until the end at serializable.
  • MVCC follows PostgreSQL’s documentation: a read never waits and sees a committed version of a row, from a snapshot taken at the first statement at repeatable read and serializable, and two writers of the same row wait for each other. At repeatable read and serializable, the writer that waited is aborted if the first one commits.
  • Serializable with MVCC aborts a transaction at commit when committing it would make the dependencies between the committed transactions form a cycle. PostgreSQL’s serializable level uses a related method, and its documentation warns that it can raise errors that would not occur in a true serial execution.
  • A deadlock is resolved by aborting the transaction whose request closed the cycle. Databases choose differently (PostgreSQL’s documentation says which one is aborted is difficult to predict), and the application has to retry the transaction that was aborted. With locks, two transactions that each hold a read lock on a row and then both want to write it deadlock, which is what happens at repeatable read and serializable in the lost-update and write-skew schedules.

Simplifications

  • Each lab runs one fixed interleaving. A level that avoids an anomaly in this schedule can still allow it in another, and a transaction that has to wait changes the timing of the statements behind it.
  • A statement is one atomic step. There are no timeouts, no indexes and no gap or next-key locks, a statement waits until every transaction it waits for has finished, and an aborted transaction is not retried. Durability, the log and crashes are not modelled.
  • A search matches rows by the start of their name and, if one is given, an exact value. A lock on a search, and a conflict with it, looks at both the old and the new value of a row that is written.

Parameter values are illustrative. They are not benchmarks of any real product.

T1 counts 2 bookings both times, because it reads from the snapshot it took at its first statement. T2’s new booking exists, but T1 does not see it until it starts a new transaction.

Here is what the two databases’ own documentation says, and no more. The labs are textbook models, so the tables above are not a description of either product.

PostgreSQL MySQL (InnoDB)
Default level Read committed Repeatable read
Read uncommitted Behaves like read committed Reads do not lock, but may use an earlier version of a row; dirty reads are possible
Read committed Each query sees the data committed before that query began Each consistent read, even in the same transaction, uses its own fresh snapshot; phantoms are possible
Repeatable read Sees a snapshot from the start of the first statement of the transaction; the standard allows phantoms, PostgreSQL does not Consistent reads use the snapshot set by the first read; searches that lock use gap locks or next-key locks to block inserts into the range
Serializable Serializable Snapshot Isolation, with predicate locking; can abort a transaction, which must be retried Like repeatable read, but plain SELECT statements are converted to SELECT ... FOR SHARE if autocommit is disabled

Three consequences:

  • The same name, a different guarantee. Read uncommitted allows a dirty read in InnoDB and not in PostgreSQL. A level that is “repeatable read” in the standard sense can still show a phantom, and PostgreSQL’s table says its own repeatable read does not.
  • The defaults differ. An application moved from one product to the other without changing its level changes its guarantees.
  • What this lesson does not say. The two pages quoted here do not tell you which of the five anomalies occur at each level in each product, and this course has not tested a real database. In particular it does not claim whether write skew or lost update can happen at InnoDB’s repeatable read. Check your product’s documentation, and test the schedule you care about on your own database.

Each line below is a requirement. Put it under the weakest level in which a PostgreSQL-style database meets it.

Challenge

The weakest level that is enough

Take a PostgreSQL-style database: read committed is the default, repeatable read is a snapshot taken at the first statement, and serializable also detects conflicts that no one-at-a-time order allows. For each requirement, choose the weakest of the three levels that meets it.

Sorting needs JavaScript.

Categories

Read committed
Each query sees what was committed before it began.
Repeatable read
One snapshot for the whole transaction; a conflicting writer is aborted.
Serializable
Every result is one that running the transactions one at a time could give.
  1. Line 1 of 6A reader must never see a change that has not been committed.
  2. Line 2 of 6A transaction that reads the same row twice must get the same value both times.
  3. Line 3 of 6A search that is run twice in one transaction must return the same rows (in PostgreSQL).
  4. Line 4 of 6Two requests read a balance, add a deposit in the application and write it back. No deposit may be lost, and a retry is acceptable.
  5. Line 5 of 6Two doctors each check that the other is on call before going off call. At least one must always stay on call.
  6. Line 6 of 6Every result must be one that running the transactions one at a time, in some order, could have produced.

Check your answers at least once before looking at the answers.

What happens if the application ignores the error?

Section titled “What happens if the application ignores the error?”

An application that runs at repeatable read or serializable and treats a serialization failure as a plain error shows the customer a failure that was avoidable, or loses the work. The database did its job: it refused a result it could not make correct. The retry belongs to the application, and it must run the whole transaction again, from the first read, since the data it read before is out of date. PostgreSQL’s documentation says the same for both levels. The cost of stronger isolation is that some transactions are run twice.

  • An isolation level is a list of anomalies that the database promises to prevent. A weaker level lets more through and makes transactions wait or abort less.
  • With locks, repeatable read stops write skew but not phantoms. With row versions it is the other way round. Serializable stops all five in both.
  • A database can stop an anomaly by aborting a transaction. The application has to retry it.
  • PostgreSQL defaults to read committed and treats read uncommitted as read committed. InnoDB defaults to repeatable read and lets read uncommitted read uncommitted data.
  • The standard’s names do not settle what a product does. Its documentation does, and a test of your own schedule settles the rest.

Next: locks and row versions, the two ways a database keeps transactions apart.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.