Consistency Guarantees: Eventual, Causal and Linearizable

What a read is allowed to return when data has several copies: eventual, session, causal and linearizable consistency, what each costs, and how CAP fits in.

Overview

When a database keeps several copies of the same data (replicas), the copies do not all change at the same instant. A write reaches one replica first and the others a little later. A consistency model is the promise the database makes about what a read can return while that is happening.

The models form a ladder. The weakest useful one, eventual consistency, only promises that the copies agree in the end. The strongest, linearizability, makes the system behave as if there were a single copy. Each step up removes a kind of surprise for the application, and each step up costs more coordination between replicas: more latency, and less ability to keep working when the network breaks.

Why copies disagree

Most replicated databases copy a write to other replicas in the background (asynchronous replication). A client that reads from a replica the write has not reached yet gets the old value. Nothing is broken; the copy is simply behind. The time it is behind is called replication lag.

Here is a stale read, step by step, with one leader and one follower.

  • Client A sends 'write x = 2' to the leader, and the leader answers ok. The write is committed on the leader.
  • Client B sends 'read x' to the follower, which has not received the write yet, and gets x = 1, the old value.
  • The leader replicates x = 2 to the follower.
  • Client B reads x from the follower again and now gets x = 2.

Every model below is a rule about which of these stale reads are allowed.

Key concepts

  • Replication lag
  • Eventual consistency
  • Read your writes
  • Monotonic reads
  • Causal consistency
  • Linearizability
  • CAP theorem
  • Quorum
  • Strict serializability

Interview questions

  • What does the CAP theorem mean in practice when you design a distributed system?
  • A user edits their profile, refreshes, and sees the old version. What is happening and how do you fix it?
  • What is the difference between linearizability and serializability?
  • Why is causal consistency interesting if linearizability is stronger?
  • How would you choose consistency levels for the parts of a social app?

Sign up free to read the full topic

More DDIA topics

Start free on Cariara · Coding problems · System design studio