What are isolation levels and which concurrency problems do they solve?
Isolation levels define what a transaction sees of other transactions' changes, and so which concurrency anomalies it is protected from. These are dirty read, non-repeatable read, phantom read and serialization anomaly: the higher the level, the stricter and costlier the protection. PostgreSQL defaults to Read Committed, and a lost update is cured with an atomic UPDATE with a condition, FOR UPDATE locking, or a higher level with a transaction retry.
SQL · Changing data and transactions
An account holds 100. Two requests withdraw 80 at the same time and both succeed. How did that happen?
A user pressed "Withdraw 80" twice or two devices sent a request at once. Each request is correct alone, but together they give a negative balance or a lost debit. These are concurrency errors, and isolation levels describe which of them the database protects against. This lesson shows the anomalies on two sessions and three ways to avoid them.
How it works and why. Isolation defines what a transaction sees of other people's changes. Full serialisation is safe but slow, so the SQL standard describes four levels from weak to strong, and the database lets you choose between speed and strictness. The anomalies: dirty read (reading other people's uncommitted changes), non-repeatable read (re-reading a row gives another value), phantom read (a repeated query with the same filter returns another set of rows) and serialization anomaly (the result of concurrent transactions is impossible under any serial order). The table for PostgreSQL:
| Level | Dirty read | Non-repeatable | Phantom | Serialization |
|---|---|---|---|---|
| Read Uncommitted | no (as Read Committed) | possible | possible | possible |
| Read Committed | no | possible | possible | possible |
| Repeatable Read | no | no | no (in PostgreSQL) | possible |
| Serializable | no | no | no | no |
PostgreSQL defaults to Read Committed, MySQL InnoDB to Repeatable Read. The standard allows phantoms at Repeatable Read, but the PostgreSQL implementation (the whole transaction sees one snapshot, taken at its first statement, not a new one per statement as in Read Committed) does not. The level is set like this: BEGIN ISOLATION LEVEL REPEATABLE READ;.
Example: a lost update. Account 10, balance 100. The application reads the balance, computes the new one in code and writes the number back. Read Committed:
| Step | Session A | Session B | balance |
|---|---|---|---|
| 1 | BEGIN; SELECT balance → 100 | 100 | |
| 2 | BEGIN; SELECT balance → 100 | 100 | |
| 3 | UPDATE … SET balance = 20; COMMIT | 20 | |
| 4 | UPDATE … SET balance = 20; COMMIT | 20 |
Both withdrawals of 80 are confirmed and the balance is 20, although after the first one it was 20 and the second should not have happened. A debit is lost with no error. Three fixes:
-- 1. an atomic update with a condition: the second one waits
-- for the row lock, then sees 20 and does not pass
UPDATE accounts
SET balance = balance - 80
WHERE id = 10 AND balance >= 80;
-- A: UPDATE 1; B: UPDATE 0 -> the application answers with a refusal
-- 2. an explicit lock: B waits, then reads 20
BEGIN;
SELECT balance FROM accounts WHERE id = 10 FOR UPDATE;
-- the application checks the value read: 20 < 80 -> ROLLBACK
UPDATE accounts SET balance = balance - 80 WHERE id = 10;
COMMIT;
-- 3. a higher level: the second one gets an error and must retry
BEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT balance FROM accounts WHERE id = 10; -- 100
-- (meanwhile A wrote 20 and committed)
UPDATE accounts SET balance = 20 WHERE id = 10;
-- ERROR: could not serialize access due to concurrent update
Two ways to handle the debit
Read, compute in code, write the number
A window between read and write lets another change in: an update is lost.
One atomic change with a condition
SET balance = balance - 80 WHERE balance >= 80: the check and the write in one statement.
How QA tests this: sends identical requests at once and checks the outcome.
// withdraw: a custom helper that calls the withdrawal API
const [a, b] = await Promise.all([withdraw(10, 80), withdraw(10, 80)]);
// exactly one request must succeed; the refusal code depends on the API
expect([a.ok, b.ok].filter(Boolean)).toHaveLength(1);
expect(await balance(10)).toBe(20);
Limits and alternatives. One run is not enough: a race depends on timing, so the test is repeated and the number of parallel requests is raised. Higher levels are not free: Repeatable Read and Serializable in PostgreSQL return a serialization error (code 40001), and the application must retry the transaction, which is a separate branch to test. FOR UPDATE locking adds waiting and the risk of a deadlock. Another strategy, optimistic: a version column and the condition WHERE version = ….
Common mistakes.
- Treating a lost update with an isolation level when an atomic
UPDATEis enough. - Testing concurrency with one sequential test: the race does not reproduce.
- Expecting dirty reads in PostgreSQL: they do not occur even at Read Uncommitted.
- Not handling a serialization error with a retry.
- Carrying conclusions between databases: MySQL has another default level and other locking behaviour.
Check yourself
Why does UPDATE … SET balance = balance - 80 WHERE id = 10 AND balance >= 80 not lose a debit even at Read Committed?
Answer
The statement locks the row. The second UPDATE waits for the first to finish, then re-checks the condition on the new value (20): balance >= 80 is false, so 0 rows are changed.
At Repeatable Read two concurrent debits give the second could not serialize access due to concurrent update. Is that an application bug?
Answer
No, it is expected: the database refuses instead of losing the update. It is a bug if the application does not retry the transaction and gives the user a 500 instead of a correct result.