ACID

Updated · 7 min read
mediumPostgreSQL 18sqlacidtransactionsatomicitydurabilityinterview

ACID in SQL is the name for the four guarantees a database gives every transaction: Atomicity, Consistency, Isolation and Durability. It is used to describe what makes a database trustworthy for money, inventory or anything where a half-done change would be wrong. Transactions introduced BEGIN, COMMIT and ROLLBACK; this lesson looks at the four guarantees one by one, using the classic example: moving money from one account to another. Srihitha sends Navika $200.00. Either both balances change, or neither does.

In One Minute

  • Atomicity: all of a transaction's changes happen, or none do.
  • Consistency: a transaction moves the database from one valid state to another; constraints hold at every commit.
  • Isolation: concurrent transactions don't see each other's unfinished changes.
  • Durability: once committed, a change survives crashes.
  • PostgreSQL gives all four by default; isolation has levels you can tune.

The Example: A Transfer

Each example below starts with two accounts. Money can't go negative, enforced by a CHECK:

SQL
CREATE TABLE accounts (
  owner       TEXT PRIMARY KEY,
  balance_usd NUMERIC(10,2) NOT NULL CHECK (balance_usd >= 0)
);
 
INSERT INTO accounts VALUES ('Srihitha', 500.00), ('Navika', 100.00);

A transfer is two updates: take from one, add to the other.

Atomicity: All or Nothing

Atomicity means a transaction's changes are applied as one unit. A $200.00 transfer, inside a transaction:

SQL▶ Try it
CREATE TABLE accounts (owner TEXT PRIMARY KEY, balance_usd NUMERIC(10,2) NOT NULL CHECK (balance_usd >= 0));
INSERT INTO accounts VALUES ('Srihitha', 500.00), ('Navika', 100.00);
 
BEGIN;
UPDATE accounts SET balance_usd = balance_usd - 200 WHERE owner = 'Srihitha';
UPDATE accounts SET balance_usd = balance_usd + 200 WHERE owner = 'Navika';
COMMIT;
 
SELECT * FROM accounts ORDER BY owner;
Output
CREATE TABLE
 
INSERT 0 2
 
BEGIN
 
UPDATE 1
 
UPDATE 1
 
COMMIT
 
  owner   | balance_usd
----------+-------------
 Navika   |      300.00
 Srihitha |      300.00
(2 rows)

Explanation: Both updates committed together: Srihitha has $300.00 and Navika $300.00. If the database crashed between the two updates, neither would survive, because nothing is permanent before COMMIT.

Consistency: Rules Hold at Every Commit

Consistency means every committed state obeys the database's rules. A transfer of $700.00 would make Srihitha's balance negative:

SQL▶ Try it
CREATE TABLE accounts (owner TEXT PRIMARY KEY, balance_usd NUMERIC(10,2) NOT NULL CHECK (balance_usd >= 0));
INSERT INTO accounts VALUES ('Srihitha', 500.00), ('Navika', 100.00);
 
BEGIN;
UPDATE accounts SET balance_usd = balance_usd - 700 WHERE owner = 'Srihitha';
UPDATE accounts SET balance_usd = balance_usd + 700 WHERE owner = 'Navika';
ROLLBACK;
 
SELECT * FROM accounts ORDER BY owner;
Output
CREATE TABLE
 
INSERT 0 2
 
BEGIN
 
ERROR:  new row for relation "accounts" violates check constraint "accounts_balance_usd_check"
DETAIL:  Failing row contains (Srihitha, -200.00).
 
ERROR:  current transaction is aborted, commands ignored until end of transaction block
 
ROLLBACK
 
  owner   | balance_usd
----------+-------------
 Navika   |      100.00
 Srihitha |      500.00
(2 rows)

Explanation: The CHECK refused the first update, the transaction was aborted, and ROLLBACK ended it. Both balances are exactly as before, and the total money, $600.00, is unchanged. The database never stored a state where the rule was broken. Note that consistency depends on the rules you declare: without the CHECK, the negative balance would have been "consistent".

