Stage 2 · Building blocks · Medium · 20 min

Transactions and isolation levels

Group reads and writes so they succeed or fail together. Then choose how much transactions running at the same time see of each other.

What each interview level is expected to know

Learn first: Choosing a database and modelling data

After this lesson you can:

  • Explain what it means for a group of changes to succeed or fail together.
  • Name the anomalies: lost update, dirty read, read skew and write skew.
  • Pick an isolation level and say which anomalies it still allows.

Start here

See it in 60 seconds

Two doctors leave at once

Two transactions each check a rule and act on it at once; only the strongest isolation level sees that together they break it.

Scene 1 of 5 · Two on call

Alice and Bob are on call, and Cara is off; at least one doctor must always stay on call.

Alice's appAlice's appBob's appBob's appRotaRota
  • Alice's app — healthy
  • Bob's app — healthy
  • Rota — healthy
The rota (rule: at least one doctor on call)
DoctorStatus
Alice (highlighted)on call
Bob (highlighted)on call
Caraoff
  1. Two on call. Alice and Bob are on call, and Cara is off; at least one doctor must always stay on call.
  2. Alice checks. Alice's app reads the rota: two doctors are on call, so she may leave.
  3. Bob checks too. At the same moment, Bob's app reads the same rota and sees the same two.
  4. Both leave. Each writes only their own row, so the two writes never touch the same row, and both commit.
  5. Nobody on call. Under read committed and under snapshot alike, 0 doctors are now on call: this is write skew.
  6. Now you try. Pick the isolation level, and run the same two transactions again. For example: 1 doctor on call, and 1 transaction aborted. At commit, Bob's read of Alice's row is out of date, so his transaction aborts and he stays on call.

Scene 1 of 5: Two on call. Alice and Bob are on call, and Cara is off; at least one doctor must always stay on call.

Key points

  1. Each transaction checked the rule, and each was right on its own.
  2. They changed different rows, so no write ever clashed.
  3. Read committed and snapshot both let this write skew through.
  4. Serializable aborts one of them, and the app retries it.

Go deeper in the Transactions & isolation levels lab →

In plain words

Say you move 100 from account A to account B. That is two writes: take 100 from A, then add 100 to B. If the app crashes between them, the money is gone. A groups the two writes, so both happen or neither does. Other transactions run at the same time. The isolation level decides what each one sees of the others. Weaker levels are faster but let some mistakes through. Stronger levels stop more mistakes, and cost more waiting or retries.

Inside the database, the transfer runs in three steps. The transaction starts, takes 100 from A and adds 100 to B. Under most isolation levels, other transactions cannot see either change yet. Then it , and both changes count at once. If anything fails before the commit, the database rolls both changes back, so A and B are as they were.

The problem it solves

Two transactions that each look correct can still give a wrong result when they overlap. One can overwrite the other's change, read a value that is later undone, or both act on a fact that stops being true.

Why it matters

Two people like the same post at the same moment, and the count goes up by one, not two. Bugs like this pass every test and only appear under real traffic.

A worked example

The post likes:42 has 5 likes. Two people like it at once: both transactions read 5 and both write 6, so the count ends at 6, not 7. Writing likes = likes + 1 in one step keeps both likes. A stronger level makes one transaction fail, and the app runs it again.

Think of it like…

Think of two cashiers selling the last concert ticket from one paper list. Each looks at the list, sees one ticket left, and sells it. The ticket is sold twice.

Where this analogy stops working

Two people acting on the same old fact is the useful part. In a shop, the fix is to hold the list while you sell. Many databases do not make readers wait like that. They give each transaction its own picture and stop one of them at the end. The app must then run it again.

Try it

See it working

Live simulationOpen the full lab →

Lab · Building blocks

Transactions & isolation levels

Up to three transactions run at the same time against a few rows. Pick an isolation level and watch which mistakes it lets through: lost updates, dirty reads, wrong totals and write skew. Serializable stops them all, and its price is aborted transactions.

Try it

Lab time 3.0 s
Starting…Starting…T3, Read committedT3Read committedstep 1 of 3Not used, IDLENot usedIDLElikes:42, = 1likes:42= 11 version
  • Starting… — healthy
  • T3 — Read committed — healthy — step 1 of 3
  • Not used — IDLE
  • likes:42 — = 1 — healthy — 1 version
  • Read
  • Write
  • Commit
  • Abort
