Dirty read
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.

    Dirty read: the 60-second story

    Dirty read

    Your ₹500 autopay failed. Your wallet held ₹1,000 all along.

    The autopay job read a tatkal purchase’s ₹800 debit before it committed. The purchase rolled back; the fee and SMS didn’t.

    A dirty read is reading a draft over someone’s shoulder

    Draft = uncommitted write, send = COMMIT, delete = ROLLBACK. Only a sent message is a promise.

    Safe if the writer commits?

    Bad: Only rollbacks hurt

    The writer committed, so whatever you read became true.

    Good: You saw the middle

    Savings debited, wallet not yet credited: you sum ₹3,000, a total no commit ever held.

    Two ways to keep an in-flight debit out of sight

    Locking makes the reader wait for the writer to end. MVCC hands it the last committed version at once.

    PostgreSQL accepts READ UNCOMMITTED, and SHOW transaction_isolation even prints it.

    The trap

    You get READ COMMITTED. Under MVCC, readers never wait for writers, so dirty reads buy nothing. MySQL and SQL Server really read dirty.

    SQL Server reports queue behind writers

    Bad: Add WITH (NOLOCK)

    No waiting, but never-committed values, and rows read twice or skipped.

    Good: Turn on RCSI

    Readers get committed versions without shared locks. You pay for a version store.

    A rider is assigned to an order that never existed

    The order service published OrderPlaced before COMMIT, then rolled back. Isolation levels describe one database; events and cache writes leak around them.

    The fix: nothing leaves before COMMIT

    1. Write the event as an outbox row, same transaction
    2. A relay publishes committed rows; consumers must be idempotent
    3. Commit first, then delete the cache key

    Remember

    A dirty read acts on uncommitted work. Run at READ COMMITTED or higher, clear caches after COMMIT, and publish events through an outbox.