Search

Search papers and lessons

Write Skew

Level 4 · 12 min read

Write skew is an anomaly where two transactions each check a rule on their own snapshot, write different rows, and together break the rule, which snapshot isolation does not prevent.

On this page

  • Colour = topic mix
  • Systems
  • Databases
  • Discrete math
  • ReadReady to readNot yet

TL;DR

Write skew is a concurrency anomaly in which two transactions each read the same data, check a rule on it, and then write different rows. Because their write sets do not overlap, snapshot isolation lets both commit, and the rule can end up broken even though each transaction was correct on its own. The problem matters for any application whose rule spans rows that different transactions update.

Builds on: Snapshot Isolation · Leads to: Serializability, the guarantee that write skew violates

Where it's used

Shared account limits. A bank joins two accounts under a rule that their combined balance must stay at or above zero. Two withdrawals run at the same time, as a race, each on a different account. Each checks the combined balance on its own snapshot, and each withdraws from a different account, so the database lets both through.

Seat and inventory counts. An airline splits ticket sales across desks, and each desk keeps its own counter of tickets sold. A rule caps the total at the number of seats on the plane. Two desks that check the total and then increment their own counters can each see room and together oversell the flight.

On-call and shift rules. A hospital requires at least one doctor on call at all times. Two doctors each check that the other is still on call, then each marks themselves off duty. Each sees a valid state, and the database writes two different rows, so the ward ends up with nobody on call. The same pattern appears in any roster that must keep a minimum number of people available.

How it works

Two rules are enough to see the problem: each transaction reads a shared value, and the two transactions write different rows. Here is the path with two accounts, x and y, under the rule x + y at least 60. Both start at 50, so the sum is 100.

flowchart TD
  I["Committed state: x = 50, y = 50, rule x + y at least 60"] --> T1["T1 reads x and y on its snapshot, sum 100, withdraws 40 from x"]
  I --> T2["T2 reads x and y on its snapshot, sum 100, withdraws 40 from y"]
  T1 --> C1["T1 writes x only, no other commit touched x, so it commits"]
  T2 --> C2["T2 writes y only, no other commit touched y, so it commits"]
  C1 --> F["Final state: x = 10, y = 10, sum 20, rule broken"]
  C2 --> F
  1. The committed state is x = 50 and y = 50, so the sum is 100. The rule needs the sum to be at least 60. Check: 100 is at least 60, so the state is valid.

  2. Transaction T1 starts with a snapshot in which x = 50 and y = 50. It reads both, sees a sum of 100, and decides a withdrawal of 40 from x is safe, because 100 minus 40 is 60. Check: 60 is at least 60, so T1 believes the rule still holds.

  3. Transaction T2 starts with its own snapshot, which is the same committed state. It also sees a sum of 100 and decides a withdrawal of 40 from y is safe. Check: T2 also computes 100 minus 40 equals 60, so it agrees.

  4. T1 commits first. Its write set is only x, and no other transaction has committed a change to x since T1 started. The rule for snapshot isolation is satisfied, so x becomes 10. Check: the write-write rule looks only at x, and nothing else touched x.

  5. T2 commits. Its write set is only y, and no other transaction has committed a change to y since T2 started. So y becomes 10. Check: the two write sets, {x}\{x\} and {y}\{y\}, do not intersect, so the commit rule passes for both.

  6. The final state is x = 10 and y = 10, with a sum of 20. The rule that the sum be at least 60 is broken. Each transaction was correct on its snapshot, and the combined result is not. Check: if the two withdrawals had run one after the other, in a serial order, the second would have seen a sum of 60 and then computed 60 minus 40 equals 20, which fails the rule and would have been refused.

The step that breaks is step 5. Snapshot isolation checks whether two transactions wrote the same item. Here they read overlapping items and wrote disjoint ones, so the check finds nothing to stop them.

The math

Give each transaction TT a read set R(T)R(T), the items it reads, and a write set W(T)W(T), the items it writes. Two transactions are concurrent when their lifetimes overlap. Under snapshot isolation, a commit of T2T_2 is refused only when a concurrent transaction T1T_1 has already committed with

W(T1)∩W(T2)≠∅.W(T_1) \cap W(T_2) \neq \varnothing.

The symbol ∅\varnothing is the empty set, so this says the two write sets share at least one item. This is the first-committer-wins rule of Snapshot Isolation.

