---
title: "Write Skew & Serializability"
description: "Watch disjoint writes empty a shared roster, then reject the impossible serial order and retry."
source: https://vizlearn.app/cs/databases/write-skew-serializability
access: premium (interactive player requires Lifetime; this doc is free)
---

# Write Skew & Serializability

Write skew occurs when concurrent transactions make decisions from overlapping reads, then
commit changes to different rows that together violate a rule. Snapshot isolation can allow
both writes. Serializability requires the committed reads and effects to match some execution
of whole transactions one after another, ruling out this history.

Suppose A and B are both on call. Each may leave only if somebody will remain. Both check the
roster, both see two names, and both leave. Every local check passed. The empty roster is the
part neither check considered.

## How Write Skew Works

In this lesson, T1 handles A's departure and T2 handles B's. Each transaction reads the whole
roster, counts the people marked ON, and writes its own row OFF only when the count exceeds 1.
With snapshot isolation, its first read fixes the row versions it will see. Another transaction
committing later does not refresh that snapshot.

A private write is still private. When T1 stages A OFF, the shared committed roster remains
A ON, B ON. Only T1's successful commit makes A OFF visible to a fresh reader. T2 continues to
use its original snapshot.

T1 writes A; T2 writes B. A same-row write-conflict check finds no overlap. Both transactions
can commit, leaving zero people on call. This is not one writer overwriting the other's update.
The conflict is between what each transaction **read** and what the other **writes**.

## Write Skew Step by Step

The default uses **Snapshot isolation** and **Last two on call**.

1. Start with A ON and B ON. The rule requires at least one person to remain.
2. T1 begins and reads both rows. Its count is 2; A leaving would leave 1, so its local check
   permits departure.
3. T2 begins and reads the same committed rows. Its count is also 2, and it independently
   permits B's departure.
4. T1 stages A OFF and T2 stages B OFF. Both writes remain private.
5. T1 commits A OFF. One person remains, but T2's snapshot still contains two ON rows.
6. T2 passes the same-row write check and commits B OFF. Nobody remains.
7. Reset a separate replay roster. T1 reads 2, leaves, and commits A OFF. T2 then reads 1
   and stays. The original T2 read 2, so this order cannot reproduce it: T2 must precede T1.
8. Reset again and reverse the order. T2 reads 2 and leaves; T1 reads 1 and stays. The
   original T1 read 2, so it must precede T2. The two requirements cannot both hold.

The animation keeps snapshots in the outer transaction cards and committed rows in the center.
An OFF chip crosses into the center only on commit. The opposing arrows below the roster mean
“must precede in a serial order”; they are neither network messages nor waiting locks.

## What Serializability Requires

You can test this history without guessing how a database schedules threads. There are only
two possible serial orders:

| Serial order | First read | Second read | Person remaining |
| --- | --- | --- | --- |
| T1, then T2 | T1 reads 2 and leaves | T2 reads 1 and stays | B |
| T2, then T1 | T2 reads 2 and leaves | T1 reads 1 and stays | A |

The concurrent history records T1 = 2 and T2 = 2. Neither row in the table matches. The cycle
`T1 → T2 → T1` expresses that contradiction. These are read/write dependencies: a reader of an
old version must precede the transaction replacing it in an equivalent serial history.

Switch the isolation control to **Serializable** without changing the workload. The teaching
model admits T1 first, then rejects T2's original attempt because no serial order fits both.
B stays ON: T2's private OFF value never became committed data.

The application now retries the entire request as T2′. Its new read sees A OFF and B ON. The
count is 1, so it refuses B's departure and commits without a write. The final history is
equivalent to T1 followed by T2′. The original attempts still overlapped; serializability is a
property of their admitted observations and effects, not a requirement to run all work serially.

The final proof replays the successful attempts: T1 reads 2 and leaves; T2′ reads 1 and stays.
Both the observations and the resulting roster match, so `T1 → T2′` receives a green YES.
The aborted T2 appears only as a history receipt and is excluded from this committed order.

PostgreSQL documents snapshot isolation's possible serialization anomalies, its Serializable
read/write monitoring, and the need to retry complete transactions after a serialization
failure. Its actual detection machinery is more involved than this lesson's two-order checker.
[PostgreSQL transaction isolation](https://www.postgresql.org/docs/current/transaction-iso.html).

## Edge Cases

- **One at a time:** T1 commits before T2 reads. T2 sees one remaining person and stays. Both
  isolation variants accept the history without an abort.
- **Spare capacity:** add C, who never requests departure. Under overlapping snapshot reads,
  both transactions read 3 and leave, so C remains. The roster rule holds, but the history is
  still not serializable: in either serial order, the second transaction must read 2.
- **Retry with spare capacity:** T2′ reads 2 after T1 commits and can safely leave. Only C
  remains, just as in the snapshot-isolated result, but the successful reads now fit a serial
  order. Equal final rows alone do not establish serializability.
- **Read-only retry:** when only B remains, T2′ performs no UPDATE. Its successful commit still
  completes the request and admits its read into the history.

## Common Mistakes

- **Checking only write-set overlap.** Disjoint writes do not protect a rule whose decision
  reads multiple rows. Follow the read/write dependencies too.
- **Treating a stable snapshot as a globally serializable history.** Both snapshots can be
  individually consistent while their decisions cannot coexist in any serial order.
- **Retrying only the UPDATE.** A new transaction that blindly writes B OFF after T1 can be
  perfectly serializable and still empty the roster. The application omitted the rule check;
  isolation cannot reconstruct it. Retry the reads and decision as well.
- **Assuming Serializable enforces an unstated rule.** This rule survives serial execution
  because the initial roster satisfies it and each transaction checks it correctly.
- **Assuming the same transaction always aborts.** T2 is the deterministic victim here.
  A real engine may choose differently or detect the problem before commit.

## A Note on Simplification

This is a fixed-row teaching model with two departure requests and at most one retry. It
validates possible serial orders directly. It does not implement PostgreSQL SSI, predicate-lock
granularity, dangerous-structure detection, lock queues, phantom inserts, crash recovery, or
retry backoff. Same-row write contention is outside the supplied scenarios. Isolation-level
names and implementations differ across databases; do not assume every `Repeatable Read`
mode is snapshot isolation.