Committed
2
Aborted
0
Lost updates
1
Problem:
Committed likes
2
stored: 1

Numbers add up:3 started = 2 committed + 0 aborted + 1 runningThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

What the transactions do
Workload
Isolation
Isolation level

A read sees the newest committed value at the moment it reads.

2

With 1, transactions never overlap.

Rows

RowCommitted valueVersions keptLocked by
likes:4211—

Versions kept: older values a running snapshot may still need. A locked row has a write that is not committed yet.

What just happened

  1. 2.5sError: T1 overwrote 1 like it never saw: a lost update.
  2. 0.0sInfo: Two people like a post, at read committed.
What this model simplifies
  • Time is slowed so you can watch each step. Real transactions like these take a few milliseconds.
  • Every write locks its row until the transaction ends, at every level, as most databases do.
  • Serializable here uses snapshot reads plus a check at commit: it aborts a writer if anything it read changed meanwhile. Real databases use finer checks, or locks, and abort less often.
  • Aborted transactions are not retried here. A real app retries them, which costs more time.
  • A transfer moves money with two atomic updates (balance = balance − 100), so transfers alone never lose money.

Mechanics

How it works

A transaction makes a group of writes happen together or not at all. Its isolation level decides what happens when two transactions overlap. The pictures show one of each: a transfer that crashes halfway, and two orders that sell at the same moment.

  1. Begin

    A transaction starts. Every read and write after that belongs to it, until it commits or aborts. Under snapshot isolation it also fixes the picture of the data it will read.

  2. Read and write

    In most databases, each write locks its row until the transaction ends. So two transactions cannot change one row at the same moment. What a read sees depends on the isolation level. Read committed sees every change committed so far, and snapshot isolation keeps the picture from the start.

  3. Commit or abort

    Pick a case to follow below

    With this case: With no transaction, A lost 100 and B never got it. 100 is gone.

    App, moves 100 from A to BAppmoves 100 from A to BDatabase, accounts A and B, NOWDatabaseaccounts A and BA = 400NOW
    • App — moves 100 from A to B — healthy
    • Database — accounts A and B — healthy — NOW — A = 400
    • Write
    Step 1 of 2

    Step 1 of 2 · Database

    Done: Take 100 from A

    What happens
    The first write sets A to 400.
    Why
    With no transaction, this write is saved for good at once.
    What changes
    A has 400. B still has 500.
    Next
    The app crashes before the second write
    Account A
    400
    Account B
    500
    A and B together
    900
    Watch: 100 is missing

    Step 1 of 2: Database. Take 100 from A. The first write sets A to 400.

    Commit makes every write visible at once, so no other transaction sees half of the work. Abort undoes all of the writes, as if the transaction never ran. In the picture, the app moves 100 from A to B and crashes halfway.

    With no transaction, each write stands alone. The first one took 100 from A, the crash stopped the second, and the 100 is gone. In a transaction, the database rolls back the open work when the connection drops, so A and B keep 500 each.

  4. Check for conflicts

    Pick a case to follow below

    With this case: Under read committed, both orders wrote 9. Two items sold, but the stock shows 9: a lost update.

    Order 1, sells one itemOrder 1sells one itemread 10Order 2, sells one itemOrder 2sells one itemread 10Database, stock starts at 10, NOWDatabasestock starts at 10NOW
    • Order 1 — sells one item — healthy — read 10
    • Order 2 — sells one item — healthy — read 10
    • Database — stock starts at 10 — healthy — NOW
    • Read
    • Commit
    Step 1 of 3

    Step 1 of 3 · Database

    Done: Both orders read the stock: 10

    What happens
    Order 1 and order 2 each start a transaction and read the stock at the same moment.
    Why
    A plain read takes no lock, so both reads go ahead.
    What changes
    Each plans to write 10 − 1 = 9.
    Next
    Order 1 writes 9 and commits
    Stock in the database
    10
    Items sold
    0
    Stock it should show
    10
    matches

    Step 1 of 3: Database. Both orders read the stock: 10. Order 1 and order 2 each start a transaction and read the stock at the same moment.

    What these pictures simplify. These pictures run two transactions in a stated order, as PostgreSQL behaves at each level. Other databases differ: in MySQL, repeatable read does not stop this lost update. Real transactions overlap in many more ways. The isolation lab above shows more of them.

    Two transactions can each be correct alone and still clash. In the picture, two orders each sell one item at the same moment. Each reads the stock, then writes it minus one. Switch between three ways to run them.

    Under read committed, the default in , both orders read 10 and both write 9. Two items sold, but the stock shows 9. This is a lost update.

    SELECT … FOR UPDATE locks the row as it is read, so the second order waits and then reads 9. At repeatable read, PostgreSQL aborts the second order instead, and the app must run it again. Either way the stock ends at 8.

    Stronger levels stop more of these mistakes, at the cost of waiting or retries. Serializable, the strongest level, gives the same result as running the transactions one at a time. The rules differ between databases: in MySQL, repeatable read does not stop this lost update.

