Implementing Serializability
Three ways a database makes concurrent transactions behave as if they ran one at a time: actual serial execution, two-phase locking and serializable snapshot isolation, and what each one costs.
Overview
The goal and the three routes
Serializable isolation promises that the result of running transactions concurrently equals the result of running them in some order, one at a time. It is the only level that rules out every race, including write skew. Databases get there by one of three routes, and they differ in what happens when two transactions conflict.
- Actual serial execution: there is no concurrency at all, so a conflict cannot happen. The main cost is throughput: one thread per partition.
- Two-phase locking (2PL): this route is pessimistic. It blocks conflicts before they happen, so when one occurs, the later transaction waits for the conflicting lock. The main cost is waiting, and deadlocks.
- Serializable snapshot isolation (SSI): this route is optimistic. It lets conflicts happen, detects them and aborts one transaction. The main cost is retries under contention.
The rest of the chapter takes the routes in that order.
Key concepts
- Serializability
- Actual serial execution
- Two-phase locking (2PL)
- Deadlock
- Next-key lock
- Serializable snapshot isolation (SSI)
- Read-write dependency
- SQLSTATE 40001
Interview questions
- When would you choose SSI over 2PL?
- Explain two-phase locking and why it prevents phantoms only with range locks.
- When is actual serial execution a good design?
- How does PostgreSQL implement SERIALIZABLE, and what must the application do?
Sign up free to read the full topic
More DDIA topics
- End-to-End Correctness and the Exactly-Once Myth
- Event Streams and Message Brokers
- Fan-Out and the Twitter Timeline Problem
- Fault Tolerance in Stream Processing
- Faults and Partial Failures
- Graph Data Models
- Isolation Levels
- Joins in Batch Processing
- Joins in Stream Processing
- Lambda and Kappa Architectures
- Leaderless Replication (Dynamo-Style)
- Linearizability
Start free on Cariara · Coding problems · System design studio