ACID: atomicity and durability
medium 26 minA transaction turns several changes into one: all of them happen or none do, and once committed they survive a crash.
See it run
All or nothing, and once committed it stays: how a log written first keeps both promises through a crash.
Operations
A bank transfer
Alice sends Bob $100 inside one transaction. The total stays $1,500 before and after.
Step 1 of 6
Begin
| value | |
|---|---|
| Alice | $1000 |
| Bob | $500 |
| total | $1500 |
| log | BEGIN 42 |
Total $1,500
Transaction 42 starts. Alice has 1,000 and Bob has 500.
Step 1: Begin
def transfer(db, a, b, amount): with db.transaction(): if db.debit(a, amount) == 0: # no row with enough money raise InsufficientFunds if db.credit(b, amount) == 0: # no such account raise NoSuchAccount # leaving the block commits; an error rolls back- Disk waits
- 1 flush at commit
- After a crash
- All or nothing
- Two separate updates
- a crash between them loses $100
Check the number of changed rows on both sides. A credit to an account that does not exist changes no rows and raises no error.
In the real world
The same acid: atomicity and durability doing real jobs. Pick one and step through it.
A checkout touches a card service and a database. One database transaction cannot cover both.
Step 1 of 3
Charge, then crash
Paid, no order
The card is charged $299 and stock falls from 5 to 4. The server dies before the order is written.
Step 1: Charge, then crash
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Two updates, no transaction
criticalEach statement commits on its own. A crash between them keeps the first and loses the second.
Wrongdb.execute("UPDATE ... balance - 100 ... id = 1")db.execute("UPDATE ... balance + 100 ... id = 2")Rightwith db.transaction(): db.execute("UPDATE ... balance - 100 ... id = 1") db.execute("UPDATE ... balance + 100 ... id = 2")Calling another service inside
criticalThe database cannot roll back an email or a card charge. It also holds its locks while the network is slow.
Wrongwith db.transaction(): save(order) cards.charge(order)Rightwith db.transaction(): save(order) outbox.add("charge", order.id)# a worker charges, with an idempotency keyIgnoring the row count
majorAn UPDATE that matches no row is not an error. Money leaves one account and arrives nowhere.
Wrongcredit(bob, 100)commit()Rightif credit(bob, 100) != 1: raise NoSuchAccountTurning off the flush for speed
majorThe reply comes before the data is safe. After a crash, customers hold receipts for payments you do not have.
Wrongfsync = offRight# keep fsync on; batch commits instead# asynchronous commit only for data you can lose
Interview tips
How to explain it
- Atomicity: all of it or none of it.
- Consistency: the rules hold before and after.
- Isolation: others do not get in the way.
- Durability: committed stays after a crash.
Memory trick
- Log first, data later.
- COMMIT keeps everything, ROLLBACK undoes everything.
- No COMMIT record means it did not happen.
Reach for it when
- Two rows must change together.
- Money, stock or seats are involved.
- A retry must not leave a half-done state.
Do not forget
- One transaction covers one database.
- Keep it short.
- Retry on deadlock and serialization errors.
- A disk can die: keep a second copy.