arthrop0d

Brief articles on varied topics.

Serializable Snapshot Isolation

sqldatabases

Serializable snapshot isolation (SSI) provides serializable transactions while retaining snapshot-based reads. It lets transactions read a consistent version of the database, then watches for read/write dependency patterns that could not occur in any one-at-a-time execution; when such a pattern is detected, it aborts a transaction rather than allowing an unsafe result.

The problem: snapshots can still produce impossible results

A serializable execution has the same effect as running the transactions one after another in some order. That guarantee matters when a transaction checks a rule and then changes data based on what it saw.

Snapshot isolation (SI) gives each transaction a consistent view of committed data as of a particular point in time. This is usually implemented with multi-version concurrency control (MVCC): instead of overwriting a row immediately, the database keeps versions and lets readers choose the version visible to their snapshot.

SI avoids many read/write conflicts without making readers wait. It also commonly prevents two concurrent transactions from updating the same row: one of them loses a first-committer-wins check and is aborted. But transactions can still make mutually inconsistent decisions when they update different rows.

Consider a rule saying that at least one doctor must remain on call. Initially both doctors are on call:

-- Transaction 1
BEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT count(*) FROM doctors WHERE on_call;
UPDATE doctors SET on_call = false WHERE name = 'Alice';
COMMIT;
-- Transaction 2, running concurrently
BEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT count(*) FROM doctors WHERE on_call;
UPDATE doctors SET on_call = false WHERE name = 'Bob';
COMMIT;

Both transactions can see two on-call doctors in their snapshots. Each turns off a different doctor, so neither encounters a same-row write conflict. Both can commit, leaving nobody on call. No ordering of the two complete transactions produces that result: if transaction 1 ran first, transaction 2 would have seen only one doctor and should not have made its change.

This is write skew: transactions read overlapping facts, then write separate data based on those facts.

Dependencies expose the unsafe pattern

SSI adds a layer of conflict tracking to snapshot isolation. The database records when one transaction reads data that another concurrent transaction later changes. This is a read/write dependency, also called an anti-dependency.

If transaction T1 reads a row version and concurrent transaction T2 writes a new version of that row, the dependency is represented as:

T1  --read/write-->  T2

The arrow means that T1 must appear before T2 in a serial order: T1 read the older state before T2 changed it. Unlike a normal write conflict, this dependency does not necessarily force either transaction to wait.

For the doctor example:

Together these edges form a cycle. A cycle means there is no single serial order that explains the observed reads and writes, so the execution is not serializable.

Not every suspicious pair is a complete cycle. SSI therefore looks for a dangerous structure: typically two consecutive read/write dependencies such as T1 -> T2 -> T3, combined with concurrency and commit-order conditions that could allow the rest of a serialization cycle to exist. This is enough to identify a transaction that must be stopped; the database does not need to wait for every possible dependency to become visible.

In practice, the database tracks information such as which transactions have read versions and which transactions later changed them. It can retain a compact summary after a transaction finishes, because a still-running transaction may depend on a transaction that has already committed. Implementations also use cleanup and memory-management techniques to keep this tracking bounded.

The response is an abort, not a block

When SSI finds a dangerous structure, it aborts one transaction, often reporting an error equivalent to “could not serialize access due to read/write dependencies.” The application can retry the whole transaction. On the retry, one of the earlier transactions has finished, so the dependency cycle cannot form in the same way.

Aborting is necessary because the problematic transaction may already have returned results to its application. At that point, blocking it cannot change the reads it already performed. The database could delay a commit, but the transaction's decisions were made from a snapshot that is now known to be incompatible with a serial execution. Discarding and retrying is the clean way to obtain a new, consistent set of reads.

SSI also deliberately keeps the main benefit of snapshots: ordinary reads do not have to wait behind writers, and writers do not have to lock every row that a transaction merely inspected. A lock-based serializable scheme can prevent some anomalies by blocking earlier, but that can increase latency and create deadlock scenarios. SSI is an optimistic approach: allow useful concurrency, detect a dependency pattern that threatens correctness, and sacrifice one transaction only when needed.

What the error means for application code

A serialization failure is not usually evidence of corrupt data or a broken query. It means the database protected the serializable guarantee by rejecting one valid attempt. Code using serializable transactions should treat this error as retryable, with a bounded retry policy and suitable backoff.

The important distinction is that snapshot isolation answers, “What committed state should this transaction read?” Serializable snapshot isolation adds, “Can all of these snapshot-based decisions coexist in a serial order?” If the answer is no, one transaction must be rolled back before its result becomes visible.

← All articles