Theory · Page 21 of 34 · Platforms and data in motion

21. Distributed data: CAP, PACELC and consistency models

As soon as data is kept on more than one machine, copies can disagree. CAP says that when the network splits, a system must choose between answering and agreeing; PACELC adds that even without a split it trades latency for consistency. Consistency models name the guarantees in between, and replication and partitioning are the two ways data is spread.

Two replicas split by a partition, choosing between answering and agreeing
Playback speed

The clip keeps a customer's address on two replicas, one in Amsterdam and one in Warsaw. A write in Amsterdam is copied to Warsaw, and both answer the same. Then the network between them fails. The customer changes her address in Amsterdam; a broker in Warsaw asks for it. The system can refuse to answer until the replicas agree (consistent, not available) or answer with the old address (available, not consistent). The link returns and the replicas converge. Then, with no failure at all, the clip shows the everyday trade: wait for both replicas before confirming a write (slower, consistent) or confirm at once (faster, briefly stale).

The intuition: two offices, one phone line

Harbour Mutual keeps customer records in two data centres, one near Amsterdam and one near Warsaw, so that either can serve its country quickly and the other can take over if one fails. Every change in one is copied to the other. Most of the time nobody notices that there are two copies.

One afternoon the line between them fails. A customer phones the Dutch office to change her address; ten minutes later her broker in Poland looks her up. The Polish copy has not heard of the change. The system has two honest options: tell the broker "I cannot answer right now", or give the address it has, knowing it may be old. There is no third option that is both immediate and certainly right, because the information that would make it right is on the other side of the broken line.

Remember When copies cannot talk, a system must choose to answer or to agree; when they can, it still chooses between fast and in step.

The theory: CAP, PACELC, consistency models, replication and partitioning

Brewer's CAP conjecture, proved by Gilbert and Lynch, concerns three properties: consistency (in the strong sense of linearisability: every read sees the latest write, as if there were one copy), availability (every request to a working node gets an answer) and partition tolerance (the system keeps working when messages between nodes are lost). Since networks do fail, partition tolerance is not optional; the theorem says that during a partition a system must give up either consistency or availability. It says nothing about the system when the network is healthy.

Abadi's PACELC fills that gap: if there is a Partition, choose between Availability and Consistency; Else, choose between Latency and Consistency. Keeping replicas in step means waiting for them before answering, which costs time on every request, not only during failures. Systems are described by both halves: a store that serves stale reads to stay fast and stays up in a partition is PA/EL; one that waits for agreement in both cases is PC/EC.

Consistency models name what a reader may see. Strong consistency (linearisability) behaves like a single copy. Sequential consistency keeps one order of operations for everyone, not necessarily real time. Causal consistency keeps the order only of operations that depend on each other: a reply is never seen before the message it answers, while unrelated writes may appear in different orders. Eventual consistency promises only that replicas converge once writes stop. Between them sit session guarantees such as read-your-writes (you always see your own changes) and monotonic reads (you never see time go backwards).

Replication keeps copies of the same data on several nodes, for availability and for reading close to the user. With a leader, all writes go to one node that sends them to followers, synchronously (consistent, slower) or asynchronously (faster, followers lag). With several leaders or none (each node accepts writes), conflicts must be resolved, by last-writer-wins, by merging, or by asking. Quorums generalise the choice: with N replicas, a write waits for W of them and a read asks R of them; if R + W > N, every read overlaps the latest write.

Read and write quorums on three replicas
Read from (R)Write to (W)A read always overlaps the latest write?Reads survive losingWrites survive losing
11no2 replicas2 replicas
13yes2 replicas0 replicas
31yes0 replicas2 replicas
22yes1 replica1 replica
12no2 replicas1 replica

Partitioning (or sharding) splits data so each node holds a part: by range of a key (customers A to K, L to Z), which keeps neighbours together but can create hot spots, or by a hash of the key, which spreads load evenly but scatters ranges. A partitioned and replicated system does both: each part is replicated, and a query that needs many parts must gather from many nodes. In a lakehouse the same words describe files: partitions are folders, and replication is the storage service's copies.

Clocks do not settle it It is tempting to order writes by their timestamps. But clocks on different machines drift, so "last writer wins" by wall-clock time can discard the write that really came last. Lamport showed how to order events by what each node has seen (logical clocks); systems that need real-time order use synchronised clocks with known error bounds and wait them out.

Why it matters

Every cloud service in the Azure platform of chapter 28 makes these choices for you, and lets you change some: a storage account's redundancy, a database's consistency level, a replica's synchronous or asynchronous mode. Knowing the trade turns a settings page into a design decision: the board's figures need consistency, a product catalogue can be eventually consistent.

They also explain the integration patterns of page 22: every copy made by a pipeline is an asynchronously replicated, eventually consistent copy, with a lag someone should know.

Where it goes wrong

  • "We chose CA." Partitions happen whether chosen or not; the real question is what the system does then.
  • Eventual consistency without a bound. "Eventually" with no measured lag is a promise nobody can check.
  • Wall-clock conflict resolution. Drifting clocks make last-writer-wins lose real changes.
  • Reading your own write from a lagging follower. A user saves and then sees the old value; route their reads to the leader.
  • Hot partitions. A range key on dates sends every new write to the same node.

Check yourself

Choose an answer to each question, then press Check.

1. What does CAP say a system must give up during a network partition?
2. What does the "ELC" in PACELC add?
3. With N = 3 replicas, which quorum guarantees that every read overlaps the latest write?
Answer every question first.

Sources

The works this page draws on, summarised in its own words rather than quoted. Read them for the full argument.

  • E. A. Brewer, "Towards Robust Distributed Systems", keynote at the ACM Symposium on Principles of Distributed Computing (PODC), 2000The CAP conjecture: consistency, availability and partition tolerance.
  • S. Gilbert and N. Lynch, "Brewer's Conjecture and the Feasibility of Consistent, Available, Partition-Tolerant Web Services", ACM SIGACT News 33(2), 2002The proof, with consistency as linearisability.
  • D. J. Abadi, "Consistency Tradeoffs in Modern Distributed Database System Design", IEEE Computer 45(2), 2012PACELC: the latency and consistency trade when there is no partition.
  • M. Kleppmann, Designing Data-Intensive Applications (O'Reilly, 2017), chapters 5, 6 and 9Replication, partitioning, quorums, consistency models and the trouble with clocks.
  • L. Lamport, "Time, Clocks, and the Ordering of Events in a Distributed System", Communications of the ACM 21(7), 1978Ordering events by causality with logical clocks.

Practise it

The chapters that put this theory to work on Harbour Mutual and Kestrel:

The tools to try it in:

  • Platform designer · Build an Azure reference architecture from parts, check it against the requirements, and export its Bicep.

Terms taught here: CAP theoremEventual consistency

An unhandled error has occurred. Reload 🗙

Reconnecting…

Your connection to the Cloud Data Architecture Lab dropped, probably a Wi-Fi blip. Hold on while it reconnects.

The connection could not be restored. The page will reload in a few seconds.