Write skew

On this page 7

Write skew is a concurrency anomaly in which two concurrent transactions read the same data, each decides on a write based on what it read, and each then updates a different row, so that the combined result violates a constraint that neither transaction violated alone. In database transactions, write skew is the characteristic weakness of snapshot isolation: it is permitted there, yet it cannot occur under true serializable isolation.

The anomaly is subtle because no row is written twice and no individual change is invalid. The damage exists only in the relationship between rows, which is exactly what application invariants such as “at least one doctor stays on call” express.

How write skew arises under snapshot isolation

Write skew arises because snapshot isolation checks write-write conflicts only. Each transaction reads from a consistent snapshot taken at its start, and the database aborts a transaction only when two transactions modify the same row. When they modify disjoint rows, both commit, even if each one based its decision on data the other has just made obsolete.

The standard illustration involves two doctors on call. The rule requires that at least one doctor remains on call. The following sequence shows how both doctors can leave at once.

StepTransaction A, first doctorTransaction B, second doctor
1Counts on-call doctors: 2Counts on-call doctors: 2
2Concludes it is safe to leaveConcludes it is safe to leave
3Sets own status to off callSets own status to off call
4Commits successfullyCommits successfully
ResultZero doctors on callInvariant violated
Timeline of the on-call write skew

The pattern always has the same shape: read a condition, decide, write somewhere else. Formally, it forms a dependency cycle of two read-write antidependencies, each transaction reading a version that the other overwrites, which is why serializability theory classifies it as a non-serializable history.

Write skew among isolation anomalies

Write skew belongs to the family of isolation anomalies, but it differs from its better known relatives because it involves two different objects. The ANSI SQL standard defined isolation levels only through dirty reads, non-repeatable reads and phantoms, and the 1995 critique by Berenson and colleagues showed that this list missed write skew entirely.

AnomalyWhat goes wrongObjects involved
Dirty readReading uncommitted dataOne row
Non-repeatable readA reread row has changedOne row
Lost updateTwo writers overwrite one valueOne row
Phantom readA repeated query returns new rowsA predicate range
Write skewDisjoint writes break a shared invariantTwo or more rows
Write skew compared with related anomalies

A lost update can be seen as the degenerate case of write skew in which both transactions happen to write the same row; snapshot isolation prevents it through first-committer-wins. A related variant is phantom-induced write skew, in which the conflicting write inserts a new row that would have changed the other transaction’s search result, such as two bookings for the same meeting room and time slot.

Which isolation levels allow write skew

Whether write skew can happen depends on the actual implementation of an isolation level, not only on its name. Several databases label snapshot isolation as repeatable read or even as serializable, so the guarantees must be checked product by product.

Isolation levelWrite skew possibleNote
Read committedYesAlso allows several other anomalies
Snapshot isolationYesClassic setting for write skew
PostgreSQL repeatable readYesImplemented as snapshot isolation
Oracle serializableYesHistorically snapshot isolation in practice
PostgreSQL serializableNoSerializable snapshot isolation aborts cycles
Two-phase locking serializableNoPredicate or range locks block the pattern
Write skew exposure by isolation level

Serializable snapshot isolation (SSI), used by PostgreSQL since version 9.1, tracks read-write dependencies at run time and aborts one transaction when it detects a dangerous structure of two consecutive antidependencies. Systems based on strict two-phase locking prevent write skew differently, by holding shared locks on everything read until commit.

Preventing write skew in applications

The most robust defense is to run the affected transactions under true serializable isolation and to retry on serialization failure. Where that is not possible, the application must turn the hidden read dependency into an explicit conflict the database can see.

TechniqueHow it worksTrade-off
Serializable isolationDatabase detects or blocks the cycleRequires retry logic on aborts
SELECT … FOR UPDATELocks the rows the decision depends onFails when the checked rows do not yet exist
Materialized conflictAdds a lock row, such as a time slot recordPollutes the data model
Database constraintUnique, exclusion or check constraint enforces the ruleOnly some invariants are expressible
Single atomic statementCombines read and write in one UPDATEWorks only for simple conditions
Techniques that prevent write skew

Explicit locking with FOR UPDATE protects only rows that already exist. When the invariant concerns the absence of rows, as in double booking, there is nothing to lock, and the solution is either an exclusion constraint or a materialized lock row.

Detecting and testing for write skew

Write skew rarely appears in ordinary testing because it needs precise interleaving of concurrent transactions. It surfaces in production under load, often as data that breaks a business rule without any error in the logs. Code review should therefore look for the check-then-act pattern across multiple rows.

  • Audit invariants that span rows: minimum staffing, account balance limits, uniqueness without a constraint.
  • Reproduce with two sessions: open two transactions manually and interleave their statements step by step.
  • Use concurrency testing tools: frameworks such as Jepsen and Elle analyze histories for anomalies.
  • Monitor serialization failures: under SSI, a rising abort rate indicates real contention that retry logic must absorb.

In plain words

Imagine two roommates who share a rule: someone must stay home with the dog. Each one looks around, sees the other at home, and decides to go out. Both leave at the same moment. Neither did anything wrong alone, yet the dog ends up alone.

Write skew is the database version of this situation. Two operations each check the same facts, then change different things. Together, they break a rule that both of them believed they were respecting.

A real-world example

A conference center runs its room booking system on PostgreSQL at the default read committed level. The booking code first checks whether any reservation overlaps the requested time, and if none exists, it inserts a new one. During a busy registration morning, two staff members book Room 4 for 10:00 to 11:00 within the same few milliseconds. Both checks find no overlap, both inserts succeed, and two groups arrive for the same room.

The developers fix the problem by adding an exclusion constraint on the room identifier and the time range, so that the database itself rejects overlapping reservations. They also move the booking transaction to serializable isolation with an automatic retry of up to three attempts. Double bookings stop entirely, and the retry rate stays below one percent of booking requests.