Transactions & isolation
ACID, isolation levels and their anomalies (dirty/non-repeatable/phantom reads), MVCC, row locks, SELECT FOR UPDATE. The double-booking problem lives here.
The mental model
A transaction is a contract of all-or-nothing. You wrap a set of changes — debit one account, credit another — and the database promises they either all happen or none do. There is no state of the world where the money left one account but never arrived in the other, no matter what crashes in between. That guarantee is what lets you reason about a multi-step operation as if it were a single instantaneous act.
The four letters, ACID, name four promises: Atomicity (all-or-nothing), Consistency (the transaction moves the database from one valid state to another, respecting constraints), Isolation (concurrent transactions do not corrupt each other's view), and Durability (once committed, it survives a crash). Atomicity and durability are mostly free from the database. Isolation is the one you have to think about, because "do not corrupt each other's view" is not one setting — it is a dial with several positions, each trading correctness for concurrency.
Why this exists — what breaks without it. Two customers book the last seat at the same instant; without isolation, both reads see it free and both bookings succeed — the double-booking bug. A transfer debits an account, the process crashes before the credit, and money vanishes — without atomicity. An inventory count is read, decremented, and written back by two requests concurrently; both read 10, both write 9, and you have sold an item you do not have — the lost update. Every one of these is an isolation or atomicity failure, and every one of them is a real production incident that has cost real companies real money. The double-booking problem lives here, and interviewers at product companies — especially fintech — will push you until they find the edge of your isolation-level knowledge.
- Deep dive
- Internal architecture
- Real-world use cases
- Best practices
- Common mistakes
- Performance
- Security
- Production troubleshooting
- Advantages & disadvantages
- Alternatives
- Interview expectations
- Interview questions
- Facts to remember
- Cheat sheet
- Quick revision