DBMS · Transactions

ACID properties

Easyacidtransactions

Interview question

What are the ACID properties of a transaction?

How to answer

The short answer

A transaction is a group of operations that the database treats as one unit of work. ACID is the set of four guarantees that make transactions safe.

Atomicity: all of the operations happen, or none of them do. Moving 500 from account A to account B is two updates. If the power fails after the first, atomicity rolls it back so that money does not vanish.

Consistency: a transaction takes the database from one valid state to another. Rules such as , and CHECK constraints must hold after it finishes. A transaction that would break a rule is rejected.

Isolation: transactions running at the same time do not see each other's half-finished work. The result should look as if they ran one after another. How strictly this is enforced depends on the isolation level.

Durability: once a transaction is committed, it stays committed, even if the server crashes right after. Databases achieve this by writing changes to a log on disk before confirming the commit.

SQL
BEGIN;
UPDATE accounts SET balance = balance - 500 WHERE id = 1;
UPDATE accounts SET balance = balance + 500 WHERE id = 2;
COMMIT;   -- ROLLBACK; would undo both updates

Interview shortcut

> Atomic = all or nothing. Consistent = rules still hold. Isolated = no peeking at unfinished work. Durable = committed means saved.

A follow-up is the difference between ACID and the weaker guarantees that many distributed NoSQL systems offer, often called BASE. ACID favours correctness. BASE favours availability and scale, and accepts that data may be briefly out of date.