agentleFS
auralshinauralshin/coding-skills/skills/isolation-anomalies/SKILL.md

isolation-anomalies

Skill0 starsChanged 3 months agoMITView on GitHub

This skill should be used when the user asks "which isolation level do I need", "why did concurrent requests break this invariant", "should I use SELECT FOR UPDATE", "what is write skew", or reports a bug that only reproduces under concurrent traffic, a lost update from a read-modify-write, or confusion about REPEATABLE READ behavior across databases.

The file

We split this skill into the rules it gives an agent

6 rules. About 0 are common, 6 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈6

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Never reason from the label.

    line 17
  2. Distinctive:

    Write skew is possible whenever a transaction:

    line 53
  3. Distinctive:

    Treat any read-check-write shape under snapshot isolation as broken until fixed.

    line 59
  4. Distinctive:

    If the invariant fits a constraint, stop here.

    line 73
  5. Distinctive:

    Read-modify-write in application code is the lost-update anomaly.

    line 77
  6. Distinctive:

    Every SERIALIZABLE transaction requires a retry loop:

    line 104

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Transaction isolation anomalies

Isolation levels are commonly taught as a ladder of "how much locking". That framing causes real bugs. Levels are defined by which anomalies they permit, and two databases can use the same label for different guarantees.

LabelActual guarantee
Postgres REPEATABLE READSnapshot isolation
MySQL InnoDB REPEATABLE READSnapshot reads plus next-key locks on writes, a different anomaly set
Oracle SERIALIZABLESnapshot isolation, not serializable
Postgres SERIALIZABLEGenuinely serializable (SSI), aborting transactions to stay so

Never reason from the label. Reason from the anomaly table.

Anomaly table

AnomalyREAD COMMITTEDSNAPSHOT / PG REPEATABLE READSERIALIZABLE
Dirty readpreventedpreventedprevented
Non-repeatable readpossiblepreventedprevented
Phantom readpossiblepreventedprevented
Lost updatepossibledetected, abortsprevented
Write skewpossiblepossibleprevented

Snapshot isolation prevents everything commonly known about and still permits write skew. That is the anomaly that survives intuition.

Write skew

Invariant: at least one doctor on call. Both transactions start with 2 on call and run concurrently.

-- transaction A
BEGIN;
SELECT count(*) FROM doctors WHERE on_call AND shift_id = 42;  -- 2, safe to leave
UPDATE doctors SET on_call = false WHERE id = 1;
COMMIT;

-- transaction B, concurrently
BEGIN;
SELECT count(*) FROM doctors WHERE on_call AND shift_id = 42;  -- also 2
UPDATE doctors SET on_call = false WHERE id = 2;
COMMIT;

Both commit. Zero doctors on call. Neither wrote the row the other read, so snapshot isolation sees no write-write conflict.

Detection recipe

Write skew is possible whenever a transaction:

  1. Reads a set of rows,
  2. Decides based on what it read,
  3. Writes something that would have changed that decision had it been visible.

Treat any read-check-write shape under snapshot isolation as broken until fixed. Common instances: booking without overlap, balance checks across accounts, username availability without a unique index, inventory across warehouses, "only one primary" flags, quota enforcement, seat allocation.

Fixes in order of preference

1. Let a constraint enforce it

Cheapest and most reliable, and independent of isolation level or application code.

CREATE UNIQUE INDEX ON users (lower(email));

ALTER TABLE bookings ADD EXCLUDE USING gist (room_id WITH =, during WITH &&);

If the invariant fits a constraint, stop here.

2. Make the write atomic

Read-modify-write in application code is the lost-update anomaly. Push arithmetic into the statement and check the row count:

UPDATE accounts SET balance = balance - 10 WHERE id = 1 AND balance >= 10;
-- 0 rows means insufficient funds

3. Materialize the conflict

Lock the rows read, so the conflict becomes visible:

SELECT count(*) FROM doctors WHERE on_call AND shift_id = 42 FOR UPDATE;

Locking a single parent row that represents the invariant is often simpler and faster than locking a set. Take locks in a consistent order across the codebase, or write skew is traded for deadlocks.

4. SERIALIZABLE

In Postgres this is Serializable Snapshot Isolation: optimistic, no extra locks, aborting transactions that would violate serializability.

BEGIN ISOLATION LEVEL SERIALIZABLE;
-- ...
COMMIT;   -- may raise SQLSTATE 40001

Every SERIALIZABLE transaction requires a retry loop:

for attempt in range(3):
    try:
        with conn.transaction():
            do_work()
        break
    except SerializationFailure:          # 40001, also 40P01 deadlock_detected
        if attempt == 2: raise
        sleep(random.uniform(0, 0.05 * 2**attempt))

The transaction body must be retry-safe, with no side effects outside the database until after commit. This is why SERIALIZABLE fails in practice: not throughput, but a retried transaction that sends the email twice.

Read Committed pitfalls

Most systems default here, so these appear constantly:

  • Two reads in one transaction can disagree. A report assembled from several queries can be internally inconsistent.
  • UPDATE re-reads. In Postgres a blocked UPDATE re-evaluates its WHERE against the newly committed row after the lock releases. The predicate may now be false, so check the row count.
  • SELECT then UPDATE in application code is always a lost-update risk. Lock, or make the update conditional and check the row count.

Optimistic concurrency

For long user-facing edits, hold a version column instead of a lock:

UPDATE docs SET body = $1, version = version + 1
WHERE id = $2 AND version = $3;
-- 0 rows means a concurrent edit

Guidance

  • Default to READ COMMITTED with constraints and atomic or conditional updates. This covers most applications.
  • Use SERIALIZABLE for genuinely multi-row invariants such as accounting, scheduling, and quota, accepting the retry loop.
  • Write the concurrency test. These bugs never appear in single-threaded tests. Use two threads with a barrier so both reach the read before either writes, then assert the invariant. That test is the only proof a fix works.
  • Name the anomaly in review. "This is write skew under snapshot isolation" ends the discussion.

Reference: Berenson et al., A Critique of ANSI SQL Isolation Levels (1995), which named snapshot isolation and write skew.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.