Skip to content

Five anomalies

Each transaction is correct on its own. Run one after the other, they give the right answer. Run at the same time, their statements interleave, and sometimes the result is one that no one-at-a-time order could give. That surprising result is an anomaly.

The five below are the classic ones. Each lab is a fixed schedule of statements: you step through it and watch what each transaction sees. The lab opens on a level and a mechanism where the anomaly happens. The next lesson changes them.

Mental model. Every anomaly is a transaction acting on a picture of the data that another transaction has already made false. What differs is what the picture was of: a value that was never committed, a value that has changed, a set of rows that has grown, a balance that has moved, or a rule about several rows.

A dirty read is a read of a change that was not committed. An account holds 100. T1 starts to withdraw 50 and changes the balance to 50, then changes its mind and rolls back. In between, T2 reads the balance.

Dirty read

An account holds 100. T1 starts to withdraw 50, then changes its mind and rolls back. T2 reads the balance while T1 is in the middle.

What to watch Look at what T2 reads, and whether T1 has committed that value.

Isolation level
How the database keeps transactions apart

Reads take no locks, so they see whatever is in a row, committed or not. Writes still lock the row until the end.

All 4 steps
  1. T1UPDATE balance = 50T1 writes 50 to balance. It is not committed yet.
  2. T2SELECT balance → aAnomalyT2 reads balance and gets 50.Dirty read: T2 read balance = 50, which T1 had written and not committed. T1’s write was then discarded, so that value never existed.
  3. T1ROLLBACKT1 rolls back, and its writes are discarded.
  4. T2COMMITT2 commits.

State after this step

Rows
RowCommittedNot committedLocks held
balance100nonenone
Transactions
TransactionStatusValues read so far
T1 Withdraws 50, then rolls backRolled backnone
T2 Reads the balanceCommitteda = 50

Outcome

Anomaly: dirty read.

  • Dirty read: T2 read balance = 50, which T1 had written and not committed. T1’s write was then discarded, so that value never existed.
  • Committed values at the end: balance = 100.
The same schedule at every level, with locks and with MVCC
Isolation levelLocksMVCC (row versions)
Read uncommittedDirty read (shown above)No anomaly
Read committedNo anomalyWaiting: T2No anomaly
Repeatable readNo anomalyWaiting: T2No anomaly
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.

T2 reads 50, T1 rolls back, and the balance ends at 100. T2 acted on a value that never existed.

In a non-repeatable read, a transaction reads the same row twice and gets two answers, because another transaction committed a change in between.

Non-repeatable read

An account holds 100. T1 reads the balance, does some other work, and reads it again. T2 withdraws 50 and commits between the two reads.

What to watch Do T1's two reads return the same value?

Isolation level
How the database keeps transactions apart

Each statement reads the newest committed version of the rows. Readers never wait for writers.

All 5 steps
  1. T1SELECT balance → aT1 reads balance and gets 100.
  2. T2UPDATE balance = balance − 50T2 writes 50 to balance. It is not committed yet.
  3. T2COMMITT2 commits.
  4. T1SELECT balance → bAnomalyT1 reads balance and gets 50.Non-repeatable read: T1 read balance twice and got 100, then 50, because T2 committed a change in between.
  5. T1COMMITT1 commits.

State after this step

Rows
RowCommittedNot committed
balance50none
Transactions
TransactionStatusValues read so far
T1 Reads the balance twiceCommitteda = 100, b = 50
T2 Withdraws 50Committednone

Outcome

Anomaly: non-repeatable read.

  • Non-repeatable read: T1 read balance twice and got 100, then 50, because T2 committed a change in between.
  • No order of running the transactions one at a time gives this result.
  • Committed values at the end: balance = 50.
The same schedule at every level, with locks and with MVCC
Isolation levelLocksMVCC (row versions)
Read uncommittedNon-repeatable readNon-repeatable read
Read committedNon-repeatable readNon-repeatable read (shown above)
Repeatable readNo anomalyWaiting: T2No anomaly
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 reads 100, and after T2 commits, T1 reads 50. Nothing was uncommitted here: both values are real. The harm is that T1 was using the first one to decide something, and the second one contradicts it.

A phantom read is the same thing for a search. A transaction counts the rows that match a condition, another transaction adds a row that matches and commits, and the second count is higher.

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

Rows that were read stay locked until the end, so they cannot change under the transaction. A search is locked only while it runs, so new rows can still appear.

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 → bAnomalyT1 counts the rows of bookings and gets 3.Phantom read: T1 counted the rows of bookings twice and got 2, then 3, because T2 committed a change to booking-3 in between.
  5. T1COMMITT1 commits.

State after this step

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

Outcome

Anomaly: phantom read.

  • Phantom read: T1 counted the rows of bookings twice and got 2, then 3, because T2 committed a change to booking-3 in between.
  • No order of running the transactions one at a time gives this result.
  • 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 read (shown above)No anomaly
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, then 3. No row T1 read has changed: a new row has appeared.

In a lost update, two transactions read a value, each computes a new one in the application, and each writes it back. The second write overwrites the first, using a number that was already out of date.

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

