ACID
ACID is the set of guarantees a database gives a transaction, a group of reads and writes that should behave as one unit. A transfer of 100 from Alice to Sam is two updates; ACID promises that both happen or neither does, that concurrent transfers do not corrupt each other, and that a committed transfer survives a power cut.
All or nothing
If anything fails mid-transaction, every change is rolled back. Never "Alice debited, Sam not credited".
Rules still hold
Constraints (foreign keys, CHECK balance >= 0, unique keys) are true before and after. Mostly your schema's job.
Concurrency looks safe
Transactions running at the same time do not see each other's half-done work. How strictly depends on the isolation level.
Committed means saved
Once COMMIT returns, the change survives a crash: it is in the write-ahead log on disk (and often on a replica).
How databases deliver it
- Atomicity and durability come from the write-ahead log (WAL): changes are appended to a log and flushed to disk before
COMMITreturns. After a crash, the database replays committed work and discards the rest. - Isolation comes from locks and MVCC (multi-version concurrency control): readers see a consistent snapshot while writers create new row versions.
- Consistency is shared: the database enforces the constraints you declare; your application must keep the rest of the business rules.
Isolation is where the bugs live
Atomicity and durability are rarely the problem in practice. Isolation is, because most databases do not run fully isolated by default. Here is the most common bug it causes.
- One item left in stock. Alice and Sam press "Buy" at the same moment, starting transactions T1 and T2.
- Both read stock = 1. Each sees "in stock", which was true when it looked.
- T1 computes 1 − 1 = 0, writes stock = 0, inserts an order and commits.
- T2 does the same math on its stale read and overwrites stock with 0. Two orders, one item: T1's decrement was lost. READ COMMITTED allows this.
- Fix it by locking the row (
SELECT … FOR UPDATE), by an atomicUPDATE … SET stock = stock - 1 WHERE stock > 0, or by running at SERIALIZABLE and retrying aborted transactions.
Isolation levels
The SQL standard defines four levels by which anomalies they allow. Real databases differ in the details, so check yours.
| Level | Dirty read | Non-repeatable read | Lost update | Write skew / phantoms | Default in |
|---|---|---|---|---|---|
| Read uncommitted | possible | possible | possible | possible | rarely used |
| Read committed | prevented | possible | possible | possible | PostgreSQL, Oracle, SQL Server |
| Repeatable read / snapshot | prevented | prevented | depends on DB | possible | MySQL InnoDB |
| Serializable | prevented | prevented | prevented | prevented | CockroachDB, Spanner (strict serializable) |
Notes that trip people up:
- PostgreSQL’s REPEATABLE READ is snapshot isolation and detects the lost update above (T2 gets a serialization error). MySQL’s REPEATABLE READ does not, because a plain
SELECTreads a snapshot whileUPDATEwrites the latest version. - Oracle’s “SERIALIZABLE” is actually snapshot isolation and still allows write skew (two doctors both going off call because each saw the other still on call).
- Serializable costs throughput and produces aborts, so the application must retry failed transactions.
ACID’s C is not CAP’s C
The same letter means two different things:
| ACID consistency | CAP consistency | |
|---|---|---|
| About | Data obeys your rules (constraints, invariants) | Every replica returns the latest write (linearizability) |
| Scope | One database, one transaction | Many replicas of the same data |
| Broken by | A bug or a missing constraint | Replication lag, network partitions |
| Read more | This page | CAP |
ACID in data engineering
- Warehouses give ACID per statement or transaction, but isolation varies: Snowflake runs READ COMMITTED, BigQuery snapshot isolation, Redshift serializable or snapshot depending on configuration.
- Lakehouse table formats (Apache Iceberg, Delta Lake, Apache Hudi) bring ACID commits to files on object storage: a commit atomically publishes a new version of the table metadata, and concurrent writers use optimistic concurrency (one wins, the other retries).
- NoSQL stores traditionally offered single-row atomicity and BASE (basically available, soft state, eventually consistent). Many now add transactions: MongoDB multi-document transactions, DynamoDB
TransactWriteItems. - Across databases or services you need distributed transactions or sagas.
Common mistakes
- Read, compute in the app, write back. That pattern is exactly the lost update. Prefer atomic SQL (
SET x = x - 1) or explicit locks. - Assuming the default level is serializable. It almost never is.
- Long transactions. Holding a transaction open while calling an external API keeps locks and old row versions alive.
- Not retrying. Serialization failures and deadlocks are normal under load; wrap transactions in a retry loop.
- Turning off
fsyncorsynchronous_commitfor speed and then being surprised that “committed” data vanished after a crash.
Learning Resources
Books
- Designing Data-Intensive Applications (2nd ed.), Martin Kleppmann and Chris Riccomini: the transactions chapter
- Database Internals, Alex Petrov: chapter 5, transaction processing and recovery
Docs and papers
- PostgreSQL: Transaction Isolation
- A Critique of ANSI SQL Isolation Levels: Berenson et al., 1995
- Hermitage: Martin Kleppmann’s tests of which anomalies each database really allows
- ACID Transactions, Yugabyte