Transactions are governed by the ACID properties, which guarantee that database transactions are processed reliably.
ACID Properties
Atomicity
“All or nothing.” If any part of a transaction fails, the entire transaction is rolled back as if nothing happened.
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
-- If server crashes here, the debit is rolled back
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
Consistency
Transactions transform the database from one valid state to another. All constraints, triggers, and rules are preserved.
Isolation
Concurrent transactions should appear as if they ran one after another. The database provides isolation levels that let you trade strictness for performance.
Durability
Once a transaction is committed, the changes persist even if the server crashes immediately after. This is typically achieved through write-ahead logging (WAL).
Transaction Anomalies
These are problems that occur when isolation is insufficient:
| Anomaly | Description | Prevention Level |
|---|---|---|
| Dirty Read | Reading uncommitted data from another transaction | Read Committed |
| Non-Repeatable Read | Same row read twice, gets different values | Repeatable Read |
| Phantom Read | Same range read twice, gets different rows | Serializable |
| Lost Update | Two transactions overwrite each other’s changes | Serializable / Optimistic Locking |
Standard SQL Isolation Levels
| Level | Dirty Read | Non-Repeatable Read | Phantom Read | Performance |
|---|---|---|---|---|
| Read Uncommitted | Possible | Possible | Possible | Best |
| Read Committed | Prevented | Possible | Possible | Good |
| Repeatable Read | Prevented | Prevented | Possible | Medium |
| Serializable | Prevented | Prevented | Prevented | Worst |
PostgreSQL Default: Read Committed
Most databases default to Read Committed. This prevents dirty reads but allows non-repeatable reads and phantoms. It is the right balance for most applications.
Serializable
The strictest level. Transactions are executed as if they ran sequentially. This prevents all anomalies but can significantly reduce concurrency. Use it only when absolute correctness is required (e.g., financial systems).
Snapshot Isolation (MVCC)
PostgreSQL, Oracle, and MySQL (InnoDB) use Multi-Version Concurrency Control (MVCC). Instead of locking rows for readers, the database keeps multiple versions of each row. Readers see a snapshot of the data as of the start of their transaction, so reads never block writes and writes never block reads.
In MVCC:
READ COMMITTED: A new snapshot is taken for each statement.REPEATABLE READ: The same snapshot is used for the entire transaction.SERIALIZABLE: Uses snapshot isolation plus runtime conflict detection.
Q: What is a Dirty Read?
A: A dirty read occurs when a transaction reads data that has been modified by another transaction but not yet committed. If that other transaction rolls back, the first transaction has read “fake” data.
Q: What is a Phantom Read?
A: A phantom read occurs when a transaction reads a range of rows twice and sees a different number of rows the second time because another transaction inserted or deleted rows in that range during the interval.
Q: Difference between READ COMMITTED and REPEATABLE READ?
A:
Read Committed:A row might change between two SELECTs in the same transaction.Repeatable Read:A row is “locked” or versioned so it remains identical for the duration of the transaction.
Q: What is the default isolation level in PostgreSQL?
A: Read Committed. It prevents dirty reads but allows non-repeatable reads and phantom reads.
1. Bank Transfer Scenario (Atomicity).
Problem: If the server crashes after deducting money but before adding it to the recipient, money vanishes.
Solution: Use BEGIN TRANSACTION and COMMIT. SQL ensures if any part fails, the whole thing is rolled back.
2. Lost Update Example.
Scenario: Two users read a balance of 100.UserAadds50 (New: 150).UserBadds30 (New: 130).IfBcommitsafterA,A′supdateis"lost",andbalanceis130 instead of $180.
Fix: Use SELECT ... FOR UPDATE or optimistic concurrency control.
3. Compare SERIALIZABLE vs REPEATABLE READ.
Summary: Repeatable Read protects existing rows from being changed, but doesn’t prevent new rows from being added (Phantoms). Serializable prevents both but is significantly slower due to high locking overhead.
4. Choose isolation level for a reporting system.
Answer: Read Committed is sufficient — reports don’t need perfect consistency. If exact numbers matter (financial audit), use Repeatable Read or Serializable.
Premium Content
Unlock ACID & Isolation Levels and all premium lessons with a subscription.
From ₹199.99/year — See plans