Write skew is the situation where two concurrent transactions avoid that check and still form a cycle of reads against writes:

W(T1)∩W(T2)=∅,R(T1)∩W(T2)≠∅,R(T2)∩W(T1)≠∅.W(T_1) \cap W(T_2) = \varnothing, \qquad R(T_1) \cap W(T_2) \neq \varnothing, \qquad R(T_2) \cap W(T_1) \neq \varnothing.

The second condition says T1T_1 reads something T2T_2 writes, and the third says T2T_2 reads something T1T_1 writes. Each transaction is then working from a value the other one is about to change.

Step 1 in symbols: the snapshot and the rule

Let the invariant be I(x,y):x+y≥60I(x, y): x + y \geq 60. Each transaction TiT_i is written to preserve II on the state it read. Here R(T1)=R(T2)={x,y}R(T_1) = R(T_2) = \{x, y\}, which is why both see a sum of 100.

Step 2 in symbols: the disjoint writes

T1T_1 writes xx and T2T_2 writes yy, so W(T1)={x}W(T_1) = \{x\} and W(T2)={y}W(T_2) = \{y\}. Their intersection is empty, and the commit rule passes both. The reads cross over, since R(T1)R(T_1) contains yy, which T2T_2 writes, and R(T2)R(T_2) contains xx, which T1T_1 writes.

Step 3 in symbols: the invariant after both commits

The final values are x=50−40=10x = 50 - 40 = 10 and y=50−40=10y = 50 - 40 = 10, so

x+y=(50−40)+(50−40)=20<60.x + y = (50 - 40) + (50 - 40) = 20 \lt 60.

The invariant fails. If the two transactions had been serial, the second one would have read x+y=60x + y = 60 and would have computed 60−40=2060 - 40 = 20, which fails 20≥6020 \geq 60, so it would have been refused. That contrast is what serializability guarantees: the outcome must match some one-at-a-time order.

Worked examples

Example
1. One shared item: snapshot isolation prevents the problem

Two transactions each withdraw 40 from the same account, which holds 50, with the rule that the balance stays at least 0.

  1. Both transactions start with a snapshot in which the balance is 50.
  2. Each computes 50−40=1050 - 40 = 10, which satisfies the rule on its snapshot.
  3. The first transaction commits, and the balance becomes 10. Its write set is {a}\{a\}.
  4. The second transaction tries to commit. A concurrent transaction has committed a write to aa, so the intersection of write sets is not empty and the commit is refused.
  5. The final balance is 10, and the rule holds.

Interpretation: when the two transactions write the same item, the first-committer-wins rule saves the invariant. Write skew needs disjoint writes, which this example does not have.

Example
2. Two accounts with a sum rule: the standard case

Start with x=50x = 50 and y=50y = 50, and the rule x+y≥0x + y \geq 0. T1 withdraws 100 from xx, and T2 withdraws 100 from yy, each checking the rule on its snapshot.

  1. On its snapshot, T1 computes (50+50)−100=0(50 + 50) - 100 = 0, which is at least 0, so it proceeds.
  2. T1 writes x=50−100=−50x = 50 - 100 = -50. Its write set is {x}\{x\}.
  3. On its snapshot, T2 also computes (50+50)−100=0(50 + 50) - 100 = 0, so it proceeds.
  4. T2 writes y=50−100=−50y = 50 - 100 = -50. Its write set is {y}\{y\}.
  5. The write sets are disjoint, so both commit under snapshot isolation.
  6. The final sum is −50+(−50)=−100-50 + (-50) = -100, which is less than 0. The rule is broken.

Interpretation: each check was valid on its own snapshot. Only the combination of the two commits was wrong, and no single transaction saw that.

Example
3. Seat counts at two sales desks (realistic)

A flight has 100 seats. Two desks each keep their own counter of tickets sold, aa for desk A and bb for desk B. The rule is that a+b≤100a + b \leq 100. Both counters start with a total of 99: a=60a = 60 and b=39b = 39. Each desk sells a seat only if the total read from both counters is below 100.

  1. Desk A starts with a snapshot in which a+b=99a + b = 99. Since 99<10099 \lt 100, it sells one seat.
  2. Desk B starts with the same snapshot and also sees 9999, so it also sells one seat.
  3. Desk A writes a=61a = 61, and its write set is {a}\{a\}.
  4. Desk B writes b=40b = 40, and its write set is {b}\{b\}. The write sets are disjoint, so snapshot isolation commits both.
  5. The final total is 61+40=10161 + 40 = 101, which is greater than 100. The flight is oversold by one seat.

