Database Isolation Levels
Control how transactions interact with each other. Trade consistency for performance.
1The Bank Account Analogy
- Poor isolation: Both see $1000 → Both withdraw → Account goes -$600!
- Good isolation: First withdrawal locks account → Second sees $200 → Correctly denied
2The Four Isolation Levels
Read Uncommitted
Transactions can read uncommitted changes from other transactions (dirty reads).
Read Committed
Only read committed data. Most common default (PostgreSQL, Oracle).
Repeatable Read
Same query always returns same results within transaction. MySQL default.
Serializable
Transactions execute as if serial (one after another). Highest isolation.
3Read Anomalies Explained
Dirty Read
Reading uncommitted data that might be rolled back.
Non-Repeatable Read
Same row returns different values when read twice.
Phantom Read
New rows appear in range queries between reads.
4Comparison Table
| Isolation Level | Dirty Read | Non-Repeatable | Phantom | Performance |
|---|---|---|---|---|
| Read Uncommitted | Possible | Possible | Possible | Fastest |
| Read Committed | Prevented | Possible | Possible | Fast |
| Repeatable Read | Prevented | Prevented | Possible | Medium |
| Serializable | Prevented | Prevented | Prevented | Slowest |
5Database Defaults
6When to Change Isolation
Use Higher Isolation (Serializable)
- • Financial transactions (transfers, payments)
- • Inventory management (stock levels)
- • Booking systems (double-booking prevention)
- • Anywhere race conditions are critical
Use Lower Isolation (Read Committed)
- • Read-heavy workloads
- • Analytics queries
- • Where slight inconsistency is acceptable
- • High-throughput applications
Understand the trade-off: Higher isolation = More consistent but slower. Be ready to explain when you'd use each level and why.
7Key Takeaways
8Interview Follow-up Questions
Interview Follow-up Questions
Common follow-up questions interviewers ask
9Test Your Understanding
Test Your Understanding
5 questions
What is a dirty read?
Which isolation level is the most commonly used default in production databases?
Phantom reads are possible at which isolation levels?
What is the trade-off of using Serializable isolation?
Transaction A reads row X, then Transaction B modifies and commits row X, then Transaction A reads row X again with different values. This is called: