Sub topic 2
A consistency model is a contract on histories
This chapter assumes you know what replication is, have used a key-value store or database, and can read short Python. The goal is to define what a consistency model promises, so later chapters can compare models precisely.
From behavior to histories
A replicated system has several copies of the data, and clients talk to different copies. Asking "is it consistent?" is too vague to answer. We need something we can check.
A history is a record of what clients did. Each operation has a start time, an end time, and a result. A consistency model is a contract: it names the set of histories the system is allowed to produce. If the system produces a history outside that set, it has violated the contract.
This framing turns arguments about behavior into questions you can test. You take a recorded history and ask whether the model permits it.
Reading a history
Here is a small history. The initial value of x is 0.
Client A: write(x, 1) starts t=0, completes t=2
Client B: read(x) starts t=3, returns 0B's read began after A's write completed, yet it returned the old value. Whether this is allowed depends on the model:
- Linearizability forbids it. A completed write must be visible to every read that starts later.
- Sequential consistency allows it. It requires one agreed order of operations that respects each client's own order, but it does not tie that order to real time. B's read can be placed before A's write.
- Eventual consistency allows it. Replicas only have to agree once updates stop.
The same history is a bug under one contract and normal behavior under another. That is why you must state the model before judging a result.
What a contract contains
A model usually constrains three things:
- Ordering. Which operations must appear in the same order to everyone.
- Visibility. When a write must become visible to later reads.
- Real time. Whether the order must match the wall-clock order of non-overlapping operations.
Overlapping operations are the key case. If a read starts before a write completes, the two are concurrent, and most models let the read return either value.
Strong and weak models
Models differ in how many histories they allow. A stronger model allows fewer histories, so clients see fewer surprises. A weaker model allows more, which gives the system freedom to be faster or to keep working during failures. The next chapters walk through this range, starting with linearizability.
A stronger model is not automatically better. It costs coordination between replicas, which costs latency and availability. The contract you pick should be the weakest one your application can tolerate.
Checking a history in code
You can encode a very simple rule and test a history against it. This function checks the real-time rule for one write and one read.
def violates_real_time(write_end, read_start, read_value, written_value):
"""True if a read that began after a write finished missed that write."""
return read_start > write_end and read_value != written_value
print(violates_real_time(2, 3, 0, 1)) # True: the stale read from above
print(violates_real_time(2, 1, 0, 1)) # False: the operations overlapThis is only one rule, and it checks a single pair. Real checkers search over many operations at once, which a later chapter covers.
Exercise
A client writes x=5 and the write completes at t=10. Another client starts reading x at t=4 and finishes at t=12, and it returns the old value 0. Does linearizability allow this?
Show answer
Yes. The read started at t=4, before the write completed at t=10, so the two operations overlap. A linearizable system may order the read before the write and return 0. The stale-read rule only applies to reads that start after the write completes.