Isolation
Transactions do not interfere with each other
“Isolation ensures that concurrent execution of transactions results in a system state that would be obtained if transactions were executed serially, i.e., one after the other.”
When to use
High concurrency environments.
Why it matters
- Accuracy: Prevents “Race Conditions” where two users buy the last ticket at the exact same millisecond.
Signs of Violation
- Dirty Reads: Reading data that another transaction wrote but hasn’t committed yet.
- Lost Updates: User A overwrites User B’s changes without knowing it.
Explanation
Problem
Concurrency is hard. If A reads X=10, and B reads X=10, and both add 1 and save, X becomes 11. It should be 12.
Solution
Locking (Row locks, Table locks) or Versioning (MVCC).
Real world analogy
Editing a Google Doc. If two people type in the same spot at the exact same time, it gets messy. Isolation is like “Locking the paragraph” so only one person can edit it at a time.
Pros and Cons
| Pros | Cons |
|---|---|
Comparison
- Isolation Levels: Read Uncommitted (Fastest, Dangerous) -> Read Committed -> Repeatable Read -> Serializable (Slowest, Safest).
Code example
Typescript
Bad (Violation)
Good (Adherence)
PHP
Good (Adherence)