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)

  1. – Dirty Read ๐Ÿšจ โ†’ You look at another carโ€™s lane change before itโ€™s even finished. (Reading uncommitted data).
  2. – 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).
  3. – 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

Similar Posts