Two orders, one stock row
WayWhat the second order doesStock after two sales
Read committedWrites over the first order's change9, which is wrong
SELECT … FOR UPDATEWaits for the lock, then reads 98
Repeatable read in PostgreSQLIs aborted, and the app runs it again8

Use it when

  • Any change that must update two rows together, such as moving money between accounts.
  • Counters and stock levels that many users change at the same moment.
  • Rules that span rows, such as "at least one doctor stays on call".

Skip it when

  • Huge batch jobs that would hold locks for minutes: split them into smaller transactions.
  • Data spread over several services: use a saga or an outbox instead (see Distributed transactions).

Check yourself

Which property means a transaction happens completely or not at all?

Judgement

Trade-offs and how it fails

What you give up

Safety
Stronger levels stop more of these mistakes. Serializable stops them all: the result is the same as running the transactions one at a time.
Speed
Stronger levels make transactions wait for locks or abort and retry more often.

Failure modes

  • Lost update: two users read likes:42 = 5, both write 6, and one like disappears.
  • Dirty read: a report reads a balance from a transfer that is then .
  • Read skew: a report reads account A before a transfer and account B after it, so its total is wrong.
  • Write skew: two doctors each see the other on call, both leave, and nobody is left.

In the real world

  • PostgreSQL defaults to read committed, and its repeatable read level is snapshot isolation.
  • PostgreSQL's repeatable read aborts the second writer of a row. MySQL InnoDB's does not, so a read-then-write there can still lose an update.
  • Booking systems use serializable or row locks so two people cannot book the same seat.

Check yourself

Why not run every transaction at the strictest level?

Interview

What interviewers look for

  • Name the anomaly before the fix: say "this is a lost update", then offer an atomic update or a lock.
  • Say which level your database uses by default; read committed is common, and it does not stop write skew.

Say it out loud: Two transactions each add 1 to likes:42, which is 5. Under read committed, what can the final value be?

Which anomaly does snapshot isolation still allow?

Expected depth by interview level

Levels describe the depth interviewers probe, not years of experience. Choose a level on the learning path to highlight it.

JuniorUseful
What a transaction is: all of its writes happen, or none do.
MidCore
Read committed against snapshot, and the lost update with its fixes.
SeniorCore
Write skew, serializable, and choosing locks, atomic updates or retries.
StaffExpected
Picking isolation per workload, and what it costs in aborts and throughput.

Practice

Apply it

Three harder questions

The first two use the idea on a new case. The last asks you to judge a design.

Apply it

Two requests read likes:42 = 5 at the same moment, and each writes 6. What is the value afterwards, and what avoids the problem?

Apply it

Under read committed, a report reads account A a moment before a transfer of 100 from A to B commits. It reads B a moment after. What total does it see?

Judge a design

At least one doctor must be on call. Two doctors each check "is someone else on call?" under snapshot isolation. Both see yes, and both go off call. Which fix works?

Next: storing large files, which do not fit in database rows.

Go deeper

Sources and further reading

First-hand sources to check this page against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.

  • Official docsTransaction IsolationPostgreSQL documentationWhat each isolation level allows in PostgreSQL, which can differ from the SQL standard.
  • Research paperA Critique of ANSI SQL Isolation Levels (1995)Berenson et al., ACM SIGMODShows the standard’s level names miss real problems such as write skew, and defines snapshot isolation.
  • Engineering guideConsistency ModelsJepsenA map of isolation and consistency levels, from weakest to strongest, with a page for each.