Consistency Models, CAP, PACELC and Consensus

What consistency means in distributed systems: the spectrum from linearizability to eventual consistency, session guarantees, CAP and PACELC explained correctly, quorums, Raft consensus, clocks and ordering, isolation levels, and how to choose per use case.

On this page

Simple definition#

A consistency model is a promise about what a read can return after writes, when data is copied across machines. Strong models behave like a single copy. Weaker models allow stale or reordered reads in exchange for latency and availability.

The problem#

Replicas live on different machines and in different regions, and messages between them are delayed or lost. A customer transfers money and immediately sees the old balance. Two people book the same seat. Two regions accept conflicting updates. The real question is which anomalies your use case can tolerate. “Is it consistent?” is too vague to answer.

The consistency spectrum#

From strongest to weakest (simplified)
flowchart LR
    L["Linearizable<br/>single-copy, real-time order"] --> S["Sequential<br/>one global order"] --> C["Causal<br/>cause before effect"] --> SESS["Session guarantees<br/>read-your-writes, monotonic"] --> E["Eventual<br/>replicas converge"]
Model Guarantee Example use
Linearizable Every operation appears to happen instantly between its call and return; all clients see the same order Account balance limits, leader election, locks, unique usernames
Sequential A single total order consistent with each client’s program order Some replicated logs
Causal If A could have caused B, everyone sees A before B Comments and replies, chat, collaborative edits
Read-your-writes You always see your own updates User profile updates
Monotonic reads You never see older data after newer data Feeds, dashboards
Eventual If writes stop, replicas converge Counters, recommendations, caches, read models

CAP, stated correctly#

CAP (Brewer, proven by Gilbert and Lynch): when a network Partition happens, a replicated system must choose between Consistency (linearizability) and Availability (every non-failed node answers).

  • It is not “pick two of three”. Partitions are not optional, so the choice is only C or A during a partition.
  • CP systems (etcd, ZooKeeper, Spanner, CockroachDB, Kafka with min.insync.replicas) refuse some requests rather than return wrong data.
  • AP systems (Cassandra and DynamoDB in some configurations, DNS, caches) answer with possibly stale data and reconcile later.

PACELC: the everyday trade-off#

PACELC (Abadi): if there is a Partition, choose A or C; Else, in normal operation, choose Latency or Consistency.

System (typical config) P → E →
Spanner, CockroachDB C C (pays latency for consensus)
Cassandra, DynamoDB (eventual reads) A L
MongoDB (majority writes and reads) C C
PostgreSQL + async replicas C on the primary L for replica reads (stale)

The “else” part matters more day to day: strong consistency across regions costs round-trips.

Quorums#

With N replicas, write to W and read from R. If R + W > N, every read overlaps the latest write.

N W R Effect
3 2 2 Strong-ish reads; tolerates 1 failure
3 3 1 Fast reads, fragile writes
3 1 1 Fast and available, eventually consistent

Quorums alone are not full linearizability (concurrent writes, sloppy quorums, clock-based conflict resolution). Consensus protocols close those gaps.

Consensus: Raft in one picture#

Consensus lets a group of nodes agree on an ordered log despite failures. Raft is the most widely used protocol (etcd, Kafka KRaft, CockroachDB, TiKV, Consul).

Raft: leader replicates a log entry to a majority
sequenceDiagram
    participant C as Client
    participant L as Leader (term 5)
    participant F1 as Follower 1
    participant F2 as Follower 2
    C->>L: write x=1
    L->>F1: AppendEntries(index 42, x=1)
    L->>F2: AppendEntries(index 42, x=1)
    F1-->>L: ok
    L->>L: majority (2 of 3) has it, so commit index 42
    L-->>C: success
    L->>F2: (F2 catches up later)
  • Leader election uses randomised timeouts and terms. A node needs votes from a majority.
  • A majority (quorum) must store an entry before it is committed. Use 3 or 5 nodes, which tolerate 1 or 2 failures.
  • A minority partition cannot commit, which is the CP behaviour.

Time, clocks and ordering#

Wall clocks drift, so “last write wins” by timestamp can silently lose updates.

  • Logical clocks (Lamport) order events; vector clocks detect concurrency.
  • Hybrid logical clocks (HLC) combine physical time with logical counters (used by CockroachDB and YugabyteDB).
  • TrueTime (Spanner) exposes bounded clock uncertainty and waits it out to give external consistency.
  • In event-driven systems, prefer per-entity version numbers and partition ordering over timestamps.

Isolation levels: consistency inside a database#

Level Prevents Still allows
Read committed Dirty reads Non-repeatable reads, lost updates, write skew
Repeatable read / snapshot isolation The above plus non-repeatable reads Write skew (two doctors both go off call)
Serializable (SSI or 2PL) All of the above Nothing, at a performance cost

For money movement, use serializable, or explicit locking or constraints (SELECT ... FOR UPDATE, unique keys, check constraints) on the rows that protect invariants.

Conflict resolution under eventual consistency#

  • Last-writer-wins is simple but lossy.
  • CRDTs (conflict-free replicated data types: counters, sets, maps) merge automatically and deterministically. They suit collaborative and offline-first apps.
  • Application merge: keep both versions and resolve them with business rules or a human.

Choosing: a simple guide#

If you need… Choose
An invariant that must never be violated (balance ≥ 0, unique IBAN) Linearizable or serializable inside one consensus or ACID boundary
A user to see their own change immediately Read-your-writes (route to the leader, or use session tokens)
Global low-latency reads of slowly changing data Eventual consistency with caches and replicas
Multi-region writes to the same entity Consensus-based distributed SQL, or single home region per entity

Common mistakes#

Quick check#

What does CAP actually say? During a network partition, a replicated system must choose between linearizable consistency and availability.

What does PACELC add? Even without a partition, there is a trade-off between latency and consistency.

N=3, W=2, R=2: why does a read see the latest write? R + W > N, so every read quorum overlaps every write quorum.

Why do Raft clusters have 3 or 5 nodes? A majority must agree. Odd sizes maximise tolerated failures (1 of 3, 2 of 5) without adding quorum cost.

What anomaly does snapshot isolation allow? Write skew: two transactions read overlapping data and write different rows, together violating an invariant.

When is eventual consistency fine? For read models, caches, analytics and notifications, where brief staleness does not break an invariant.

Summary#

  • Consistency is a spectrum. Choose per operation.
  • CAP is about partitions; PACELC adds the everyday latency trade-off.
  • Quorums and consensus (Raft) provide agreement; clocks need care.
  • Protect invariants inside one strongly consistent boundary; use eventual consistency elsewhere.

References#