Isolation levels
medium 26 minIsolation decides what transactions that run at the same time may see of each other.
See it run
Two transactions at the same time: what each may see, which anomalies follow, and which level stops them.
Operations
Dirty read
B reads a value that A has not committed. A then changes its mind.
Step 1 of 6
A starts
| A | B | x | |
|---|---|---|---|
| t1 | BEGIN | 50 | |
| t2 | |||
| t3 | |||
| t4 | |||
| t5 | |||
| t6 |
x = 50
x is 50.
Step 1: A starts
# read uncommitted: may return the newest value, committed or notdef read_uncommitted(row): return row.versions[-1].value# read committed: the newest committed valuedef read_committed(row): for v in reversed(row.versions): if v.committed: return v.value- Stopped from
- Read committed
- Costs
- Very little
- Read uncommitted
- may show it
- Read committed and up
- never
In PostgreSQL you cannot see this even if you ask: its read uncommitted behaves like read committed.
In the real world
The same isolation levels doing real jobs. Pick one and step through it.
A level is chosen per transaction, not once for the whole company.
Step 1 of 3
Most requests
The default
Product pages and simple updates run at read committed. Atomic updates keep them right.
Step 1: Most requests
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Serializable without a retry
criticalAborts are normal at this level. Without a retry users see errors that mean nothing is wrong.
Wrongwith db.transaction("SERIALIZABLE"): work()Rightfor attempt in range(3): try: with db.transaction("SERIALIZABLE"): work() # including every read break except SerializationFailure: backoff(attempt)Trusting the name of a level
criticalRepeatable read means one thing in PostgreSQL, another in InnoDB and another in SQL Server. Read the manual of your database.
Wrong# repeatable read, so no phantoms anywhereRight# PostgreSQL: snapshot isolation# InnoDB: snapshot for plain reads, gap locks for locking reads# SQL Server: locks on rows, phantoms possibleRead, compute, write
majorBetween the read and the write somebody else may change the value. Your write then removes theirs.
Wrongx = read()write(x - 1)RightUPDATE stock SET n = n - 1WHERE id = 7 AND n >= 1Setting the level too late
majorIn PostgreSQL the level must be set before the first query of the transaction. Setting it outside a transaction block has no effect on the next one.
WrongSET TRANSACTION ISOLATION LEVEL SERIALIZABLE;BEGIN;RightBEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
Interview tips
How to explain it
- Isolation decides what transactions see of each other.
- Each level forbids more anomalies and costs more waiting or retries.
- Serializable: the result equals some one-at-a-time order.
Memory trick
- DNP: dirty, non-repeatable, phantom, in the order they are stopped.
- Really Careful Readers Stay safe: RU, RC, RR, S.
Raise the level when
- Several reads must agree.
- A rule spans several rows.
- A decision depends on what you did not change.
Do not forget
- Default levels differ.
- Retry the whole transaction.
- A snapshot is not serializable: write skew.