Concurrency Anomalies
When multiple transactions interleave their read and write operations on shared database objects without proper coordination, several concurrency anomalies (race conditions) can occur. This post walks through each one — and how databases detect and prevent them.
When Transactions Interleave, What Can Go Wrong?
When two transactions read and write the same objects at the same time, their operations can interleave in ways that produce outcomes no serial execution would ever reach. There are three fundamental conflicts.
Read-Write Conflict (Unrepeatable Read)
- What happens: a transaction reads a data object, another transaction modifies and commits that same object, and the first transaction reads it again — getting a different result mid-execution.
- Example:
- Transaction 1 reads account A and sees a balance of $10.
- Transaction 2 overwrites account A with $19 and commits.
- Transaction 1 reads account A again — within the same transaction — and sees $19.
- Why it's invalid: if Transaction 1 were running in complete isolation, reading account A twice without modifying it should yield $10 both times.
Write-Read Conflict (Dirty Read)
- What happens: a transaction reads uncommitted changes made by another transaction that subsequently aborts and rolls back.
- Example:
- Transaction 1 updates account A to $12.
- Before Transaction 1 commits, Transaction 2 reads account A and sees the uncommitted $12.
- Transaction 1 aborts and rolls back its update.
- Why it's invalid: Transaction 2 acted on "dirty" temporary data that legally never existed in the database — and if Transaction 2 commits, it may base its updates on data that vanished.
Write-Write Conflict (Lost Update / Blind Write)
- What happens: two transactions perform blind writes (writing an object without reading it first) on overlapping records, interleaving into an impossible state.
- Example:
- Transaction 1 wants to set A = $10 and B = "Alice".
- Transaction 2 wants to set A = $19 and B = "Bob".
- The writes interleave: Transaction 2 writes A = $19, then Transaction 1 writes B = "Alice".
- Why it's invalid: the final state has $19 in A and "Alice" in B — an outcome that matches neither running Transaction 1 first nor Transaction 2 first in any serial order.
So How Do Databases Detect and Prevent These?
Serializability
Serializability is the fundamental standard for correctness: an interleaved execution schedule is serializable if its final outcome on the database state is identical to running those transactions sequentially, one after another, in some serial order.
Conflict Serializability & Dependency Graphs
But testing every possible database state is impractical — so how does the database verify serializability efficiently?
It uses conflict serializability through a dependency graph (precedence graph):
- Conflicting operations: two operations conflict if they belong to different transactions, access the exact same object, and at least one is a write.
- Building the graph:
- Each running transaction becomes a node.
- A directed edge is drawn T1 → T2 if T1 executes an operation that conflicts with a later operation on the same object by T2.
- Cycle detection:
- No cycles → the execution is proven conflict serializable (equivalent to some serial ordering) and is safe.
- A cycle exists (e.g., T1 → T2 → T1) → the schedule contains an unresolvable conflict that creates anomalies, and one of the transactions must be aborted or the operations reordered.
View Serializability
There's also a broader, more relaxed definition — view serializability — which allows certain schedules that conflict serializability rejects, such as particular sequences of blind writes, or schedules where higher-level application logic still ensures a correct end result.
So why don't real databases use it? Because view serializability requires the engine to inspect and interpret application-level program logic — or ask a human developer what the queries actually mean. Database systems don't know upcoming queries in advance and can't infer intent, so every real-world engine uses conflict serializability instead.
Related reads
- Concurrency Control Theory in Databases — the problem this taxonomy of anomalies belongs to, from processors up to ACID
- Two-Phase Locking — the runtime protocol that prevents these anomalies from forming
- How WALs became a thing — the sibling engine that handles atomicity and durability while concurrency control handles I and C