Q&A-1Explain the ACID properties, and which one is most often misunderstood.
Answer
The 90-second answer. ACID is four guarantees the transaction manager makes about a group of statements.
- Atomicity: all statements commit, or none do. A crash or error mid-transaction leaves no partial work; uncommitted changes are undone.
- Consistency: the transaction takes the database from one state satisfying your declared constraints to another. The database does not invent this property; your schema plus the other three do.
- Isolation: concurrent transactions don't observe each other's uncommitted work. How much they can observe of each other's committed work is the isolation level; READ COMMITTED is PostgreSQL's default.
- Durability: once commit returns, the change survives a crash. The WAL record is flushed before the client sees the acknowledgement.
The most misunderstood is consistency. It does not mean the data is correct in any business sense. It means no declared constraint (NOT NULL, UNIQUE, FOREIGN KEY, CHECK) is violated at commit. If your rule isn't declared, the database will let a transaction break it and consistency in the ACID sense still holds.
Here is that gap, on PostgreSQL 16 at the default READ COMMITTED:
CREATE TABLE orders (id int PRIMARY KEY, total numeric NOT NULL);
CREATE TABLE order_lines (order_id int REFERENCES orders(id), amount numeric NOT NULL);
INSERT INTO orders VALUES (1, 100);
INSERT INTO order_lines VALUES (1, 100);
-- Invariant we care about: orders.total = SUM(order_lines.amount).
-- It is not declared anywhere, so the database cannot enforce it.A: BEGIN;
B: BEGIN;
A: SELECT total FROM orders WHERE id = 1; -- 100
B: SELECT total FROM orders WHERE id = 1; -- 100
A: INSERT INTO order_lines VALUES (1, 30);
A: UPDATE orders SET total = 130 WHERE id = 1;
A: COMMIT;
B: INSERT INTO order_lines VALUES (1, 50);
B: UPDATE orders SET total = 150 WHERE id = 1; -- B's stale 100 + 50
B: COMMIT;Final state: total = 150, SUM(amount) = 180. Every ACID property held. Atomicity: both transactions committed in full. Isolation: READ COMMITTED permits exactly this interleaving. Durability: both commits survived. Consistency: no declared constraint was violated. The invariant broke anyway.
What would have caught it: declare the rule where the database can see it. A single-row CHECK (total >= 0) makes the offending transaction abort with SQLSTATE 23514, and atomicity rolls back the whole transaction, line insert included. That is consistency doing its job. A cross-row invariant can't be declared, so you either lock (SELECT total FROM orders WHERE id = 1 FOR UPDATE before the read-modify-write, serialising the two transactions) or run at SERIALIZABLE, where PostgreSQL's SSI aborts one with SQLSTATE 40001 and the application retries.
The second confusion is the letter itself: the C in ACID is not the C in CAP. CAP's C is about distributed replicas agreeing on a value; ACID's C is about declared constraints on one node. They share a letter and nothing else.
Runner-up: durability. It is a configuration, not a constant. With synchronous_commit = off, commit returns before the WAL is flushed, so a crash can lose the last few hundred milliseconds of acknowledged transactions. With asynchronous replication, a commit acknowledged on the primary is gone if the primary dies before the replica receives it. "Durable" means durable under the settings you actually run.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.