05 - Transactions & ACID
What is a Transaction?
A transaction is a logical unit of work that consists of one or more database operations. It represents a complete business operation that must succeed or fail as a whole.
Real-World Analogy: Bank Transfer
Transfer $100 from Alice to Bob:
┌─────────────────────────────────────────┐
│ Step 1: Check Alice has $100 │
│ Step 2: Deduct $100 from Alice │
│ Step 3: Add $100 to Bob │
│ Step 4: Record the transaction │
└─────────────────────────────────────────┘
This MUST be atomic - either all steps complete, or none do.
Scenario without transactions:
- Step 2 completes (Alice -$100)
- System crashes before Step 3
- Result: $100 disappeared! 💸
With transactions:
- All steps succeed → Commit (permanent)
- Any step fails → Rollback (undo everything)