RC vs RR
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.

    Read committed vs repeatable read: the 60-second story

    Read committed vs repeatable read

    Withdraw ₹5,000 if the balance is still over ₹20,000. What happens?

    Money moved since you last looked. One level re-checks, one says start again, one withdraws anyway.

    One snapshot per statement, or one per transaction

    Read committed sees each new commit at its next statement. Repeatable read keeps the snapshot from its first statement, not from BEGIN.

    Same name, same level?

    Bad: RR is RR

    PostgreSQL’s aborts a conflicting write with 40001. MySQL’s writes over the newer row.

    Good: Compare the writes

    Ask what a write does when it meets a newer row.

    Read committed in PostgreSQL

    1. Your UPDATE meets a row someone is changing
    2. Wait for them to commit or roll back
    3. Re-check your WHERE on the new version
    4. Still matches, apply. No longer matches, skip silently.

    Rows hold 9 and 10. A adds 1 to each; B deletes the 10s and waits.

    The trap

    B re-checks only the row it waited for, now 11. DELETE 0, yet a row now holds 10.

    MySQL repeatable read runs two kinds of read

    Plain SELECTs read the snapshot. UPDATE reads and locks the latest rows, including order 150 that another session committed later.

    Read committed for writes, repeatable read for reports

    Guard the write in one UPDATE and check rows affected. Give multi-query reports one read-only snapshot, ideally on a replica.

    Migrating MySQL to PostgreSQL? Check three habits.

    1. Multi-query reads lose their stable snapshot
    2. Gap locks no longer keep competing inserts out
    3. Its repeatable read throws 40001, so add retries

    Remember

    A snapshot per statement, or one per transaction. The real difference is what a write does when it meets a newer row.