Five anomalies
What an anomaly is
Section titled “What an anomaly is”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.
Anomalies that start with a read
Section titled “Anomalies that start with a read”Dirty read
Section titled “Dirty read”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.
Reads take no locks, so they see whatever is in a row, committed or not. Writes still lock the row until the end.
- T1
UPDATE balance = 50T1 writes 50 to balance. It is not committed yet. - T2
SELECT 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. - T1
ROLLBACKT1 rolls back, and its writes are discarded. - T2
COMMITT2 commits.
State after this step
| Row | Committed | Not committed | Locks held |
|---|---|---|---|
balance | 100 | none | none |
| Transaction | Status | Values read so far |
|---|---|---|
| T1 Withdraws 50, then rolls back | Rolled back | none |
| T2 Reads the balance | Committed | a = 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.
| Isolation level | Locks | MVCC (row versions) |
|---|---|---|
| Read uncommitted | Dirty read (shown above) | No anomaly |
| Read committed | No anomalyWaiting: T2 | No anomaly |
| Repeatable read | No anomalyWaiting: T2 | No anomaly |
| Serializable | No anomalyWaiting: T2 | No 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.
Non-repeatable read
Section titled “Non-repeatable read”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?
Each statement reads the newest committed version of the rows. Readers never wait for writers.
- T1
SELECT balance → aT1 reads balance and gets 100. - T2
UPDATE balance = balance − 50T2 writes 50 to balance. It is not committed yet. - T2
COMMITT2 commits. - T1
SELECT 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. - T1
COMMITT1 commits.
State after this step
| Row | Committed | Not committed |
|---|---|---|
balance | 50 | none |
| Transaction | Status | Values read so far |
|---|---|---|
| T1 Reads the balance twice | Committed | a = 100, b = 50 |
| T2 Withdraws 50 | Committed | none |
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.
| Isolation level | Locks | MVCC (row versions) |
|---|---|---|
| Read uncommitted | Non-repeatable read | Non-repeatable read |
| Read committed | Non-repeatable read | Non-repeatable read (shown above) |
| Repeatable read | No anomalyWaiting: T2 | No anomaly |
| Serializable | No anomalyWaiting: T2 | No 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.
Phantom read
Section titled “Phantom read”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?
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.
- T1
SELECT COUNT(*) WHERE bookings → aT1 counts the rows of bookings and gets 2. - T2
INSERT booking-3 = 1T2 inserts booking-3 with the value 1. It is not committed yet. - T2
COMMITT2 commits. - T1
SELECT 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. - T1
COMMITT1 commits.
State after this step
| Row | Committed | Not committed | Locks held |
|---|---|---|---|
booking-1 | 1 | none | none |
booking-2 | 1 | none | none |
booking-3 | 1 | none | none |
| Transaction | Status | Values read so far |
|---|---|---|
| T1 Counts the bookings twice | Committed | a = 2, b = 3 |
| T2 Adds a booking | Committed | none |
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.
| Isolation level | Locks | MVCC (row versions) |
|---|---|---|
| Read uncommitted | Phantom read | Phantom read |
| Read committed | Phantom read | Phantom read |
| Repeatable read | Phantom read (shown above) | No anomaly |
| Serializable | No anomalyWaiting: T2 | No 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.
Anomalies where two writes collide
Section titled “Anomalies where two writes collide”Lost update
Section titled “Lost update”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.
Each statement reads the newest committed version of the rows. Readers never wait for writers.
- T1
SELECT balance → aT1 reads balance and gets 100. - T2
SELECT balance → bT2 reads balance and gets 100. - T1
UPDATE balance = a + 30T1 writes 130 to balance. It is not committed yet. - T1
COMMITT1 commits. - T2
UPDATE balance = b + 20T2 writes 120 to balance. It is not committed yet. - T2
COMMITAnomalyT2 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
| Row | Committed | Not committed |
|---|---|---|
balance | 120 | none |
| Transaction | Status | Values read so far |
|---|---|---|
| T1 Deposits 30 | Committed | a = 100 |
| T2 Deposits 20 | Committed | b = 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.
| Isolation level | Locks | MVCC (row versions) |
|---|---|---|
| Read uncommitted | Lost update | Lost update |
| Read committed | Lost update | Lost update (shown above) |
| Repeatable read | No anomalyT2 aborted: deadlockWaiting: T1 | No anomalyT2 aborted: serialization failure, concurrent update |
| Serializable | No anomalyT2 aborted: deadlockWaiting: T1 | No 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.
Write skew
Section titled “Write skew”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?
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.
- T1
SELECT COUNT(*) WHERE on-call → nT1 counts the rows of on-call and gets 2. - T2
SELECT COUNT(*) WHERE on-call → mT2 counts the rows of on-call and gets 2. - T1
IF n ≥ 2: UPDATE alice = 0T1 writes 0 to alice. It is not committed yet. - T2
IF m ≥ 2: UPDATE bob = 0T2 writes 0 to bob. It is not committed yet. - T1
COMMITT1 commits. - T2
COMMITAnomalyT2 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
| Row | Committed | Not committed |
|---|---|---|
alice | 0 | none |
bob | 0 | none |
| Transaction | Status | Values read so far |
|---|---|---|
| T1 Alice goes off call | Committed | n = 2 |
| T2 Bob goes off call | Committed | m = 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.
| Isolation level | Locks | MVCC (row versions) |
|---|---|---|
| Read uncommitted | Write skew | Write skew |
| Read committed | Write skew | Write skew |
| Repeatable read | No anomalyT2 aborted: deadlockWaiting: T1 | Write skew (shown above) |
| Serializable | No anomalyT2 aborted: deadlockWaiting: T1 | No 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.
Sort them
Section titled “Sort them”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.
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.