How it works
Moving $50 from Ana to Ben takes two writes: subtract from one row, add to another. If the server crashes between them, $50 has vanished. A transaction groups reads and writes into one unit that either commits (takes effect) or aborts (leaves no trace). ACID is the usual name for what that unit promises, and each letter is narrower than it sounds.
Atomicity means a failure before commit discards all of the transaction’s writes, so after that crash Ana still has her $200 and the application can safely try again. Isolation is the concurrency promise: transactions running together should not disturb each other, to a degree set by the isolation level. Durability means committed data survives a crash, usually because it reached a write-ahead log, an append-only file flushed to disk before the commit is acknowledged. A failed disk with no replica can still lose it.
Consistency is the odd one. It means your invariants hold, such as “transfers neither create nor destroy money.” The database enforces rules you declare, like a unique index or a check that balances stay at or above zero. It cannot know that debiting $50 and crediting $40 is wrong, and it will commit that. Consistency is mostly the application’s job, helped by atomicity and isolation.
These promises cost time: commits wait for log flushes, and conflicting work waits or retries. If the connection drops after you send COMMIT, you cannot tell whether it succeeded, so retried operations need an idempotency key, a unique ID that marks the second attempt as a repeat.
A refund job updates an order row and then writes a ledger row, as two separate autocommit statements. About once a month it crashes between them. A teammate proposes a nightly repair script. What would you do instead, and what would still be unsolved?
Wrap both writes in one transaction, so a crash leaves neither and there is nothing to repair. That does not cover a crash after COMMIT but before the job records that it finished, because the retry would refund again. Give each refund an idempotency key, for example a unique constraint on refund ID in the ledger. If the ledger lives in a different database, one local transaction no longer covers both writes, and you need an outbox (a table of pending work that a relay publishes after commit) or a distributed protocol.
Use this as a guide for self review; written answers are not graded.Keep in mind
- Atomicity is about undoing partial work after a failure, not about concurrency.
- The database enforces only the rules you declare; the rest of consistency is your code.
- A connection lost during COMMIT leaves the outcome unknown, so retries need idempotency.