π§ Understanding Read Phenomena in Databases: Dirty, Non-Repeatable, and Phantom Reads
When working with databases, especially in systems with concurrent transactions, we expect consistency. But without proper control, transactions can interfere with each other, leading to unexpected and sometimes disastrous results.
This blog dives into three common read anomalies, collectively known as read phenomena, that arise from weak isolation: Dirty Reads, Non-Repeatable Reads, and Phantom Reads.
π Isolation and ACID
Before we explore the anomalies, letβs recall that databases aim to follow ACID properties β Atomicity, Consistency, Isolation, and Durability. Isolation ensures that one transaction doesnβt see the intermediate state of another.
However, depending on the chosen isolation level, some of these guarantees may weaken to allow better performance. And thatβs when read anomalies sneak in.
π What Are Read Phenomena?
Letβs explore each of the three read anomalies with examples and visual prompts.
1. π§Ό Dirty Read
A dirty read happens when Transaction T2 reads data modified by T1, which hasn't been committed yet.
If T1 rolls back, T2 would have based its logic on a value that never existed.
Real-life analogy: You peek at someoneβs exam answer sheet while they're still writing. Then they erase it. What you saw was never final.
π₯ Example:
Time | Transaction T1 | Transaction T2 |
---|---|---|
10:00 AM | UPDATE balance = 1200 | |
10:01 AM | SELECT balance β returns 1200 β | |
10:02 AM | ROLLBACK |
π΄ Issue: T2 read a temporary, uncommitted value.
2. π Non-Repeatable Read
A non-repeatable read occurs when a transaction reads the same row twice and gets different results, due to another transaction modifying and committing between those reads.
Real-life analogy: You check your bank balance. A second later, your salary gets credited. You check again β the number changed.
π₯ Example:
Time | Transaction T1 | Transaction T2 |
---|---|---|
2:00 PM | SELECT price FROM product β βΉ500 | |
2:01 PM | UPDATE product SET price = βΉ600 β | |
2:02 PM | SELECT price FROM product β βΉ600 β |
π΄ Issue: T1 sees inconsistent data across reads.
3. π» Phantom Read
A phantom read happens when a transaction runs a query twice, and the set of rows returned is different, due to inserts or deletes by another transaction.
Real-life analogy: You count how many guests are in a room. You turn around, someone enters. You count again β now there are more people.
π₯ Example:
Time | Transaction T1 | Transaction T2 |
---|---|---|
3:00 PM | SELECT * FROM orders WHERE amount > 1000 β 5 rows | |
3:01 PM | INSERT INTO orders (amount = 1500) β | |
3:02 PM | SELECT * FROM orders WHERE amount > 1000 β 6 rowsβ |
π΄ Issue: Same query returns different row sets.
π‘οΈ Isolation Levels & What They Prevent
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 |
β
= Allowed (anomaly can occur)
β = Prevented
π§ͺ Want to Try It?
You can simulate these in two SQL shells with this pattern:
-- Transaction 1
BEGIN;
UPDATE accounts SET balance = 1200 WHERE id = 1;
-- Transaction 2
SELECT balance FROM accounts WHERE id = 1; -- Dirty read?
-- Transaction 1
ROLLBACK;
Change isolation levels using:
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
π― Conclusion
Understanding these read phenomena helps us decide the right isolation level for our application. Choose higher isolation for critical financial or transactional systems, and lower isolation when performance matters more than absolute consistency.
Top comments (0)