Sub topic 7
Eventual consistency and its anomalies
This chapter assumes you know what replication is, that a consistency model is a contract on the histories a system may produce, and that you can read simple Python. Earlier chapters covered linearizability and sequential consistency. Here we look at the weakest widely used contract.
What eventual consistency promises
Eventual consistency says: if no new writes arrive, all replicas will eventually hold the same value. That is the whole guarantee. It says nothing about how long "eventually" takes. It also says nothing about what a read returns in the meantime.
A write is acknowledged by one replica, then copied to the others in the background. Reads can go to any replica, so they are fast and keep working during a partition. The price is that clients can observe stale or inconsistent data.
Common anomalies
Because the contract is weak, several anomalies are allowed.
| Anomaly | What the client sees |
|---|---|
| Stale read | A read returns a value older than a completed write. |
| Read-your-writes violation | You write, then read from another replica and do not see your own write. |
| Non-monotonic reads | A read returns a newer value, and the next read returns an older one. |
| Out-of-order visibility | A reply appears before the post it answers, because the two writes replicate independently. |
| Conflicting writes | Two clients write the same key on different replicas, and the replicas disagree until the conflict is resolved. |
Later chapters show how session guarantees, causal consistency and conflict resolution remove some of these.
Simulating a stale read
The following program models two replicas and a delayed replication queue. It runs as shown.
class Replica:
def __init__(self):
self.data = {}
replica1, replica2 = Replica(), Replica()
pending = [] # replication messages not yet delivered
def write(key, value):
replica1.data[key] = value
pending.append((key, value))
def deliver_all():
while pending:
key, value = pending.pop(0)
replica2.data[key] = value
replica2.data["x"] = 0
write("x", 1)
print(replica2.data["x"]) # 0, stale read
deliver_all()
print(replica2.data["x"]) # 1, convergedThe first read is stale. After delivery, both replicas agree, which is exactly the eventual guarantee.
When it is a good fit
Eventual consistency suits data where brief staleness is harmless and availability or low latency matters more. Examples are view counters, shopping-cart suggestions, DNS records and social feeds. It is a poor fit for operations such as debiting an account balance or enforcing a unique username, where a stale read can cause a wrong action.
Exercise
Client A writes x=1 to replica 1. Client B then reads x from replica 2 and gets 0. Later B reads x from replica 2 again and gets 1. Does this violate eventual consistency? Which stronger model does it violate?
Show answer
It does not violate eventual consistency, because the replicas converged once replication completed. The first read violates linearizability, since it began after the write completed and returned the old value.
Exercise
Replica 1 holds x=5 and replica 2 holds x=3 after concurrent writes, and no further writes occur. Is the system eventually consistent as long as it keeps these values?
Show answer
No. Eventual consistency requires all replicas to converge to the same value once writes stop. Replicas that keep different values need a conflict resolution rule to converge.