LESSON 02 · Read committed

Hide unfinished work

Read committed, a common default, stops transactions from seeing or overwriting each other’s unfinished writes.

SYSTEM VIEW

The interleaving

Diagram view
Ana committed—
T2 sees—

Watch Ana’s row while a transfer is half done, first with no isolation, then at read committed.

  1. With no isolation, T1 debits Ana by writing 150 and hasn’t committed yet. The report, T2, reads that 150 and adds Ben’s $100: $250, not $300, so $50 looks missing. That is a dirty read.

    T2’s total
    $250
    Should be
    $300

    What changed

    • T1 writes 150, uncommitted
    • T2 reads 150
  2. T1 aborts, and Ana’s row goes back to 200. The committed balance was 200 all along, so the report has acted on 150, a value that never officially existed.

    What changed

    • T1 aborts
    • Ana back to 200
    • The 150 T2 read
  3. Replay T1’s debit, still with no isolation. It writes 150 and hasn’t committed when T3, a support tool, sets Ana’s balance to 500 and commits. T3 has overwritten another transaction’s uncommitted write: a dirty write.

    What changed

    • T1 writes 150, uncommitted
    • T3 writes 500 over it
    • T3 commits 500
  4. T1 aborts, and rollback restores the 200 its write replaced. T3’s 500 is gone even though it committed. Rollback only works when one transaction at a time owns a row’s uncommitted value.

    T3 committed
    $500
    Ana now
    $200

    What changed

    • T1 aborts
    • Ana back to 200
    • T3’s committed 500
  5. Replay at read committed. T1 writes 150 and takes the row lock until it commits or aborts. In a multi-version engine such as Postgres, the row keeps the last committed value, 200, beside the new one, so T2’s read returns 200 at once.

    Row lock
    T1, until commit
    T2 reads
    200, no wait

    What changed

    • T1 holds the row lock
    • Row keeps 200 beside 150
    • T2 reads 200
  6. T3 tries to subtract 20 from Ana’s balance. Its update needs the row lock T1 holds, so it waits while T1 goes on to credit Ben. Readers don’t wait for the lock; writers queue on it.

    What changed

    • T3 waits for the lock
    • T1 credits Ben $50
  7. T1 commits 150 and releases the lock. T2’s next read returns 150, the new committed value, and T3’s update resumes against 150 and commits 130.

    What changed

    • T1 commits 150
    • T2 reads 150
    • T3 commits 150 − 20 = 130
  8. Inside one transaction, T2 read 200 and then 150: both committed, at different moments. Read committed protects each read, not a sequence of them. It’s cheap, and Postgres and Oracle both default to it.

    First read
    $200
    Next read
    $150

    What changed

    • T2 saw two values

Space plays · ← → step · Home resets

THE IDEA

How it works

Without isolation, two specific things go wrong. A dirty read is seeing another transaction’s uncommitted write. A report that runs while Ana’s transfer is half done sees her debit but not Ben’s credit, so $50 looks missing. If the transfer then aborts, the report has acted on a value that never officially existed.

A dirty write is overwriting another transaction’s uncommitted write. Suppose T1 lowers Ana’s balance from 200 to 150 without committing, and a support tool then sets it to 500. If T1 aborts and restores 200, the support tool’s write is gone even though it committed. Rollback only works when one transaction at a time owns a row’s uncommitted value.

Read committed isolation forbids both. A transaction takes a row lock when it first updates a row and holds it until it commits or aborts, so a second writer waits. For reads, multi-version engines such as Postgres and Oracle keep the last committed value alongside the new one and return that, so readers do not wait. Follow one row. T1 debits Ana, writing 150 uncommitted. T2 reads Ana and immediately gets 200. T3 tries to subtract 20 and blocks on T1’s lock. T1 commits. T2’s next read returns 150, and T3 resumes against the new value, leaving 130.

This level is cheap, and Postgres and Oracle both default to it, but it protects each read rather than a sequence of them. Two reads in one transaction can return different committed values, and writers can still overwrite updates computed from an earlier read.

DECISION POINT

Checkout marks an order paid and inserts a shipment row in one transaction. A warehouse dashboard, polling during checkout, sometimes shows paid orders with no shipment. The database runs at read committed. A teammate blames dirty reads. Is that right, and what would you check?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Dirty reads see unfinished work; dirty writes overwrite it.
  • Row locks hold back writers, while old committed versions keep readers moving.
  • Read committed protects each read, not a sequence of reads.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
ApplyQUESTION 01

In Postgres at read committed, T1 has updated a row but not committed. T2 selects that row, and T3 updates it. What happens?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.