What is ACID?
ACID is four guarantees of a transaction: atomicity, consistency, isolation and durability. Atomicity means all changes are applied or none, consistency that the database moves between states that obey the declared rules, isolation that concurrent transactions do not interfere with each other, durability that after COMMIT the changes survive a failure. Rules that are not declared are guaranteed by the application, and ACID does not span microservices.
SQL · Changing data and transactions
The bank confirmed the transfer and the server crashed right after. Will the transfer vanish?
ACID is often learned as four words. It is more useful when each letter has a concrete situation where it works and a concrete check. This lesson walks one transfer through four failure moments and shows what the database cannot guarantee.
How it works and why.
- Atomicity: all changes of a transaction are applied or none. In PostgreSQL the changes of an unfinished transaction simply remain invisible as uncommitted row versions.
- Consistency: a transaction takes the database from one state that obeys the declared rules to another. The database checks what is declared: PRIMARY KEY, UNIQUE, FOREIGN KEY, CHECK, NOT NULL. Rules that are written nowhere (for example "the sum over all accounts does not change in a transfer") are guaranteed only by the application.
- Isolation: concurrent transactions do not interfere with each other within the chosen isolation level (a separate lesson).
- Durability: after
COMMITthe changes survive a failure. PostgreSQL flushes the write-ahead log (WAL) to disk before confirming the commit.
Example: where exactly a transfer can fail. A transfer of 100 between accounts consists of two UPDATEs and a COMMIT. What happens depends on the moment of the failure:
| Moment of failure | What the user sees after the restart | Who guarantees |
|---|---|---|
After the debit, before COMMIT | Both accounts unchanged | Atomicity |
| The credit violated CHECK or FOREIGN KEY | An error, both accounts unchanged | Atomicity + Consistency |
After COMMIT, an OS crash before the reply to the client | The transfer is there | Durability |
After COMMIT, the reply to the client was lost | The transfer is there, the client does not know. A retry may duplicate | The application: Idempotency-Key |
The last row is the most interesting for QA: it is not a database failure but the edge between the database and the client, where ACID guarantees nothing anymore. The check: cut the connection after COMMIT, repeat the request and see whether two transfers appeared.
Consistency in action (CHECK rejects a negative balance):
UPDATE accounts SET balance = balance - 500 WHERE id = 10;
-- ERROR: new row for relation "accounts" violates check
-- constraint "accounts_balance_check" (shortened)
What the database guarantees and what the application does
Declared rules and the way data is stored
Atomicity, keys, CHECK, durability after COMMIT, isolation by level.
Rules that are not in the schema
A transfer keeps the total sum, a repeated request does not duplicate the action, the reply to the client after commit.
Limits and alternatives. The C in ACID is not the same as consistency in the CAP theorem. ACID works within one database. Between microservices there is no single transaction, so sagas and eventual consistency are used there, and the intermediate states and compensations must be tested. Durability can be loosened on purpose: in PostgreSQL synchronous_commit = off confirms the commit faster, but recent transactions may be lost on a crash (without corrupting the database). Test builds are sometimes tuned for speed, so durability is not checked there.
Common mistakes.
- Assuming consistency guarantees every business rule: the database only knows the declared constraints.
- Mixing C with CAP.
- Treating a 200 as proof that data is committed: the application may have replied before
COMMITor after an error it swallowed. - Testing durability on an environment where
fsyncorsynchronous_commitis switched off for speed. - Expecting ACID across several services.
Check yourself
During a transfer the debit ran and the credit to a non-existent account violated a FOREIGN KEY. What happens to the debit, and which property ensures it?
Answer
The transaction rolls back and the debit is cancelled. That is atomicity: an error at any step cancels all of them. The error itself comes from consistency (FOREIGN KEY).
Why does the database not guarantee on its own that the total amount of money in the system stays the same in transfers?
Answer
Consistency checks only declared rules (keys, CHECK). "The sum is unchanged" is declared nowhere, so an application bug that debits 100 and credits 90 passes every database check. It needs a separate query to verify.