Two valid transactions left nobody on call
Failure story 17, in concurrency and ownership.
The challenge
Section titled “The challenge”Two clinicians are on call. Each asks to go off call at the same moment. Each transaction checks that at least one other clinician is still on call, and each commits. Who’s on call?
The setup
Section titled “The setup”| Assumption | Value |
|---|---|
| Database | PostgreSQL, with transactions at the REPEATABLE READ isolation level, which PostgreSQL implements as snapshot isolation |
| Table | on_call(shift_id, clinician_id, on_call boolean), one row per clinician per shift |
| Rule | A shift must always have at least one clinician on call |
| Request | “Take me off call”, which runs the transaction below |
BEGIN ISOLATION LEVEL REPEATABLE READ;SELECT count(*) FROM on_call WHERE shift_id = 'night-11-21' AND on_call; -- must be at least 2UPDATE on_call SET on_call = false WHERE shift_id = 'night-11-21' AND clinician_id = :me;COMMIT;What happened
Section titled “What happened”| Step | Dr Rao’s transaction | Dr Kim’s transaction |
|---|---|---|
| 1 | Begins; its snapshot shows Rao and Kim on call | Begins; its snapshot shows Rao and Kim on call |
| 2 | Counts 2: allowed | Counts 2: allowed |
| 3 | Updates Rao’s row | Updates Kim’s row |
| 4 | Commits | Commits |
| 5 | Nobody is on call for the night shift. Both doctors were told You’re off call. |
There was no error and no lock wait, and each transaction was correct by itself.
Why it went wrong
Section titled “Why it went wrong”This is write skew. Snapshot isolation guarantees that each transaction sees a consistent snapshot and that two transactions don’t overwrite the same row. These two read the same rows but wrote different rows, so snapshot isolation saw no conflict. The rule they were protecting, at least one on call, depends on both rows together, and nothing in the database knew that.
READ COMMITTED, PostgreSQL’s default, allows the same outcome. Only SERIALIZABLE promises that the result is the same as if the transactions had run one after the other, and run one after the other, the second doctor would have been refused.
Two designs
Section titled “Two designs”| Aspect | Design A: snapshot isolation | Design B: serializable, with retry |
|---|---|---|
| Isolation level | REPEATABLE READ |
SERIALIZABLE |
| The two requests together | Both commit | One commits; the other fails with SQLSTATE 40001 (serialization failure) |
| Application code | Runs once | Retries the whole transaction on 40001 |
| The retried request | Not applicable | Counts 1 on call, so it refuses: You can’t go off call: you’re the only clinician on call |
The retry isn’t optional. PostgreSQL’s serializable mode prevents the anomaly by aborting one transaction, and expects the application to run it again. Without the retry, the second doctor sees a database error instead of a sensible answer.
Another way to fix it is to make the conflict visible to the database by locking the rows the rule depends on, so the second transaction has to wait: see the exercise.
The check
Section titled “The check”Run the two requests concurrently, with a test hook that pauses each transaction after its count and before its update, so the interleaving is guaranteed.
- Expected: after both requests finish, at least one clinician is still on call.
- Expected: the second caller gets a clear refusal, not a 500 error: You’re the only clinician on call.
- Run it 1,000 times without the hook. Expected: the same result every time. Under Design A, the failure appears whenever the requests overlap.
This check is in the checks catalog as test templates in Java and Python.
When the simpler design is enough
Section titled “When the simpler design is enough”- The rule involves one row only, such as a balance can’t go below zero, enforced by a single conditional update or a check constraint. Snapshot isolation handles that.
- Concurrent requests that affect the same rule are impossible by construction, for example because one process handles all changes to a shift.
For educators
Section titled “For educators”Discussion question. What other rules in your systems span several rows: room bookings, overlapping appointments, account limits across cards, unique usernames across tables? How are they enforced today?
Exercise. Your team can’t move the application to SERIALIZABLE this quarter. Using PostgreSQL’s default READ COMMITTED level, change the transaction so the rule still holds, and explain why it works.
Worked answer
Lock the rows the rule depends on before counting them:
BEGIN; -- READ COMMITTEDSELECT clinician_id FROM on_call WHERE shift_id = 'night-11-21' AND on_call FOR UPDATE; -- locks every on-call row for the shift-- the application counts the rows returned and refuses if there are fewer than 2UPDATE on_call SET on_call = false WHERE shift_id = 'night-11-21' AND clinician_id = :me;COMMIT;Dr Rao’s transaction locks both rows. Dr Kim’s SELECT … FOR UPDATE waits. When Rao commits, PostgreSQL re-checks Kim’s query against the updated rows: Rao’s row no longer has on_call = true, so only Kim’s row is returned. Kim’s transaction counts 1 and refuses. This works because it turns two transactions reading the same rows into two transactions locking the same rows, which the database does see. (FOR UPDATE can’t be combined with count(*), which is why the count moves into the application.)
Further reading
Section titled “Further reading”- PostgreSQL’s documentation on transaction isolation (opens in a new tab), including which anomalies each level permits and the need to retry serialization failures.
- The PostgreSQL wiki’s Serializable Snapshot Isolation (opens in a new tab) page, with worked examples of write skew.
Related pages
Section titled “Related pages”Written and edited by Abhinav Taduka, enterprise software architect.
New pages are added regularly. Follow Abhinav on LinkedIn to see them first.