CodePark/ questions / Transactions & ACID
START PRACTICINGPRACTICE
Backend question · Databases & SQL › Transactions & ACID

What does ACID stand for? Explain each property with a money transfer example.

ACID stands for Atomicity, Consistency, Isolation and Durability. Take a transfer that debits one account and credits another.

Atomicity: the transaction happens completely or not at all. If the credit fails after the debit, the database rolls back the debit.

Consistency: a transaction moves the database from one valid state to another. After commit, constraints still hold, such as:

  • foreign keys
  • CHECK (balance >= 0)
  • uniqueness

Business rules that are not declared as constraints are the application's job.

Isolation: concurrent transactions do not see each other's unfinished work. How strict this is depends on the isolation level, from Read Committed to Serializable.

Durability: once COMMIT returns, the transfer survives a crash. PostgreSQL does this by writing the write-ahead log (WAL) to disk before it reports the commit. The WAL is a log of every change.

  • Atomicity means all or nothing.
  • Consistency means constraints still hold after commit.
  • Isolation means concurrent transactions do not interfere, as far as the isolation level allows.
  • Durability means committed data survives a crash, thanks to the WAL.