AlgoViz

When two correct updates break a rule

medium 6 min

Two technicians are on call.

Video lesson · Chapter 1 of 5

Two doctors start on duty

0:00 / 3:25

Two doctors sign off, one broken rota

1:13Reel

Theory

Read or listen first. About 1 min.

What it is

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

On-call rotaStartAfterAdaOnOffBoOnOffTwo independent writes break the shared rule.
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 shift
Ada → Shift header (lock); Bo → Shift header (lock); Shift header → Ada row (guard); Shift header → Bo row (guard)
Write 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.

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
The night-shift rosterAdaon dutyOn call2 peopleBoon dutyTwo people cover the shift

Lock the shared shift before reading or retry the serializable transaction.

Structure

The shared shift

The shared shift

Ada → Shift header (lock); Bo → Shift header (lock); Shift header → Ada row (guard); Shift header → Bo row (guard)
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.

A shared decision across two rows

A schedule that empties the shift

StepAda’s transactionBo’s transaction
1Reads Ada and Bo on dutyReads Ada and Bo on duty
2Sets Ada off dutySets Bo off duty
3Commits its rowCommits its row under snapshot isolation
4Final rosterZero technicians on duty

Represent the rule where writers can meet

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.

Sources