What it is
ACID names four guarantees that a database transaction gives: Atomicity (all of its writes happen or none do), Consistency (it moves the data from one valid state to another), Isolation (concurrent transactions do not see each other's unfinished work) and Durability (once acknowledged, a commit survives crashes). The acronym is from 1983; each letter is implemented by a different mechanism with its own cost.
Why it exists
Applications run many operations at once on machines that crash. Without guarantees, every developer would have to reason about partial writes, interleavings and lost data in every code path. ACID moves that reasoning into the database: application code can treat a transaction as a single step that either happened completely and permanently, or not at all.
How it works
Atomicity. In Postgres every row change creates a new row version stamped with the transaction id; the commit log (pg_xact) records whether that id committed or aborted. Committing flips that status in one step, which makes all the versions visible together; aborting leaves them invisible until vacuum removes them. Engines such as InnoDB and Oracle update rows in place and keep undo logs to restore the old values on rollback.
Consistency. The database guarantees that declared constraints hold after every transaction: NOT NULL, CHECK, UNIQUE, primary and foreign keys, exclusion constraints, and anything enforced by triggers. A statement that would break one fails, and the transaction must roll back. Rules the schema does not express are the application's responsibility, which is why the "C" is often described as a property of the application using the other three letters.
Isolation. Postgres gives each statement (READ COMMITTED) or each transaction (REPEATABLE READ, SERIALIZABLE) a snapshot of committed data, and uses row locks for conflicting writes. Readers never see uncommitted changes. Weaker levels still allow some anomalies between concurrent transactions; the Isolation Levels lesson covers which.
Durability. Changes are first described in the write-ahead log (WAL). On COMMIT, Postgres flushes the WAL up to the commit record to stable storage (fsync or an equivalent) and only then answers. Changed data pages stay in shared buffers and are written to the data files later by the background writer and at checkpoints. After a crash, recovery starts from the last checkpoint and replays every WAL record after it. The rule that makes this safe: a data page may only be written once the WAL describing its change is on disk.
Group commit. A flush covers everything in the WAL buffer, so commits that arrive while one flush is in progress are made durable together by the next one. This raises commit throughput far above "one flush per commit"; it does not remove the flush latency from each commit.
When to use it
- For any data where losing or half-applying a change is unacceptable: money, orders, inventory, permissions, anything that is the system of record.
- Whenever invariants span several rows or tables and must hold under concurrency.
- As the default: relational databases give ACID transactions without extra work, and turning guarantees down should be an explicit decision.
When not to
- High-volume data where losing the last fraction of a second is acceptable (metrics, click streams, caches): relaxed durability such as
synchronous_commit = off, or a store without transactions, can be cheaper. - Workloads spread over many services: ACID ends at the database boundary, and cross-service consistency needs sagas, outboxes and idempotency instead.
- Globally distributed writes with very low latency requirements, where strict isolation and synchronous durability across regions cost too many round trips.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Flush WAL on every commit (default) | No acknowledged commit is lost on a crash | Storage flush latency on every commit |
synchronous_commit = off |
Commits ~as fast as memory | A crash can lose the last ~3 × wal_writer_delay of commits (never consistency) |
| Synchronous standby | Commits survive losing the whole primary | A network round trip and a remote flush per commit; commits wait if no standby answers |
| Frequent checkpoints | Shorter crash recovery | More disk writes during normal operation (full-page writes, I/O spikes) |
| Stricter isolation | Fewer anomalies | More aborts and retries, or more locking |
Alternatives
- BASE systems ("basically available, soft state, eventually consistent"): many NoSQL stores trade multi-record transactions and immediate consistency for availability and partition tolerance. Several now offer single-partition or limited multi-record transactions.
- Distributed SQL databases (Spanner, CockroachDB, YugabyteDB and similar) provide ACID transactions across nodes using consensus replication, at the cost of consensus round trips on every commit.
- Application-level consistency: sagas, outboxes and idempotent consumers when operations span services.
Failure modes
- Disks or controllers that lie about flushes (volatile write caches without power-loss protection):
fsyncreturns, the data is not durable, and a power cut loses or corrupts it. fsync = offin production: fast until the first crash, then potentially unrecoverable corruption.- Assuming "C" covers business rules that were never declared as constraints.
- Long recovery after a crash when checkpoints are far apart and the WAL since the last one is large.
- Commits stalling when the only synchronous standby is down.
In production
Use storage with reliable flushes and measure its flush latency (pg_test_fsync). Keep fsync and full_page_writes on. Use synchronous_commit = off only per transaction for data you can afford to lose. For synchronous replication list several standbys (synchronous_standby_names = 'ANY 1 (a, b)') so one failure does not stop commits. Tune checkpoint_timeout and max_wal_size for a recovery time you can live with, and test restores and failovers, not just backups.
Common misconceptions
- "ACID means my data is correct." It means the database kept its promises; wrong logic commits wrong data atomically and durably.
- "Durable means written to the table on disk." It means the log is on disk; the table catches up later.
- "Consistency in ACID is the same as in CAP." ACID consistency is about invariants; CAP consistency is about all replicas returning the latest write (linearisability).
- "Isolation means transactions run one at a time." Only SERIALIZABLE promises an outcome equivalent to that, and even it runs transactions concurrently.
Interview angle
Be ready to explain each letter with its mechanism (row versions or undo log, constraints, snapshots and locks, WAL and fsync) and its cost. A strong answer distinguishes ACID consistency from CAP consistency, explains why a commit waits for a flush and how group commit helps, and knows the durability knobs: what synchronous_commit = off risks, why fsync = off is different, and what a synchronous standby adds.
Model assumptions
Steps 1–6 are scripted illustrations, not simulations: the engine does not model row versions, constraints, snapshots, the WAL, checkpoints or recovery. Packets are slowed down so the order is visible; values are annotations. Steps 7–9 and the sandbox are simulated: each request is one committed write whose time inside the database is set by hand: about 6 ms (1 ms of work plus an illustrative 5 ms WAL flush on network-attached storage), about 1.2 ms with synchronous_commit = off. The model does not share flushes between commits (no group commit). The synchronous standby is the engine's synchronous replication: the primary answers once the standby, 4 ms away, has flushed the write (3 ms) and its acknowledgement has travelled the 4 ms back, so a commit takes about 11 ms longer; the primary's connection stays busy while it waits. Arrivals are random (Poisson), durations lognormal. Numbers are illustrative, not benchmarks.