Database Isolation Levels

F

Fatima Alam

Guest

๐Ÿ’ก 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 LevelKey PointsConcurrencyIsolation Strength
Read UncommittedCan read uncommitted (dirty) data. Dirty Read, Non-Repeatable Read, Phantom Read possible.HighLow
Read CommittedReads only committed data. Prevents Dirty Read. Non-Repeatable & Phantom Read possible.Medium-HighMedium-Low
Repeatable ReadReads held via shared locks until commit. Prevents Dirty & Non-Repeatable Read. Phantom Read possible.MediumHigh
SerializableStrictest. Uses range locks. Prevents Dirty, Non-Repeatable & Phantom Reads.LowHighest

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 LevelDirty ReadNon-Repeatable ReadPhantom 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.


Code:
| 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.


Code:
| 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.


Code:
| 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.โ€


Code:
| 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 / IsolationRead UncommittedRead CommittedRepeatable ReadSerializable
Dirty ReadCan read uncommitted (dirty) data โœ…Prevents dirty read โŒPrevents dirty read โŒPrevents dirty read โŒ
Non-Repeatable ReadNon-Repeatable read possible โœ…Non-Repeatable read possible โœ…Prevents Non-Repeatable read โŒPrevents Non-Repeatable read โŒ
Phantom ReadPhantom read possible โœ…Phantom read possible โœ…Phantom read possible โœ…Prevents Phantom read โŒ
S-lock on readNoneShort-livedHeld till commitHeld + Range
X-lock on writeExclusive lock applied โœ…Exclusive lock applied โœ…Exclusive lock applied โœ…Exclusive lock applied โœ…
ConcurrencyHighMedium-HighMediumLow
Isolation strengthLowestMedium-LowHighHighest

Continue reading...
 


Join ๐•‹๐•„๐•‹ on Telegram
Channel PREVIEW:
Back
Top