LESSON 06 · Serializable isolation

Choose how to serialize

Serializable isolation makes concurrent transactions behave as if they ran one at a time, and there are three main ways to build it.

SYSTEM VIEW

The interleaving

Diagram view
Method—
Bookings—
Aborts—

Watch two buyers race for the last of 100 places three ways: one at a time, under locks, and on snapshots that abort a conflict.

  1. Serializable isolation promises an outcome some one-at-a-time order could give. Replay the last of 100 places, 99 sold, under actual serial execution: one thread runs each buyer’s script in turn, so A’s counts 99 and books while B’s waits.

    Serializable means
    a one-at-a-time outcome
    Starts with
    99 of 100 sold
    One thread runs
    one script at a time

    What changed

    • Both send a script
    • B’s script waits
    • A books: 99 → 100
  2. Only when A’s script ends does B’s run. It counts 100 and is refused, so the section never oversells. B waited out all of A’s script, so one slow script stalls everything queued behind it.

    Scripts stay
    short, as in Redis
    A slow script
    stalls the queue

    What changed

    • B’s wait
    • B’s script counts 100
    • B refused: sold out
  3. Replay with two-phase locking. Readers take shared locks and writers exclusive ones, held until the transaction ends. Each buyer’s count takes a shared lock on section 5’s index range, which keeps out phantoms, and sees 99.

    Readers take
    shared locks
    Writers need
    exclusive locks

    What changed

    • A’s shared lock
    • B’s shared lock
    • Both count 99
  4. Both buyers now insert a booking. An insert into the range needs an exclusive lock, which the other buyer’s shared lock blocks. A waits for B and B waits for A, so neither can go on: a deadlock.

    What changed

    • A waits for B’s lock
    • B waits for A’s lock
    • Deadlock
  5. The database detects the deadlock and aborts B, which frees its lock. A’s insert goes through and A commits: 100 booked. B retries from the start, counts 100 and is refused. Shared locks block writers, so one long report can stall them.

    Serializable in
    InnoDB, SQL Server
    A long report
    stalls writers

    What changed

    • B aborted, its lock freed
    • A’s insert goes through
    • A commits: 99 → 100
    • B retries: sold out
  6. Replay with serializable snapshot isolation. Each buyer reads from its own snapshot and nothing blocks: both count 99 and insert at once. The database only notes what each one read.

    What changed

    • A’s snapshot
    • B’s snapshot
    • Both count 99 and insert
  7. Both commit, and A’s goes through: 100 booked. Each buyer read the range the other wrote into, so no one-at-a-time order explains both, and the database aborts B with SQLSTATE 40001. Under heavy contention, such aborts waste work.

    Postgres
    SERIALIZABLE is SSI
    Abort code
    SQLSTATE 40001

    What changed

    • A commits: 99 → 100
    • B aborted: 40001
  8. 2PL and SSI both need a retry loop around the whole transaction: B starts again on a new snapshot, counts 100 and is refused. Defaults and names differ by engine, and Oracle’s “serializable” is snapshot isolation, so test the anomaly, not the label.

    Postgres, Oracle default
    read committed
    InnoDB default
    repeatable read
    Oracle serializable
    allows write skew

    What changed

    • B starts again
    • B refused: sold out

Space plays · ← → step · Home resets

THE IDEA

How it works

Serializable isolation promises an outcome matching some one-at-a-time order, which removes every anomaly in this chapter. Replay the last of 100 places. Actual serial execution runs transactions one after another, on one thread per partition. The first buyer finishes, and the second counts 100 and is refused. Transactions must be short, often scripts as in Redis, and a slow one stalls everything behind it.

Two-phase locking (2PL) makes readers take shared locks and writers exclusive ones, held until the transaction ends, plus index-range locks to block phantoms. Both buyers lock the range, both try to insert, and the database detects the deadlock and aborts one, which retries and sees 100. One long report can stall writers. MySQL InnoDB and SQL Server use this for their serializable levels.

Serializable snapshot isolation (SSI) runs both on snapshots without blocking, tracks who read what the other wrote, and aborts one when the pattern might not match any serial order. Postgres SERIALIZABLE works this way, reporting SQLSTATE 40001. Heavy contention makes retries waste work. Both 2PL and SSI need a retry loop around the whole transaction.

Postgres and Oracle default to read committed, and MySQL InnoDB to repeatable read. Postgres’s repeatable read is snapshot isolation. InnoDB’s uses a snapshot for plain reads but the latest committed row for updates and locking reads. Oracle’s “serializable” is really snapshot isolation and allows write skew, so test the anomaly you care about. A sensible path: keep the default, guard known risks with atomic updates and constraints, and move cross-row rules to serializable once retries exist.

DECISION POINT

Your Postgres app has a dozen rules spanning rows: spending limits, seat allotments, quotas. Each has its own lock or counter table, and two recent bugs came from a code path that forgot a lock. Would you change the approach, and what would you check first?

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

Keep in mind

  • Serial execution avoids conflicts, 2PL blocks on them, and SSI detects them and aborts.
  • Isolation level names differ across engines, so test the anomaly instead of trusting the label.
  • Approaches that abort need the application to retry the whole transaction.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
ApplyQUESTION 01

A Postgres app at read committed has a write-skew bug. The team switches to REPEATABLE READ, reasoning that it is stricter. What should they expect?

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

Design ticketing for concert seats

Sell every seat exactly once through a huge on-sale spike, with temporary holds and payments that can fail or go silent.

The brief

Fans open a seat map for one concert, choose up to 4 specific seats, hold them for 10 minutes, and pay. Holds can expire and payments can fail, while most of the demand arrives in the first minute. Design the first version for one concert at a time. Keep resale, dynamic pricing, and bot detection outside this exercise.

  • One concert has 50,000 assigned seats, in 100 sections of 500. An order holds 1 to 4 seats and averages 2.5. Use decimal units.
  • 500,000 fans are online when the sale opens. Each polls the seat map every 5 seconds and makes one hold attempt on average during the first 60 seconds.
  • A hold lasts 10 minutes. Assume 80% of holds become paid orders, and the other 20% expire or fail payment and return their seats to sale.
  • A charge takes about 3 seconds, and the provider can time out with no clear answer. It accepts idempotency keys. Seat and order rows average 200 bytes.
  • A seat must not be sold to two buyers, and nobody should be charged without a ticket. The seat map may be a few seconds stale, but the decision to grant a hold may not.

Reading a design isn’t the same as producing one under a clock. Try it timed: the requirement change stays hidden until partway through.

Loading your draft…

State your scope. Estimate peak seat-map reads, hold attempts, and seat checks per second, how many holds can succeed, charges in flight, and storage. Keep units visible.

0 / 4,000 characters

Name each component and what it is the authority for. Trace a hold, a successful payment, and a hold that expires. Say exactly what stops two buyers from holding one seat, and which isolation level or guard you rely on.

0 / 4,000 characters

A charge times out with no clear answer. Separately, a server crashes after a charge succeeds and before the seats are marked sold. Explain both outcomes, then name the bottleneck you expect during the spike and the metric that would show it.

0 / 4,000 characters

Respond to the new seating rule below. Explain whether your per-seat guard catches it, what you would change, and what the change costs at peak.

The requirement changesThe venue adds a rule: a hold must not leave a single empty seat stranded between two taken seats in the same row. Two buyers claiming different seats at the same moment could strand one together, though neither would alone. How does your design enforce this during the spike, and what does it cost?

0 / 4,000 characters

Write an initial response in all four sections to unlock the checklist and two worked approaches.

0 of 3 checkpoints answered correctly to complete this lesson.