Try it: if desk B started only after desk A had committed, what total would it read, and would it sell a seat? Answer: it would read a+b=61+39=100a + b = 61 + 39 = 100, which is not below 100, so it would refuse to sell.

Common mistakes and tradeoffs

It is tempting to think that snapshot isolation prevents every anomaly that concurrent transactions can cause. In fact it prevents dirty reads, non-repeatable reads, and lost updates, but write skew passes, because the two transactions write disjoint items and the commit check never compares them.

It is tempting to think write skew only happens with unusual code. In fact it happens whenever one transaction reads a value that another transaction writes, and the rule relates the two. Any rule that spans rows and is checked before writing can trigger it.

It is tempting to think the fix is always to make both transactions write the same row. In fact that works, because first-committer-wins then aborts one of them. But it works only if you can identify a shared item to write, and it serializes work on that row. A common workaround is to create a dedicated row that both transactions write, which forces the conflict.

When to use snapshot isolation: read-heavy workloads where readers must not wait for writers, and where each rule touches only one row or one set of rows that a single transaction writes. When not to use it: rules that span rows read by one transaction and written by another. Alternatives: serializable isolation, such as the serializable snapshot isolation that PostgreSQL added in version 9.1, which tracks the read and write patterns that form the dangerous cycle; Two-Phase Locking, which locks what a transaction reads; and explicit locking such as selecting rows for update before the check.

History

Before the mid-1990s, the SQL standard described isolation levels by the anomalies they forbid, and the descriptions were loose. In 1995 Berenson and colleagues published a critique showing that a common level, snapshot isolation, permitted write skew and so was not serializable. Their paper named the anomaly and is the standard reference for what snapshot isolation does and does not prevent.

The anomaly caught on because snapshot isolation was widely implemented in multi-version databases, and because the cost of preventing write skew was not obvious to application developers. Systems that offered a serializable level later had to decide how to detect the dangerous cycle without the cost of locking every read. PostgreSQL's 9.1 release in 2011 added a serializable level built on snapshot isolation that does detect these patterns, which is the practical answer to the problem.

Recap

  • Snapshot isolation checks only whether two transactions wrote the same item.
  • Write skew occurs when two concurrent transactions read each other's write targets, write disjoint items, and together break a rule.
  • The sum rule x+y≥60x + y \geq 60 is the simplest example: each withdrawal is safe alone, but both together break it.
  • The fix is a stronger isolation level, a lock on what is read, or a conflict on a shared row.
  • Read next: Serializability for the guarantee write skew violates, and Two-Phase Locking for a lock-based alternative.

Check yourself

QuestionRecall: what is the write set of a transaction, and why does snapshot isolation's commit check look only at it?Show answer

The write set is the set of items a transaction writes. Snapshot isolation's first-committer-wins rule aborts a transaction only when a concurrent committed transaction wrote an item in its write set, so the check compares write sets only and never looks at what was read.

QuestionComputation: two transactions start with x = 30 and y = 30 under the rule x + y at least 40. Each withdraws 20 from a different account, checking the rule on its snapshot. What is the final sum, and does the rule hold?Show answer

Each transaction computes 60−20=4060 - 20 = 40 on its snapshot, which passes. Their write sets are {x}\{x\} and {y}\{y\}, which are disjoint, so both commit. The final values are x=10x = 10 and y=10y = 10, so the sum is 20, which is less than 40. The rule is broken.

QuestionWhy: why does serializing the two withdrawals in the previous question prevent the problem?Show answer

If one transaction commits first, the second one starts from a state where the sum is already reduced, so it reads a sum that reflects the first withdrawal. Its check then computes a sum below the threshold and refuses to withdraw. A serial order makes the second transaction read the first one's effect.

QuestionApplied: a booking system stores one row per room and a rule that no room is double-booked for the same night. Is that a write skew risk?Show answer

Not usually. Each booking checks and writes the same room row, so two bookings of one room have overlapping write sets, and snapshot isolation's first-committer-wins rule aborts one of them. Write skew needs a rule that spans rows, such as a total over all rooms that two transactions read and write separately.

Sources