Backend & systems·topic 3 of 13
Transactions and isolation
Everybody says ACID in interviews and almost nobody can name the anomaly their isolation level still allows. The vocabulary here is precise on purpose: the words are the difference between a bug you can reason about and one you cannot reproduce.
Read in order · tick what you already know
- 01
you wrap the two updates in a transaction so either both land or neither does, and nobody sees the halfway state.
ACID
- 02
the process died halfway through and you needed the first update to be rolled back rather than left standing on its own.
Atomicity
- 03
you told the user it was saved, so it has to still be there after somebody pulls the power cable out.
Durability
- 04
you pick how much of other people's in-flight work your transaction is allowed to see, usually by accepting whatever the default was.
Isolation level
- 05
you read a value another transaction had written but not committed, and then it rolled back and the value never existed.
Dirty read
- 06
you only see other transactions' work once they commit, but two reads inside your own transaction can still disagree.
Read committed
- 07
you read the row at the start of the transaction, read it again at the end, and it has a different value because somebody committed in between.
Non-repeatable read
- 08
you read the same row twice inside one transaction and are guaranteed to get the same answer both times.
Repeatable read
- 09
you run the same range query twice in one transaction and the second time there is a new row matching it.
Phantom read
- 10
the database promises the result is as if the concurrent transactions had run one after another, and aborts one of them to keep that promise.
Serialisable
- 11
your transaction sees the database frozen as of the instant it began, no matter what commits while it runs.
Snapshot isolation
- 12
an update writes a new version of the row instead of overwriting it, so readers already in flight keep seeing the old one.
MVCC
- 13
two transactions each check the same rule, each sees it satisfied, each writes a different row, and together they break the rule.
Write skew
- 14
someone opened a transaction, went off to call an HTTP API, and left locks and dead row versions pinned for eight seconds.
Idle in transaction
- 15
you ask both databases to prepare, wait for both to say yes, and only then tell both to commit.
Two-phase commit
- 16
you break the cross-service operation into local transactions and write an undo step for each one, because there is no shared rollback.
Saga
- 17
you cannot roll back the payment you already captured, so you issue a refund that puts the world approximately back.
Compensating transaction