0%
Distributed Systems: Consensus, Clocks, and Replication
Foundations
Time and Order
Replication
Consensus
Transactions and Operations
Causal, Session and Eventual Consistency
Linearizability is strong and expensive, and the previous lesson argued that most data does not need it. The question this lesson answers is what weaker guarantees are actually useful, how they differ and how to implement them. It starts with eventual consistency, the weakest common model, then adds session guarantees that fix the most visible anomalies for individual users, and finally covers causal consistency, the strongest model that remains available during network partitions.
What eventual consistency promises
Eventual consistency, popularized by Werner Vogels at Amazon in 2008, promises that if no new updates are made to an object, eventually all replicas will return the same value. That is a liveness property: it says something good will happen eventually. It gives no bound on how long eventually is, no guarantee about what reads return in the meantime and no guarantee about what value replicas converge to. A system that returned random values for an hour and then converged would technically satisfy it. In practice, eventually consistent systems converge within milliseconds to seconds and converge to sensible values, but those properties come from the implementation, not the model.
Anomalies users notice
Because eventual consistency permits almost anything before convergence, users can see confusing behavior. A user updates their profile and the page still shows the old version, because the read went to a lagging replica. A user refreshes a comment thread and a comment that was just there disappears, because the second read went to a replica that had not received it. A reply appears before the message it replies to. A user deletes a photo, then sees it reappear briefly. None of these violate eventual consistency, and all of them generate support tickets. Session and causal guarantees exist to rule out these specific anomalies without paying for linearizability.
Why systems choose it
Eventually consistent systems can accept reads and writes at any replica without coordination, so they stay available during partitions, keep latency low because every operation is local and scale writes across replicas. Dynamo, Cassandra, Riak, DNS, CDN caches and most mobile sync systems are eventually consistent at their core. For data such as shopping carts, social feeds, analytics counters, product catalogs and caches, the occasional stale read is acceptable, and the availability and latency benefits are large.
Convergence needs conflict handling
Eventual consistency requires that replicas converge, which means concurrent writes must be resolved deterministically. As earlier lessons showed, the choices are last write wins, which converges by discarding concurrent writes, keeping siblings, which converges to a set of values for the application to merge, or CRDTs, which merge automatically. A system that resolves conflicts differently on different replicas, for example by preferring the locally received write, never converges and is not eventually consistent at all.
Strong eventual consistency
The CRDT lesson introduced strong eventual consistency: replicas that have received the same set of updates are in the same state, immediately, regardless of order. This is a safety property in addition to the liveness of plain eventual consistency, and it is what CRDTs provide. It does not bound staleness either, since a replica that has not received an update does not reflect it, but it rules out replicas holding different values after seeing the same updates, which makes reasoning far easier.
Measuring eventual
Since the model gives no bound, teams should measure staleness directly. Probabilistically bounded staleness research at Berkeley showed how to predict, from replication latency distributions and quorum settings, the probability that a read returns the latest value a given time after the write. In production, a simple probe writes a timestamp to a key at one replica and repeatedly reads it from others, recording how long until each sees it. Tracking the ninety ninth percentile of that window, and alerting when it grows, turns eventual from a vague promise into an operational metric.
Bounded staleness
Some systems offer bounded staleness as an explicit guarantee: reads may be stale, but by no more than a configured time or number of versions. Azure Cosmos DB offers a bounded staleness level, where reads lag writes by at most a configured number of versions or time interval, and Spanner and CockroachDB offer reads at a timestamp a few seconds in the past that are served locally but are consistent snapshots. Bounded staleness requires coordination to enforce the bound, since a replica must know it is not too far behind, but it gives applications a precise contract.
Eventual consistency in caches
Every cache in front of a database makes the combined system eventually consistent, even if the database itself is linearizable. A cache entry with a time to live of 60 seconds can serve a value up to a minute old. Invalidating on write narrows the window but does not close it, because invalidation messages can be delayed or race with reads that refill the cache. Facebook described this in detail in its paper on scaling memcache, including leases to prevent stale sets after invalidation. Recognizing caches as eventually consistent replicas helps engineers apply the same reasoning and guarantees discussed in this lesson.
Where eventual consistency came from
The idea predates the name. DNS has always been eventually consistent: a record change propagates through caches as their time to live values expire, and for a while different resolvers return different answers. Lotus Notes, Usenet and early mobile sync systems such as Coda and Bayou replicated data optimistically and reconciled later. What Amazon added with Dynamo in 2007 was a clear argument that, for a large class of commercial data, accepting writes everywhere and reconciling later beats refusing writes during failures. The argument was economic: a shopping cart that rejects an add to cart loses money, while a cart that briefly shows a removed item costs almost nothing.
Replication lag in practice
In a healthy single region deployment, asynchronous followers usually lag the leader by a few milliseconds, and cross region replicas by tens to hundreds of milliseconds. Lag spikes during load peaks, large transactions, schema migrations, network congestion and replica restarts, and a replica that was offline can be hours behind when it returns. MySQL and PostgreSQL both expose lag metrics, such as seconds behind source and replay lag, and many outages trace back to an application that silently started reading from a replica minutes behind. Because the anomalies scale with lag, it is the single most important number to monitor in an eventually consistent setup.
Eventual consistency and invariants
Eventual consistency cannot maintain invariants that span replicas. If an account balance must never go negative, two replicas can each accept a withdrawal that is valid locally but together overdraw the account, and nothing in convergence undoes it. Systems either accept occasional violations and compensate afterward, such as an airline overbooking and offering vouchers, or they route the operations that threaten the invariant through coordination. The CALM theorem, which states that programs whose outputs only grow as inputs grow can run without coordination, gives a precise test for which operations are safe under eventual consistency and which are not.
Read repair and anti entropy
Eventual convergence depends on mechanisms that actually move updates between replicas. Read repair fixes divergent replicas when a read notices them, hinted handoff delivers writes that a down replica missed once it returns and background anti entropy compares replicas with Merkle trees and copies missing data. If any of these are disabled or broken, eventual can become never for keys that are rarely read. Cassandra operators learned this the hard way with deleted data resurrecting after tombstones expired before repair ran, which is why scheduled repair within the tombstone grace period is a standard operational requirement.
A quick test for eventual consistency
When evaluating whether eventual consistency is acceptable for a feature, imagine every read returning a value a few seconds old and occasionally a minute old, and ask what breaks. If the answer is a slightly outdated number or list, eventual consistency is probably fine. If the answer is a confused user, a duplicate charge or a security exposure, a stronger guarantee is needed for at least that path.
Eventual consistency only promises that replicas agree once writes stop, with no bound on staleness and no guarantee about intermediate reads. Measure the staleness window, resolve conflicts deterministically and add session or causal guarantees where users would notice anomalies.