200.00 moves completely; 700.00 breaks the CHECK and nothing moves; the total stays 600.00

Isolation: No Peeking at Unfinished Work

Isolation means a transaction doesn't see another transaction's changes until they're committed. While the transfer is between its two updates, Srihitha's balance is already lower but Navika's isn't higher yet. Another session totaling all balances at that moment would see $400.00 instead of $600.00, money that seems to vanish. Isolation prevents that: other sessions keep seeing the old balances until COMMIT, then see both changes at once.

Try it runs a single session, so this can't be shown there; the picture shows what two sessions would see on a real server:

Session A is between its two updates; session B still sees 500.00 and 100.00 until A commits

How much isolation, and at what cost, is the subject of Isolation Levels.

Durability: Committed Means Permanent

Durability means that once COMMIT returns, the change will survive a crash or power cut. PostgreSQL writes the change to its write-ahead log and flushes it to disk before reporting success, as Transactions explained. After a crash, it replays the log. A setting called synchronous_commit can trade a little durability for speed, but the default keeps the full guarantee.

PropertyProtects againstPostgreSQL mechanism
AtomicityHalf-finished changesTransactions, ROLLBACK
ConsistencyBreaking the rulesConstraints, checked on every change
IsolationSeeing others' unfinished workMVCC snapshots and locks
DurabilityLosing committed work in a crashWrite-ahead log, flushed at commit
ACID in Four Letters

Common Mistakes

Doing a transfer as two separate statements. A failure between them loses or creates money.

Relying on application checks for consistency. Declare the rule as a constraint, so every path obeys it.

Assuming isolation means no anomalies at all. The default level still allows some; see Isolation Levels.

Turning off durability settings for speed without understanding them. A crash could lose recent commits.

Interview Corner

What does ACID stand for?

Atomicity (all or nothing), Consistency (constraints hold before and after), Isolation (concurrent transactions don't interfere) and Durability (committed changes survive failures).

Give an example of atomicity.

A money transfer: the debit and the credit happen together in one transaction, or neither happens if anything fails.

How does a database guarantee durability?

By writing changes to a write-ahead log and flushing it to stable storage before acknowledging the commit, then replaying it after a crash.

Practice

Challenge: Transfer $50.00 from Navika to Srihitha in a transaction, then show the balances and confirm the total is still $600.00.

Show solution
SQL▶ Try it
CREATE TABLE accounts (owner TEXT PRIMARY KEY, balance_usd NUMERIC(10,2) NOT NULL CHECK (balance_usd >= 0));
INSERT INTO accounts VALUES ('Srihitha', 500.00), ('Navika', 100.00);
 
BEGIN;
UPDATE accounts SET balance_usd = balance_usd - 50 WHERE owner = 'Navika';
UPDATE accounts SET balance_usd = balance_usd + 50 WHERE owner = 'Srihitha';
COMMIT;
 
SELECT owner, balance_usd FROM accounts ORDER BY owner;
SELECT sum(balance_usd) AS total_money FROM accounts;
Output
CREATE TABLE
 
INSERT 0 2
 
BEGIN
 
UPDATE 1
 
UPDATE 1
 
COMMIT
 
  owner   | balance_usd
----------+-------------
 Navika   |       50.00
 Srihitha |      550.00
(2 rows)
 
 total_money
-------------
      600.00
(1 row)

Navika has $50.00 and Srihitha $550.00, and the total is unchanged: money moved, none was created or lost.

Quick check: ACID

3 questions
1A transfer's debit succeeds and its credit fails inside a transaction. What happens to the debit?
2Which property is enforced by CHECK and foreign key constraints?
3Which property makes a committed change survive a power cut?

Key Takeaways

You wantUse
All or nothingBEGIN; ... COMMIT;
Rules that always holdCHECK, FOREIGN KEY, NOT NULL, UNIQUE
Others not to see half-done workTransactions (isolation is automatic)
Survive crashesCOMMIT (durability is the default)

Was this tutorial helpful?

Lesson 75 of 115