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 app — healthy
- Bob's app — healthy
- Rota — healthy
| Doctor | Status |
|---|---|
| Alice (highlighted) | on call |
| Bob (highlighted) | on call |
| Cara | off |
- Two on call. Alice and Bob are on call, and Cara is off; at least one doctor must always stay on call.
- Alice checks. Alice's app reads the rota: two doctors are on call, so she may leave.
- Bob checks too. At the same moment, Bob's app reads the same rota and sees the same two.
- Both leave. Each writes only their own row, so the two writes never touch the same row, and both commit.
- Nobody on call. Under read committed and under snapshot alike, 0 doctors are now on call: this is write skew.
- 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
- Each transaction checked the rule, and each was right on its own.
- They changed different rows, so no write ever clashed.
- Read committed and snapshot both let this write skew through.
- Serializable aborts one of them, and the app retries it.
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 (A group of database changes that succeed or fail together: either all of them are saved, or none are.) 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 (Making a change final. A committed database write counts and will not be undone. In Git, a commit is one saved set of code changes.), 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
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.
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.
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.
Commit or abort
Pick a case to follow belowWith this case: With no transaction, A lost 100 and B never got it. 100 is gone.
App, moves 100 from A to B App moves 100 from A to B Database, accounts A and B, NOW Database accounts A and B A = 400 NOW - App — moves 100 from A to B — healthy
- Database — accounts A and B — healthy — NOW — A = 400
- Write
Step 1 of 2Step 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 A400Account B500A and B together900Watch: 100 is missingStep 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.
Check for conflicts
Pick a case to follow belowWith this case: Under read committed, both orders wrote 9. Two items sold, but the stock shows 9: a lost update.
Order 1, sells one item Order 1 sells one item read 10 Order 2, sells one item Order 2 sells one item read 10 Database, stock starts at 10, NOW Database stock starts at 10 NOW - 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 3Step 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 database10Items sold0Stock it should show10matchesStep 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 (A widely used open-source SQL database.), 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.
| Way | What the second order does | Stock after two sales |
|---|---|---|
| Read committed | Writes over the first order's change | 9, which is wrong |
| SELECT … FOR UPDATE | Waits for the lock, then reads 9 | 8 |
| Repeatable read in PostgreSQL | Is aborted, and the app runs it again | 8 |
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
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 (Undoing a change to return to the last good state. A release rollback puts the previous version back. A transaction rollback cancels its writes.).
- 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
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?
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
Apply it
Judge a design
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.