Sub topic 1
Why Replication Creates Consistency Questions
Prerequisites
This chapter assumes you know how client-server systems communicate over a network, and that you have used a database or key-value store. You should be able to read short Python programs. No earlier chapter is required, because this is the first one.
Why data gets replicated
Replication means keeping copies of the same data on several machines, called replicas. Teams replicate data for three reasons:
- Fault tolerance. If one machine fails, another copy still serves requests.
- Lower latency. A user in Frankfurt can read from a nearby replica instead of one in Virginia.
- Read throughput. Many replicas can answer reads in parallel.
One copy of the data has one value at any moment. Several copies can disagree, and that is where consistency questions come from.
Where the disagreement comes from
A write to one replica takes time to reach the others. Messages can be delayed, reordered or lost, and replicas can crash or be cut off from each other. During that window, two clients asking different replicas about the same key can get different answers.
Nothing is broken in this run. Every replica followed its protocol. The surprise is that Client B read 0 after Client A saw its write succeed.
A small simulation
The following program models two replicas with asynchronous replication. The write is applied locally and queued for the other replica.
class Replica:
def __init__(self, name):
self.name = name
self.data = {"x": 0}
primary = Replica("r1")
follower = Replica("r2")
pending = [] # replication messages still "in flight"
def write(key, value):
primary.data[key] = value
pending.append((key, value)) # delivered later
def deliver_all():
while pending:
key, value = pending.pop(0)
follower.data[key] = value
write("x", 1)
print("read from r1:", primary.data["x"]) # 1
print("read from r2:", follower.data["x"]) # 0 (stale)
deliver_all()
print("read from r2:", follower.data["x"]) # 1The second read is stale, meaning it returns a value that a newer write has already replaced. After deliver_all() the replicas agree again.
The design question
You can reduce staleness by making the write wait until every replica has acknowledged it. That costs latency, and the write stops working if any replica is unreachable. You can instead acknowledge immediately, which is fast and available but allows stale reads. Every replicated system picks a point between these two options.
A consistency model states which results reads and writes are allowed to return under that choice. The next chapter defines this precisely.
Summary
- Replicas exist for fault tolerance, latency and throughput.
- Updates travel over an unreliable network, so replicas can disagree temporarily.
- Waiting for all replicas gives fresher reads but costs latency and availability.
- A consistency model is the rule that says which disagreements clients may observe.
Exercise
In the Python simulation, a client reads from follower before deliver_all() runs. Name the property of the system that allows the stale result, and give one change that would remove the stale read and one cost of that change.
Show answer
The stale result is allowed because replication is asynchronous: the write is acknowledged and readable on the primary before the follower receives it. Making write apply the update to the follower before returning (synchronous replication) removes the stale read. The cost is higher write latency, and the write cannot complete if the follower is unreachable.