Database Isolation Levels
๐ก Database Isolation Levels
Imagine youโre driving through a busy intersection.
Cars = transactions
Intersection = database
Traffic lights = isolation levels
If there are no rules, everyone just drives whenever they want โ chaos.
Thatโs why databases use Isolation Levels: rules that control how transactions move so data doesnโt crash into each other.
๐ฆ The Traffic Light Analogy
Think of a busy intersection.
Cars = transactions
Intersection = database
Traffic lights = isolation levels
๐ Read Uncommitted โ No traffic lights. Everyone just rushes in. Fast, but lots of accidents (dirty reads).
๐ฆ Read Committed โ Red/green lights exist, but they only control entry. Cars can still switch lanes suddenly (non-repeatable reads).
๐ Repeatable Read โ Not only red/green, but lanes are locked until you exit the junction. No car can push you out โ but surprise, new cars might still show up in an empty lane (phantoms).
๐ฎ Serializable โ A strict traffic cop makes cars go one batch at a time. Zero accidents, but slow traffic.
Understanding isolation levels is key to writing correct and concurrent database applications.
This guide explains all levels with clear tables, anomalies, and transaction flows so that learning is fast and practical.
Isolation Level | Key Points | Concurrency | Isolation Strength |
---|---|---|---|
Read Uncommitted | Can read uncommitted (dirty) data. Dirty Read, Non-Repeatable Read, Phantom Read possible. | High | Low |
Read Committed | Reads only committed data. Prevents Dirty Read. Non-Repeatable & Phantom Read possible. | Medium-High | Medium-Low |
Repeatable Read | Reads held via shared locks until commit. Prevents Dirty & Non-Repeatable Read. Phantom Read possible. | Medium | High |
Serializable | Strictest. Uses range locks. Prevents Dirty, Non-Repeatable & Phantom Reads. | Low | Highest |
2๏ธโฃ What Problems Can Happen? (Accidents on the Road)
- – Dirty Read ๐จ โ You look at another carโs lane change before itโs even finished. (Reading uncommitted data).
- – Non-Repeatable Read ๐ โ You check once, see a car in lane 1. You look again, and it has moved to lane 2. (Same row, different value).
- – Phantom Read ๐ป โ You look at the junction, count 2 cars. A moment later, new cars have appeared out of nowhere. (New rows magically appear).
Isolation Level | Dirty Read | Non-Repeatable Read | Phantom Read |
---|---|---|---|
Read Uncommitted | โ Yes | โ Yes | โ Yes |
Read Committed | โ No | โ Yes | โ Yes |
Repeatable Read | โ No | โ No | โ Yes |
Serializable | โ No | โ No | โ No |
3๏ธโฃ Transaction Examples (Traffic Flow Timelines)
Letโs watch how two cars (T1 & T2) drive through the intersection.
Time flows downward.
S-lock = car blocking a lane for looking.
X-lock = car blocking a lane for turning/changing.
Read Uncommitted (Dirty Read possible)
One car drives in without waiting, and another peeks too early.
| Time โ | T1 | T2 |
| ------ | --------------------- | ------------------ |
| t1 | BEGIN | |
| t2 | UPDATE Alice (X-lock) | |
| t3 | | READ Alice (dirty) |
| t4 | COMMIT | |
Read Committed (Non-Repeatable Read possible)
Cars wait for the green light, but switching lanes between looks is allowed.
| Time โ | T1 | T2 |
| ------ | ------------------------------------------- | --------------------- |
| t1 | BEGIN | |
| t2 | READ Alice (S-lock,<br>released after read) | |
| t3 | | UPDATE Alice (X-lock) |
| t4 | | COMMIT |
| t5 | READ Alice again (new value) | |
| t6 | COMMIT | |
Repeatable Read (Phantom Read possible)
Your lane is locked until you leave, but new lanes can still open.
| Time โ | T1 | T2 |
| ------ | ------------------------------------ | --------------------------------- |
| t1 | BEGIN | |
| t2 | READ Alice (S-lock held till commit) | |
| t3 | | UPDATE Alice (X-lock,<br>blocked) |
| t4 | COMMIT | |
| t5 | | UPDATE Alice (succeeds) |
| t6 | | COMMIT |
Serializable (No anomalies)
The traffic cop says: โOne batch at a time. Wait your turn.โ
| Time โ | T1 | T2 |
| ------ | ----------------------------------------------------------------------------- | ----------------------------- |
| t1 | BEGIN | |
| t2 | READ Accounts WHERE balance>1000<br>(S-lock + range lock<br>held till commit) | |
| t3 | | INSERT Bob(2000)<br>(blocked) |
| t4 | COMMIT | |
| t5 | | INSERT succeeds |
| t6 | | COMMIT |
4๏ธโฃ Crux / Key Differences
Feature / Isolation | Read Uncommitted | Read Committed | Repeatable Read | Serializable |
---|---|---|---|---|
Dirty Read | Can read uncommitted (dirty) data โ | Prevents dirty read โ | Prevents dirty read โ | Prevents dirty read โ |
Non-Repeatable Read | Non-Repeatable read possible โ | Non-Repeatable read possible โ | Prevents Non-Repeatable read โ | Prevents Non-Repeatable read โ |
Phantom Read | Phantom read possible โ | Phantom read possible โ | Phantom read possible โ | Prevents Phantom read โ |
S-lock on read | None | Short-lived | Held till commit | Held + Range |
X-lock on write | Exclusive lock applied โ | Exclusive lock applied โ | Exclusive lock applied โ | Exclusive lock applied โ |
Concurrency | High | Medium-High | Medium | Low |
Isolation strength | Lowest | Medium-Low | High | Highest |