← Back to SQL, page 12

sql-0224 · Easy fresher

Explain each letter of ACID with a concrete example.

Atomicity: a transfer's debit and credit happen together or not at all. Consistency: a transaction moves the database from one valid state to another, respecting constraints such as balance >= 0. Isolation: concurrent transactions do not see each other's partial work, to the degree the isolation level promises. Durability: once COMMIT returns, the change survives a crash.

Full answer
  • Atomicity. The engine logs changes so it can undo an incomplete transaction. A crash halfway through a transfer leaves both accounts unchanged.
  • Consistency. Constraints, foreign keys and triggers are enforced, so a committed state never violates the declared rules. Application-level rules, such as "total money in the system is unchanged", are the application's job, helped by atomicity.
  • Isolation. Each transaction behaves as if it ran with less interference from others. Weaker levels allow specific anomalies in exchange for concurrency; SERIALIZABLE aims for results equivalent to some serial order.
  • Durability. The commit is written to stable storage (a write-ahead log or journal) before success is reported. Settings such as SQLite's PRAGMA synchronous = OFF or PostgreSQL's synchronous_commit = off trade some durability for speed.

A common interview follow-up is that the C in ACID differs from the C in the CAP theorem, which is about replicas agreeing.

Key points

  • Atomicity: all or nothing
  • Consistency: constraints hold after every commit
  • Isolation: controlled visibility between concurrent transactions
  • Durability: committed data survives crashes

Common mistakes

  • Describing consistency as 'data is correct' without mentioning constraints
  • Assuming every isolation level prevents every anomaly

Follow-ups

  • Which anomalies does READ COMMITTED allow?
  • How does a write-ahead log provide durability?

Tips

  • Give one example per letter