Skip to the guide

Transactions and ACID

IntermediateConcept3 pages

A transaction groups several reads and writes so they act as one: all of them happen or none do, and other users never see the half-done state. Interviewers expect a transaction around every multi-step change to money, stock or bookings, and they probe two things: what each letter of ACID really promises, and what you leave out of the transaction.

A vending machine

You put $2 into a vending machine and press B4. Either the snack drops and the machine keeps your money, or the snack jams and you get your $2 back. You never lose the money without the snack, and nobody gets a free one. The machine's count of snacks always matches what's actually inside. If two people want the last bag, the machine serves one sale at a time. And once the snack has dropped, a power cut can't take it back.

  • the machine → the database, each buyer's sale → a transaction, and the last bag two people want → a hot row
  • money and snack together, or neither → atomicity
  • the count always matches the contents → consistency: the rules hold before and after
  • one sale at a time → isolation: each transaction acts as if it were alone
  • the snack stays dropped → durability: once committed, it survives a crash