Sub topic 14
Serializability versus linearizability
This chapter assumes you know what linearizability is, what a replicated key-value store is, and what a database transaction is. It also assumes you can read short Python snippets. The two terms sound alike and are often confused. They answer different questions.
Two different questions
Linearizability is about single objects and real time. It says each operation on one register appears to take effect at a single instant between its start and its end. This is a recency guarantee.
Serializability is about transactions. A transaction is a group of reads and writes on one or more objects that runs as a unit. Serializability says the outcome of running transactions concurrently equals the outcome of running them one at a time in some order. It says nothing about which order, and nothing about real time.
| Property | Unit | Ordering constraint | Recency guarantee |
|---|---|---|---|
| Linearizability | Single operation on one object | Must respect real-time order | Yes |
| Serializability | Multi-object transaction | Any serial order is acceptable | No |
| Strict serializability | Multi-object transaction | A serial order that respects real time | Yes |
A history that is serializable but not linearizable
Suppose x and y both start at 0. Transaction T1 writes x = 1 and completes at t=2. Transaction T2 starts at t=3 and reads x.
T1: write(x, 1) starts t=0, completes t=2
T2: read(x) starts t=3, returns 0A serializable database may order T2 before T1, because the serial order T2, T1 produces the same results. The database never has to explain why T2 missed a write that had already completed. This is the same history as the first example in this tutorial, so it is not linearizable. As a transaction history, it is serializable.
A history that is linearizable but not serializable
Linearizability checks each object on its own. Take two accounts, each linearizable on its own, and a transfer that is not a single transaction:
accounts = {"a": 100, "b": 100}
def transfer(src, dst, amount):
accounts[src] -= amount # one linearizable write
accounts[dst] += amount # another linearizable write
def audit():
return accounts["a"] + accounts["b"]
transfer("a", "b", 30)
print(audit()) # 200 when run aloneIf audit runs between the two writes, it reads 170. Every individual read and write was linearizable. No serial order of transfer and audit gives 170, so the combined behavior is not serializable. Only a transaction boundary fixes this.
Strict serializability
Strict serializability combines both properties. Transactions behave as if run one at a time, and that order must be consistent with real time. If T1 finishes before T2 starts, T1 comes first in the order.
It is the strongest of the common models, and it is what most people expect from "a normal database". Systems such as Google Spanner provide it. They pay for it with coordination, such as consensus across replicas and clock-uncertainty waits.
Isolation levels in practice
Many databases do not give you serializability by default. Snapshot isolation and read committed are weaker. They permit anomalies such as write skew. Check your database's default isolation level before assuming transactions are serializable.
Exercise
A database runs transactions in an order that is equivalent to some serial order, but a read that starts after a write completes can return the old value. Which property does it have, and which does it lack?
Show answer
It has serializability. It lacks linearizability, and so it is not strictly serializable, because the equivalent serial order does not respect real time.
Exercise
Why does making every single-key read and write linearizable not make a two-account transfer safe?
Show answer
Linearizability applies to each operation on one object separately. A reader can observe the state between the two writes. Atomicity across both keys needs a transaction, and a serializable one if the audit must always see a consistent total.