Transactions and ACID
Table of Contents
Introduction: The Partial Update That Cost Me a Weekend
What Is a Transaction?
ACID Properties Explained
Atomicity: All or Nothing
Consistency: Valid State to Valid State
Isolation: Concurrent Transactions Don't See Each Other's In-Progress Work
Durability: Committed Data Survives Crashes
Basic Transaction Syntax
Transaction in psql
Savepoints: Partial Rollbacks
Transaction Isolation Levels
Isolation Level
Dirty Read
Non-Repeatable Read
Phantom Read
Setting Isolation Level
When to Use Each Level
Concurrency Problems and How Isolation Levels Solve Them
Dirty Read
Non-Repeatable Read
Phantom Read
Locking in PostgreSQL
Row-Level Locks
Table-Level Locks
Deadlocks: What They Are and How to Prevent Them
Preventing Deadlocks
Practical Transaction Patterns
Publishing a Post (Atomically)
Safe Counter Increment (No Race Condition)
Transfer/Move Between Entities
Advisory Locks for Application-Level Coordination
Two-Phase Commit (Distributed Transactions)
Common Transaction Mistakes
1. Transactions That Are Too Long
2. Forgetting to Handle Errors
3. Auto-Commit Confusion
What I Learned About Transactions
Next Steps
Last updated