Skip to content

Concurrency Problems and Isolation Levels (Lost Update, Dirty Read, Phantom Read) ​

This page covers the 5 problems that arise when several people read and modify the same data at the same time, and the transaction isolation levels databases use to prevent them.

Why Concurrency Problems Happen ​

A transaction is "a bundle of work that must be processed all at once." For a bank transfer, "withdraw from account A + deposit into account B" is one transaction. Finalizing the work is called a commit, and undoing it as if it never happened is called a rollback.

A DB used by a single person has no problem, but in a real service many transactions touch the same data at the same time. If they aren't properly kept apart from each other (isolation), the data gets tangled. Picture two people editing a shared document at the same time and one person's changes getting lost.

5 Concurrency Problems ​

1. Lost Update ​

A transaction that comes later overwrites the changes of a transaction that started earlier, so one side's change disappears.

Balance 10,000 won
A: changes 10,000 → 15,000 (not committed yet)
B: changes 10,000 → 20,000 and commits
A: commits
→ one side's change is overwritten and vanishes without a trace

2. Dirty Read ​

Another transaction reads intermediate data that hasn't been committed yet. If the first transaction rolls back, the reader has worked with a value that never existed. It is also called a "current-state error."

A: changes the product price 5,000 → 3,000 (before commit)
B: reads 3,000 won and uses it in a calculation
A: something goes wrong, rolls back → 5,000 won again
→ B calculated with a 3,000 won price that never existed

3. Non-Repeatable Read ​

Within one transaction, reading the same data twice gives different results. This is because another transaction updated or deleted the value in between.

A: looks up Hong Gil-dong's tier → 'GOLD'
B: changes Hong Gil-dong's tier to 'VIP' and commits
A: looks it up again → 'VIP'
→ the same question gets a different answer within one task

4. Phantom Read ​

Within one transaction, querying the same range twice gives a different number of rows. In between, another transaction inserted a new row, so data that wasn't there appears like a phantom.

A: counts members with tier 'VIP' → 5
B: adds 1 new VIP member and commits
A: counts again → 6

Non-Repeatable Read vs Phantom Read

Both share the point "I read it twice and it was different." Tell them apart this way: a non-repeatable read is when the value of an existing row changed (update/delete), and a phantom read is when a new row appeared in the result set (insert).

5. Inconsistency ​

While several pieces of data are being read in turn, another transaction changes that data, so values from mismatched points in time get mixed in a calculation.

A: reads account 1 to compute the sum of accounts 1 and 2
B: completes a transfer from account 2 → account 1 and commits
A: reads account 2
→ adds account 1 before the transfer + account 2 after the transfer, producing a wrong total

The Fix: Transaction Isolation Levels ​

Databases define how far apart transactions are kept using 4 isolation levels. The further down you go, the stricter it gets and the more problems it prevents, but transactions wait on each other longer, so the amount that can be processed at the same time (performance) goes down.

Isolation levelMeaningDirty ReadNon-Repeatable ReadPhantom Read
Read UncommittedReads even uncommitted valuesOccursOccursOccurs
Read CommittedReads only committed valuesPreventedOccursOccurs
Repeatable ReadValues read stay the same for the whole transactionPreventedPreventedOccurs
SerializableProcesses transactions as if lined up one at a timePreventedPreventedPrevented

Many DBs default to Read Committed (Oracle, PostgreSQL, etc.) or Repeatable Read (MySQL InnoDB). The safest level, Serializable, has a heavy performance cost, so it is used only where truly needed.

Summary ​

ProblemOne-line summaryOperation that causes it
Lost updateOne side's change is overwritten and disappearsSimultaneous updates
Dirty readRead a value before commit, then it was rolled backReading before commit
Non-repeatable readRead the same row twice and the value differsUpdate/Delete in between
Phantom readRead the same range twice and there are more rowsInsert in between
InconsistencyValues from different points in time mixed in a calculationChanges during reading
  • The fix is isolation levels: they get stricter in the order Read Uncommitted → Read Committed → Repeatable Read → Serializable.
  • The stricter it is, the safer but slower, which is a trade-off.

Reference: personal Notion study notes