Isolation levels
0-day streakReview

Your progress

Level 1

0 XP100 XP to level 2
0day streak
0best streak
0topics started
0cards reviewed
    Review cards

    Progress is stored only in this browser. No account is required.

    Transaction isolation levels: the 60-second story

    Transaction isolation levels

    Your report says ₹1,200. Only ₹1,000 exists.

    No row was written wrongly. The report read savings before a ₹200 transfer and wallet after it.

    Four SQL levels, each blocking one more anomaly

    SERIALIZABLE means the result equals some one-at-a-time order. It costs waiting or aborts, so most databases default lower.

    Same level name, different promise

    Bad: MySQL REPEATABLE READ

    Two ₹200 top-ups both read ₹500 and write ₹700. ₹200 vanishes.

    Good: PostgreSQL REPEATABLE READ

    The second writer fails with 40001. Retry it; nothing is lost.

    How snapshot isolation works

    1. Every read sees data as of your start
    2. Readers and writers never block each other
    3. Two writers of one row: first committer wins
    4. Old row versions are kept to serve it

    Both doctors sign off. Nobody is on call.

    Each snapshot shows the other on call, and each updates a different row. No write conflict, so snapshot isolation commits both.

    Under SQL-92’s strict wording, snapshot isolation shows no dirty read, fuzzy read or phantom. It passes as SERIALIZABLE.

    The catch

    Yet it allows write skew. Oracle’s SERIALIZABLE and PostgreSQL’s REPEATABLE READ are both snapshot isolation.

    Start from the anomaly, then pick the cheapest fix

    Most races are fixed in the write, not by raising the level.

    40001

    the error a stronger level hands you

    PostgreSQL and CockroachDB abort conflicting transactions with it. Retry the whole transaction, reads included, and keep emails and payments outside the retry.

    Remember

    Name the anomaly, not the level. Atomic writes for one row, one snapshot for reports, SERIALIZABLE plus retries across rows.