Each statement reads the newest committed version of the rows. Readers never wait for writers.

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 + 20T2 writes 120 to balance. It is not committed yet.
  6. T2COMMITAnomalyT2 commits.Lost update: T2 wrote balance from a value it had read before T1 changed it, so T1’s update is gone.

State after this step

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

Outcome

Anomaly: lost update.

  • Lost update: T2 wrote balance from a value it had read before T1 changed it, so T1’s update is gone.
  • No order of running the transactions one at a time gives this result.
  • Committed values at the end: balance = 120.
The same schedule at every level, with locks and with MVCC
Isolation levelLocksMVCC (row versions)
Read uncommittedLost updateLost update
Read committedLost updateLost update (shown above)
Repeatable readNo anomalyT2 aborted: deadlockWaiting: T1No anomalyT2 aborted: serialization failure, concurrent update
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.

The account holds 100, T1 deposits 30 and T2 deposits 20, so the balance should be 150. It ends at 120: T2 wrote 100 + 20 and erased T1’s 30.

In write skew, two transactions each read the same rows, each checks a rule against what it read, and each then writes a different row. Neither overwrites the other, and yet together they break the rule.

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

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 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. T2COMMITAnomalyT2 commits.Write skew: T2 and T1 each read rows that the other then changed, they wrote different rows, and both committed. No one-at-a-time order gives this result.

State after this step

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

Outcome

Anomaly: write skew.

  • Write skew: T2 and T1 each read rows that the other then changed, they wrote different rows, and both committed. No one-at-a-time order gives this result.
  • No order of running the transactions one at a time gives this result.
  • Committed values at the end: alice = 0, bob = 0.
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 (shown above)
SerializableNo anomalyT2 aborted: deadlockWaiting: T1No anomalyT2 aborted: serialization failure, read/write dependencies

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.

Alice and Bob are both on call, and the rule is that at least one of them must stay. Each transaction counts who is on call, sees two, and takes one person off. Both commit, and 0 doctors are on call.

Each transaction was right about what it saw. The rule belongs to the pair of rows, and each one only changed one of them. This is the case the previous lesson meant when it said that the database cannot enforce a rule it does not know.

Each line is a schedule in words, and there are four anomalies to choose from. Put each line under the anomaly it shows. Dirty reads are left out of this exercise, and the quiz at the end of the module covers them.

Challenge

Which anomaly is it?

Each line describes what two concurrent transactions did. Ask what changed under the first one: the value of a row it had read, the set of rows a search returns, a value that both overwrote, or a rule that two of them broke together. Put each line under the anomaly it shows.

Sorting needs JavaScript.

Categories

Non-repeatable read
A row that was read changes before the second read.
Phantom read
A search returns a different set of rows the second time.
Lost update
Two transactions write the same row from a value one of them had already made out of date.
Write skew
Each transaction checks a rule, writes a different row, and together they break the rule.
  1. Line 1 of 8T1 reads an account balance and gets 100. T2 withdraws 50 and commits. T1 reads the same balance again and gets 50.
  2. Line 2 of 8A report reads a customer's address when it starts and again when it finishes. In between, a support agent corrected it and committed.
  3. Line 3 of 8T1 counts the open support tickets and gets 12. T2 files a new ticket and commits. T1 counts again and gets 13.
  4. Line 4 of 8A check lists the rooms booked for one night and finds three. Another request books a fourth room and commits. The check runs the same search and finds four.
  5. Line 5 of 8Stock is 10. Two orders, of 3 and of 2, each read 10, subtract in the application and write back. The stock ends at 8, not at 5.
  6. Line 6 of 8Two requests read a page counter of 41, each adds one and writes 42. Two visits were counted as one.
  7. Line 7 of 8Two engineers are on call, and the rule is that one must stay. Each checks that the other is on call, and each goes off call. Both commit, and nobody is on call.
  8. Line 8 of 8A person has two accounts of 60 each, and the rule is that their total must not go below 0. Two withdrawals of 100, one from each account, each check that the total is at least 100, and both commit. The total is now negative.

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

What happens if the application does not care?

Section titled “What happens if the application does not care?”

Take the lost update. Two deposits of 30 and 20 into a balance of 100 end at 120 instead of 150, and nothing reports an error. Both transactions committed, both were told they succeeded, and the 30 is gone. That is the worst property of these anomalies: they do not crash, they do not log an exception, and they appear only when two requests happen to overlap. A system can pass every test on a quiet day and lose money under load.

A rule of thumb that follows: the more often several requests touch the same rows at once, the more likely the schedule above becomes. A balance that changes ten times a day will hide the bug for months. A counter that changes ten times a second will show it within minutes. This is reasoning, not a measurement: the labs run one fixed schedule and say nothing about how often it happens.

  • An anomaly is a result that no one-at-a-time order of the transactions could give. It happens only when the statements interleave.
  • A dirty read sees a change that is not committed. A non-repeatable read sees a row change between two reads. A phantom read sees a new row appear in a search.
  • A lost update overwrites a change using a value that was read before the change. Write skew breaks a rule about several rows even though each transaction writes a different one.
  • In the labs the lost update ends at 120 where 150 was expected, and the write skew leaves 0 doctors on call.
  • None of these produces an error. The result is wrong and the database reports success.

Next: the isolation levels, and which of these anomalies each level stops.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.