An invariant describes a condition that must remain true after every commit. “At least one technician stays on call” is a property of a set of rows. Each technician can read a consistent snapshot, follow the business rule correctly, and still write a combination of changes that breaks it.
The on-call rule spans two rows
Ada and Bo each see two doctors on call and update different rows. Both writes can commit, leaving the rota empty.
Why it exists
This failure is called write skew. Lost-update protection on individual records will happily confirm that nobody else edited Ada’s row and nobody else edited Bo’s row. The hidden dependency is the roster each person read before making their decision.
View diagram · The shared shiftWrite skew violates a condition spanning both rows. Full size
Words to know
Write skew
Transactions change different records after reading overlapping state and together violate a rule.
Serialization failure
A transaction rejection that requires a new decision on fresh data.
How it works
Step through the idea, or press Play.
Step 1 of 3
Name the invariant. At least one person must remain for each shift. Atomic transactions protect the changes inside each transaction; the isolation level determines which combinations of concurrent decisions may commit.
Construct the schedule. Each transaction reads the same roster and writes a different row. Under snapshot isolation the writes can avoid a direct write conflict. Comparing only the edited row’s version misses this write skew.
Pick a serialization point. Lock the shift header before checking the roster, or run the entire decision at serializable isolation and retry serialization failures. When locking multiple shift headers, use a common order to reduce deadlocks.
Understand it
Start here
Two names on the night roster
Ada and Bo each promise to leave only if somebody else stays. Read the roster from each person’s point of view before either changes a row.
Two names on the night roster
Lock the shared shift before reading or retry the serializable transaction.
Structure
The shared shift
The shared shift
Write skew violates a condition spanning both rows.
A reproducible write skew
Start with Ada and Bo both on call. Each transaction reads a count of two before either writes. Ada updates only Ada; Bo updates only Bo. With both commits accepted, the count is zero. Rerun with the shift-header lock acquired before reading the count.
Give each shift a durable header. Every operation that changes membership first locks that header and then evaluates the roster using current data. Different shifts can proceed independently. The cost is that a busy shift serializes its writers. An administrative import that bypasses the header can still invalidate the guarantee.
When the database rejects a transaction
Serializable isolation may reject a transaction whose observations cannot be explained by a serial ordering. Retrying only the final update would reuse the very decision that became invalid. Start a fresh transaction, read the roster again, and tell the second technician that the shift still needs coverage. Delay external notifications until the decision commits.
Serialize the roster decision
Lock the shared shift before reading or retry the serializable transaction.
Guard only individual rows
Both writers can pass their own version checks.
Summary
The whole topic in one minute.
Identify the complete set of data behind the rule. Guard that decision explicitly or let serializable execution reject incompatible decisions.