Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

ACID & Isolation Levels
SQL

ACID & Isolation Levels

Master the fundamental properties of transactions and how isolation levels prevent data anomalies.

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:

AnomalyDescriptionPrevention Level
Dirty ReadReading uncommitted data from another transactionRead Committed
Non-Repeatable ReadSame row read twice, gets different valuesRepeatable Read
Phantom ReadSame range read twice, gets different rowsSerializable
Lost UpdateTwo transactions overwrite each other’s changesSerializable / Optimistic Locking

Standard SQL Isolation Levels

LevelDirty ReadNon-Repeatable ReadPhantom ReadPerformance
Read UncommittedPossiblePossiblePossibleBest
Read CommittedPreventedPossiblePossibleGood
Repeatable ReadPreventedPreventedPossibleMedium
SerializablePreventedPreventedPreventedWorst

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.UserAadds100. User A adds 50 (New: 150).UserBadds150). User B adds 30 (New: 130).IfBcommitsafterA,Asupdateis"lost",andbalanceis130). If B commits after A, A's update is "lost", and balance is 130 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.

My Private Notes

Notes are auto-saved locally to this device.