Transactions & ACID
Definition
Transaction
- In plain English
- A transaction is a group of database changes that succeed together or fail together.
- Think of it like
- It is like a bank transfer where money can't leave one account without arriving in the other.
ACID
- In plain English
- ACID is a set of four promises a transaction makes: atomic, consistent, isolated and durable.
- Think of it like
- It is like a contract with four clauses that protect your data.
Why it exists
You reserve seat 14C but fail to record its payment. The customer sees a sold seat without a completed booking.
A database needs to treat related changes as one unit. Otherwise a crash can leave only half the intended result.
How it works
Atomicity
- In plain English
- Atomicity means all of a transaction's changes happen, or none do.
- Think of it like
- Like a train that either reaches the station with every carriage or doesn't leave at all.
Consistency
- In plain English
- Consistency means a transaction moves data from one valid state to another, never breaking the rules you declared.
- Think of it like
- Like a referee who rejects any move that breaks the game's rules.
Isolation
- In plain English
- Isolation means transactions running at the same time don't see each other's half-finished work.
- Think of it like
- Like two cooks each using their own chopping board.
Durability
- In plain English
- Durability means once a transaction says "done", its changes survive a crash.
- Think of it like
- Like a signed receipt that stays valid even if the shop's lights go out.
Commit and rollback
- In plain English
- Commit makes a transaction's changes permanent, while rollback throws them all away.
- Think of it like
- Like pressing "send" on an email versus "discard draft".
| Property | Promise | Without it |
|---|---|---|
| Atomicity | All changes or none | Miles leave without arriving. |
| Consistency | Declared rules stay true | A balance becomes negative. |
| Isolation | Concurrent work follows isolation rules | Two buyers take the last item. |
| Durability | Confirmed changes survive a crash | A confirmed booking disappears. |
The app begins a transaction, debits and credits miles, then commits both changes or rolls both back.
Examples
Record a complete booking
You mark seat 14C sold and record its payment in one database transaction.
BEGIN;
UPDATE seats SET sold = TRUE
WHERE seat_id = '14C' AND sold = FALSE;
-- Continue only if exactly one seat row changed.
INSERT INTO payments (booking_id, amount) VALUES (714, 80);
COMMIT;BEGIN;
UPDATE seats SET sold = TRUE
WHERE seat_id = '14C' AND sold = FALSE;
-- If the payment statement fails, the app sends:
ROLLBACK;Recording payment is a database change. Charging an external payment service needs separate coordination; database rollback cannot undo that charge.
The insight
Atomicity prevents half-finished database actions.
Transfer 5,000 miles
You move 5,000 miles from member 1 to member 2. Both accounts must exist before starting.
CREATE TABLE accounts (
member_id INTEGER PRIMARY KEY,
balance INTEGER NOT NULL CHECK (balance >= 0)
);
INSERT INTO accounts VALUES (1, 8000), (2, 1000);
BEGIN;
UPDATE accounts SET balance = balance - 5000 WHERE member_id = 1;
UPDATE accounts SET balance = balance + 5000 WHERE member_id = 2;
COMMIT;The balances become 3,000 and 6,000, preserving the 9,000-mile total. In the app, roll back if either update changes no row.
The insight
Declared rules protect data alongside transaction boundaries.
Protect the last box
Two pharmacists try to dispense the last box at the same time.
Row lock
- In plain English
- A row lock makes conflicting changes to one row wait until the current transaction finishes.
- Think of it like
- It is like holding the only key to a stock cupboard.
Pharmacist A locks the final box, B waits, A commits zero stock, and B sees zero and stops.
Isolation level
- In plain English
- An isolation level specifies how much concurrent transactions can affect what each transaction reads.
- Think of it like
- It is like choosing how many kitchen tasks must wait for another cook to finish.
This uses PostgreSQL’s Read Committed level: reads exclude unfinished changes, and the waiting lock request sees the updated row. Stronger levels can require retrying instead.
The insight
Choose isolation and locking that protect the whole stock decision.
Trade-offs
Pros
- Prevents partially completed changes.
- Protects declared rules during failures.
Cons
- Locks and coordination add work.
- Transactions across machines need extra coordination.
Use when
- Money and inventory must add up.
- Related booking changes must succeed together.
Avoid when
- Independent readings need no grouped changes.
- Approximate counters tolerate occasional missing updates.
Quick check
Takeaways
- A transaction succeeds or fails as a unit.
- ACID covers atomicity, consistency, isolation, and durability.
- Choose transaction boundaries and isolation around the rules you must protect.