How it works
Serializable isolation promises an outcome matching some one-at-a-time order, which removes every anomaly in this chapter. Replay the last of 100 places. Actual serial execution runs transactions one after another, on one thread per partition. The first buyer finishes, and the second counts 100 and is refused. Transactions must be short, often scripts as in Redis, and a slow one stalls everything behind it.
Two-phase locking (2PL) makes readers take shared locks and writers exclusive ones, held until the transaction ends, plus index-range locks to block phantoms. Both buyers lock the range, both try to insert, and the database detects the deadlock and aborts one, which retries and sees 100. One long report can stall writers. MySQL InnoDB and SQL Server use this for their serializable levels.
Serializable snapshot isolation (SSI) runs both on snapshots without blocking, tracks who read what the other wrote, and aborts one when the pattern might not match any serial order. Postgres SERIALIZABLE works this way, reporting SQLSTATE 40001. Heavy contention makes retries waste work. Both 2PL and SSI need a retry loop around the whole transaction.
Postgres and Oracle default to read committed, and MySQL InnoDB to repeatable read. Postgres’s repeatable read is snapshot isolation. InnoDB’s uses a snapshot for plain reads but the latest committed row for updates and locking reads. Oracle’s “serializable” is really snapshot isolation and allows write skew, so test the anomaly you care about. A sensible path: keep the default, guard known risks with atomic updates and constraints, and move cross-row rules to serializable once retries exist.
Your Postgres app has a dozen rules spanning rows: spending limits, seat allotments, quotas. Each has its own lock or counter table, and two recent bugs came from a code path that forgot a lock. Would you change the approach, and what would you check first?
Move those paths to SERIALIZABLE, since SSI protects every rule without each path knowing which lock to take. First add a retry loop for SQLSTATE 40001 around whole transactions, keep external calls out of them, and keep them short. Postgres gives the guarantee only among transactions running at SERIALIZABLE, so a path left at read committed is not covered. Load-test the hot paths, and if aborts climb, keep a counter row or atomic update for the hottest rule.
Use this as a guide for self review; written answers are not graded.Keep in mind
- Serial execution avoids conflicts, 2PL blocks on them, and SSI detects them and aborts.
- Isolation level names differ across engines, so test the anomaly instead of trusting the label.
- Approaches that abort need the application to retry the whole transaction.