Sub topic 14
Serializability versus linearizability
This chapter assumes you know what linearizability and sequential consistency mean, you have used a relational or key-value database, and you can read short Python. The two words sound alike and are often confused, even in vendor documentation. They describe different things, and a system can have one, both, or neither.
Two different questions
Linearizability and serializability answer different questions.
- Linearizability is about a single object, such as one register or one key. It says that each operation appears to take effect at one instant between its start and its end, and that the order respects real time.
- Serializability is about transactions. A transaction is a group of reads and writes on possibly many objects, treated as one unit. Serializability says the outcome of running transactions concurrently is the same as running them one at a time in some order.
The first is a recency guarantee on single objects. The second is an isolation guarantee across many objects. Serializability says nothing about real time. Linearizability says nothing about multi-object transactions.
| Property | Linearizability | Serializability |
|---|---|---|
| Unit of operation | Single read or write on one object | Transaction over many objects |
| Field of origin | Distributed systems, replication | Databases, concurrency control |
| Ordering rule | Any order consistent with real time | Any order, as long as it is some serial order |
| Real-time guarantee | Yes | No |
| Typical question | "Does my read see the latest write?" | "Can concurrent transactions corrupt each other's invariants?" |
Serializability allows stale order
Serializability only requires that some serial order exists. That order does not have to match the order in which transactions actually finished.
Suppose the initial value of x is 0.
T1: write(x, 1) starts t=0, commits t=2
T2: read(x) starts t=3, returns 0, commits t=4T2 began after T1 committed, yet it saw the old value. This is not linearizable. It is serializable, because the serial order "T2, then T1" produces exactly these results. A database could do this legitimately if, for example, reads are served from a lagging replica but transactions are still isolated from one another.
This is the same history used earlier in the tutorial to show a non-linearizable result. The new point is that a serializable database may produce it.
Linearizability does not isolate transactions
The reverse also holds. Imagine a store that gives every single key linearizable reads and writes but has no transactions. Two clients move money between accounts a and b.
The invariant is that a + b stays at 100. A client reads both balances, then writes both. With no isolation, two such clients can interleave their reads and writes. Every individual operation is linearizable, and the invariant still breaks.
The code below simulates one such interleaving on a dictionary where each operation is atomic on its own.
store = {"a": 50, "b": 50}
# Client 1 wants to move 10 from a to b.
# Client 2 wants to move 20 from a to b.
# Each client reads both values first, then writes.
c1_a, c1_b = store["a"], store["b"] # reads 50, 50
c2_a, c2_b = store["a"], store["b"] # reads 50, 50
store["a"], store["b"] = c1_a - 10, c1_b + 10 # writes 40, 60
store["a"], store["b"] = c2_a - 20, c2_b + 20 # writes 30, 70
print(store, store["a"] + store["b"]) # {'a': 30, 'b': 70} 100The total survives here, but the result is wrong: 30 was moved in total, yet a lost only 20. Client 1's update to a was overwritten. This is a lost update, and it is a transaction-level anomaly. Linearizability on each key cannot detect or prevent it, because each single read and write behaved correctly. Serializability would prevent it, because it would force the two transfers to behave as if one ran fully before the other, leaving a at 20 and b at 80.
A related anomaly is write skew. Two transactions each read an overlapping set of objects, then write to different objects, and together they violate an invariant that each transaction checked on its own. Weaker isolation levels such as snapshot isolation allow write skew. Serializability does not.
Strict serializability
You can ask for both properties at once. Strict serializability (also called strong serializability or external consistency) requires that transactions behave as if executed one at a time in a serial order that also respects real time. If transaction T1 commits before T2 starts, T1 comes before T2 in the order.
Put differently, strict serializability is serializability plus the real-time constraint from linearizability, applied to whole transactions instead of single operations. A linearizable object is the special case where every transaction is a single operation.
The relationship between the models can be drawn as a small hierarchy.
How systems provide each
The mechanisms differ because the problems differ.
- Linearizability in a replicated system usually comes from consensus or a single leader with synchronous replication. Reads either go through the leader's log or are confirmed with a quorum. Consensus was covered in the previous chapter.
- Serializability within one database usually comes from two-phase locking, optimistic concurrency control with validation, or serializable snapshot isolation. These control which transactions may overlap. They do not require replication.
- Strict serializability across machines needs both. Google Spanner is a well-known example. It runs transactions with locking and two-phase commit across consensus groups, and it uses synchronized clocks with bounded uncertainty so that commit order matches real time. FoundationDB and CockroachDB also aim for strong guarantees, with differences in how they treat clock uncertainty and in what exact guarantee they document.
A single-node database can be serializable and trivially linearizable at once, since there is only one copy. The two properties diverge once data is replicated or reads are served from stale snapshots.
Gotchas and common errors
- Assuming "serializable" means "up to date." A serializable read may return an old value if the serial order places the reader before the writer. If you need recency, you need the real-time constraint.
- Assuming "linearizable" means "transactional." A linearizable key-value store gives no atomicity across keys. Compare-and-set on one key is not a multi-key transaction.
- Trusting the default isolation level. Many databases default to read committed or snapshot isolation, both weaker than serializable. Write skew can occur under snapshot isolation. Check the setting, and check what the vendor means by it.
- Treating vendor labels as proof. Some products call snapshot isolation "serializable." Testing, covered in the next chapter, is the way to verify a claim.
- Ignoring cost. Strict serializability needs coordination on most operations. That raises latency, which connects back to PACELC, and it lowers availability during partitions, which connects back to CAP.
Deciding which you need
Ask what could go wrong for your application.
- If the risk is a stale read of one item, such as a lock, a leader record, or a unique username, you need linearizability on that item.
- If the risk is a broken invariant across several items, such as a balance transfer or a seat booking, you need serializable transactions.
- If both risks apply, such as a payment system where a later transaction must see an earlier committed one, you need strict serializability.
- If neither applies, weaker models are cheaper and usually enough.
Exercise
A database runs transactions at serializable isolation, but its read-only transactions are served from a replica that lags the primary by a few seconds. A user commits a deposit, and one second later starts a read-only transaction that does not show the deposit. Is the database violating serializability? Is it violating linearizability or strict serializability?
Show answer
It is not violating serializability, because the result is equivalent to a serial order in which the read-only transaction runs before the deposit. It does violate strict serializability and linearizability-style real-time ordering, because the read started after the deposit committed yet did not see it. To fix it, read from the primary or use a mechanism that waits for the replica to catch up to the commit time.
Exercise
Each key in a store is linearizable, but there are no multi-key transactions. Two clients each read keys p and q, check that at least one of them is set to "on-call", and then each sets one different key to "off". Both started with p and q both "on-call". What anomaly can occur, and does per-key linearizability prevent it?
Show answer
Write skew can occur. Each client sees both keys "on-call", so each believes it is safe to turn one off. Each writes a different key, and the final state has both off, which violates the invariant that someone stays on call. Per-key linearizability does not prevent this, because every individual read and write is correct. Serializable transactions would prevent it.