System Design · 03. Data & Storage · 12 min · Advanced

Transactions & Isolation

Make several changes all-or-nothing, even when many users change data at once.

In short

A transaction makes several changes all-or-nothing. Isolation decides what overlapping transactions can see: dirty reads, changing values and phantoms appear as you go down the levels.

Databases give isolation with locks (wait) or MVCC (snapshots). Across several databases a plain transaction is not enough; that needs sagas or two-phase commit, covered in the Architecture chapter.

Key ideas

  • A transaction groups several operations into one unit that behaves as a single step. (step 1)
  • A read-committed database blocks dirty reads, yet T2 still sees the world change under its feet. (step 5)
  • Higher isolation means fewer surprises and more waiting or retries. Pick the lowest level your rules can live with. (step 7)
  • Whenever you read, think, then write, ask what happens if someone else does the same at that moment. (step 10)

Good to know

  • Transactions give ACID: Atomic, Consistent, Isolated, Durable. Atomic and durable come from the write-ahead log; this lesson is about the I. (step 2)
  • PostgreSQL, MySQL InnoDB and Oracle all use MVCC. Reads do not block writes, and writes do not block reads. (step 9)

Watch out

  • Isolation bugs are rare, silent and hard to reproduce, because they depend on exact timing. (step 3)
  • Only the lowest level, read uncommitted, allows this. Almost every database defaults to something stronger. (step 4)
  • Phantoms break checks like "only 1 booking per room" or "at least 1 doctor on call", which look at a range of rows. (step 6)
  • Keep transactions short and touch rows in the same order everywhere. Never wait for a user's click inside a transaction. (step 8)
Transaction T1Transaction T2accountstotal $800

Key numbers

What T2 sees
$800
Anomaly
none

Terms

Transaction
A group of operations that the database treats as one unit.
Atomicity
All the changes of a transaction happen, or none of them do.
ACID
Atomic, Consistent, Isolated, Durable: the four promises of a database transaction.
Isolation
Overlapping transactions behave as if they had run one after the other.
Dirty read
Reading a change that another transaction has not committed yet.
Non-repeatable read
Reading the same row twice in one transaction and getting different values.
Phantom read
Running the same range query twice and seeing new rows appear.
Isolation level
A setting that decides which anomalies a transaction may see.
Serializable
The strongest level: the result equals running the transactions one at a time.
Two-phase locking
Take locks as you go and release them all at commit, so others wait.
Deadlock
Two transactions each wait for a lock the other holds; the database aborts one.
MVCC
Keep several versions of each row so readers use a snapshot and never wait for writers.
Lost update
Two transactions read the same value and both write back, so one change disappears.