Serializable Isolation
ELI5 — The Vibe Check
Serializable isolation is the strictest mode where the database pretends all transactions run one after another, even though they're actually concurrent. If two transactions would cause a conflict, one gets rolled back and told to try again. It's the safest but slowest, like a single-lane bridge where only one car crosses at a time.
Real Talk
Serializable is the highest SQL isolation level, guaranteeing that concurrent transaction execution produces results identical to some serial ordering. PostgreSQL implements Serializable Snapshot Isolation (SSI), which detects potential serialization anomalies and aborts offending transactions rather than using locks. It prevents all concurrency anomalies including write skew.
Show Me The Code
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
BEGIN;
-- Both transactions read and write based on same data
SELECT SUM(balance) FROM accounts;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
COMMIT;
-- If conflict detected: ERROR: could not serialize access
When You'll Hear This
"Use serializable isolation for financial transactions where correctness is non-negotiable." / "Serializable might abort your transaction, so always have retry logic."
Related Terms
Read Committed
Read Committed is the default isolation level in PostgreSQL.
Snapshot Isolation
Snapshot isolation gives each transaction a frozen-in-time photo of the database.
Strong Consistency
Strong consistency means the moment you write something, everyone everywhere immediately sees the updated value. No 'give it a sec' nonsense.
Two-Phase Commit
Two-phase commit (2PC) is like a wedding ceremony for distributed transactions. Phase 1: 'Do you all agree to commit?' Every node says yes or no.