Sub topic 7
Concurrency anomalies: what goes wrong without isolation
Prerequisites
This chapter assumes you know basic SQL, including SELECT, UPDATE, primary keys, and constraints. You should also understand that several application sessions can use the same database at the same time.
Isolation controls how much one transaction can observe or affect while another transaction is running. Weak isolation can improve concurrency, but it allows anomalies, which are surprising or incorrect results caused by overlapping transactions.
A shared example
Suppose an accounts table contains one account with a balance of 100. Two sessions, A and B, can read or change that row concurrently.
CREATE TABLE accounts (
id integer PRIMARY KEY,
balance numeric(12, 2) NOT NULL
);
INSERT INTO accounts (id, balance) VALUES (1, 100.00);
-- Session A
BEGIN;
UPDATE accounts SET balance = balance - 30 WHERE id = 1;
-- Do not commit yet.
-- Session B
SELECT balance FROM accounts WHERE id = 1;The result in Session B depends on the database's isolation rules. The following anomalies describe the main risks.
Dirty reads
A dirty read occurs when a transaction reads data that another transaction changed but has not committed. If the writer later rolls back, the reader used a value that never became permanent.
For example, Session B might see a balance of 70 while Session A's debit is still uncommitted. If Session A rolls back, the real balance returns to 100. Most production databases prevent dirty reads, even at their lowest practical isolation level.
Non-repeatable reads
A non-repeatable read occurs when the same query returns different values during one transaction.
- Session A reads an account balance of 100.
- Session B changes the balance to 70 and commits.
- Session A reads the balance again and gets 70.
The first and second reads used different committed versions of the row. READ COMMITTED commonly permits this behavior because each statement gets a fresh view of committed data.
Phantom reads
A phantom read concerns a set of rows rather than one row. A transaction runs a condition-based query twice, but the second execution includes new or removed rows.
For example, Session A counts all pending orders and gets 5. Session B inserts another pending order and commits. Session A repeats the count and gets 6. The new row is a phantom.
Lost updates
A lost update occurs when two transactions read the same value and then write results based on that old value.
| Step | Session A | Session B |
|---|---|---|
| 1 | Reads balance 100 | Reads balance 100 |
| 2 | Plans to subtract 30 | Plans to subtract 20 |
| 3 | Writes 70 | Writes 80 |
| Result | A's change disappears | Final balance is 80 |
Use an atomic update, such as SET balance = balance - 30, or lock the row before calculating a new value.
Write skew
Write skew occurs when transactions read shared facts, update different rows, and together violate a rule.
Two doctors are on call. Each transaction checks that at least two doctors are on call, then removes a different doctor. Both checks pass, but both transactions can commit, leaving nobody on call. Serializable isolation detects this conflict and aborts one transaction.
Choosing protection
Use stronger isolation when correctness depends on a stable view or a rule spanning multiple rows. Use row locks when a read must be followed by a related write. Use atomic conditional updates when one SQL statement can enforce the rule.
Exercise
Which anomaly occurs when a transaction reads a row twice and another transaction commits an update between those reads?
Show answer
A non-repeatable read.