Isolation levels and write skew 00:00:01 Two doctors start on duty There are exactly two doctors on call, Ada and Bo. Each asks to leave the shift, but only if somebody else remains available. Both requests arrive before either decision is committed. The roster rule requires at least one doctor to remain. Either person can leave while the other stays; permitting both departures would leave the hospital uncovered. Both transactions begin at the same moment, reading the roster before any write happens. Give each doctor a separate record, and write the business rule above both names. The database is being asked to protect a relationship between records, although each doctor will modify only one. 00:00:41 Follow both private snapshots In Ada’s private snapshot Bo is on duty. Ada therefore stages a change to her own row. She has not yet observed a committed departure by Bo. Bo sees the original snapshot too. He stages a change to his own row while the visible copy of Ada’s record still says she is on call. Each local decision looks acceptable in its own snapshot. At this instant the changes are staged, and neither doctor has written the other doctor’s row. Reading both rows is necessary to make the decision, but it is not sufficient to guard the condition against another concurrent decision. The uncommitted edits belong to different private transactions at this point. 00:01:24 Why write conflicts missed the rule Under snapshot isolation, each transaction sees a consistent snapshot and writes independently. Both writes commit, leaving the roster empty, which violates the rule that one doctor must remain. This is write skew, not a simple overwrite—different rows were changed, but the combined result breaks the rule. No number was replaced by an independently computed stale copy of that same number. Each row was written exactly once. It is the combined state of the two different rows that is impossible under the rule. 00:02:00 Replay the two possible serial orders If Ada commits first, Bo sees no one else on call and must stay, so his transaction fails. Reverse the serial order. Bo leaves first, then Ada sees nobody else on call and stays. In neither serial order do both doctors leave. The final state has no one on call, which cannot be explained by either serial order. This thought experiment gives a practical definition of the anomaly. If a committed outcome fits no serial order of the same transactions, consistent snapshots for individual readers were not enough to provide serializability. 00:02:38 Enforce and retry One option is to enforce serializability and retry aborted operations. Another is to synchronize this particular shift rule through a shared guard row that every change checks under the same transaction. Avoid long external calls while holding locks to prevent blocking other transactions. After one doctor successfully leaves, the other must rerun the rule against current state. The second request should be refused for this shift if it would leave nobody on call. Keep the guard or serializable transaction short, and test both doctors submitting together. The application needs to handle a serialization failure by rerunning the decision from a fresh view, not merely repeating the last write.