Ankit KanojiyaIntermediate
Evergreen guide by Ankit Kanojiya · Intermediate · Databases
Transactions, ACID, and Isolation Levels Explained
A grounded look at ACID guarantees, the four SQL isolation levels, and the anomalies each one prevents.
- Transactions
- ACID
- Isolation Levels
- Concurrency
- MVCC
- SQL
A transaction groups several operations so they succeed or fail as a unit. Behind that simple idea sit the ACID guarantees and a set of isolation levels that control how concurrent transactions see each other's work.
The ACID properties
ACID is four promises a transactional database makes:
- Atomicity: all statements in a transaction commit together, or none do. A failure rolls everything back.
- Consistency: a transaction moves the database from one valid state to another, respecting constraints.
- Isolation: concurrent transactions do not corrupt each other; the result is as if they ran in some serial order.
- Durability: once committed, data survives a crash, typically guaranteed by a write-ahead log.
A basic transaction looks like this:
begin;
update accounts set balance = balance - 100 where id = 1;
update accounts set balance = balance + 100 where id = 2;
commit;
If the second update fails, rollback undoes the first, so money is never lost or created.
Why isolation is negotiable
Isolation is the one ACID property databases let you relax, because full isolation is expensive under concurrency. The SQL standard defines four levels, each allowing more anomalies in exchange for more throughput.
The three classic anomalies:
- Dirty read: reading another transaction's uncommitted changes.
- Non-repeatable read: a row read twice returns different values because another transaction changed it in between.
- Phantom read: a query run twice returns different rows because another transaction inserted or deleted matching rows.
The four isolation levels
- Read Uncommitted allows dirty reads. Rarely useful; some engines do not even implement it as distinct.
- Read Committed prevents dirty reads. Each statement sees only committed data, but two reads in the same transaction can differ. This is the common default.
- Repeatable Read prevents dirty and non-repeatable reads. Rows seen once stay stable for the whole transaction.
- Serializable is the strictest: transactions behave as if executed one at a time, preventing all three anomalies.
begin isolation level repeatable read;
select balance from accounts where id = 1;
-- The same row returns the same value for the rest of this transaction.
commit;
Implementation differs by engine
The standard names behavior, not mechanism, so engines differ. PostgreSQL uses multi-version concurrency control (MVCC): readers see a snapshot and never block writers. Its Repeatable Read also prevents phantoms through snapshot isolation, going beyond the minimum the standard requires. Its Serializable level adds predicate checks and may abort a transaction with a serialization failure that the application must retry.
Because of this, portable code should treat a serialization error as an expected outcome under high contention:
-- On serialization_failure, roll back and retry the whole transaction.
begin isolation level serializable;
update inventory set qty = qty - 1 where sku = 'A1' and qty > 0;
commit;
Choosing a level
- Read Committed is a sound default for most applications. It avoids dirty reads with minimal overhead.
- Repeatable Read suits reports or multi-step reads that must see a consistent snapshot.
- Serializable is the safest when correctness under concurrency is critical, at the cost of retries and reduced throughput.
A useful rule: start at the default, then raise the level only for the specific transactions that exhibit a real anomaly.
Pitfalls to watch
- Long-running transactions hold locks and snapshots, bloating storage and blocking cleanup. Keep them short.
- Optimistic approaches at Serializable require retry logic; omitting it turns rare conflicts into user-facing errors.
- Isolation protects within the database only. Logic split across services may still need application-level coordination such as idempotency keys.
Summary
ACID defines what a transaction guarantees; isolation levels define how much concurrency those guarantees tolerate. Understanding the anomalies each level prevents lets you pick the weakest setting that is still correct for the workload, which is usually the fastest safe choice.