Notes
All notes

Availability, Consistency and CAP (System Design #3)

The network between your two data-center replicas drops. Both sides are healthy, but they cannot see each other. Now a user writes a record on one side and reads on the other. Do you serve the read (and risk showing stale data), or refuse it (and risk being down)? There is no third option, and that forced choice is the CAP theorem.

CAP is the most quoted and least understood result in system design. This chapter states it precisely, explains what it does not say, and introduces the modern refinement (PACELC) that governs the other 99.9% of the time — when there is no partition.

The theorem, stated precisely

Three properties:

The theorem (Brewer, Gilbert & Lynch): a distributed system cannot guarantee all three simultaneously. Since real networks do partition, P is not optional — so during a partition you must choose C or A.

\[ \text{during a partition: choose } C \text{ (refuse/block) or } A \text{ (serve possibly-stale)} \]

The common shorthand “pick two of three” is misleading. In normal operation you often have C and A. The choice only bites when P happens.

\begin{tikzpicture}[font=\rmfamily, >=stealth,
  c/.style={draw, thick, circle, minimum size=1.5cm, align=center, font=\rmfamily\small},
  a/.style={->, thick, black!50}]
  \node[c, fill=blue!12] (c) at (0,1.6) {Consistency};
  \node[c, fill=green!12] (a2) at (-2.1,-0.8) {Availability};
  \node[c, fill=orange!16] (p) at (2.1,-0.8) {Partition tolerance};
  \draw[a] (c) -- (a2); \draw[a] (a2) -- (p); \draw[a] (p) -- (c);
  \node[font=\rmfamily\small, text=black!60] at (0,-0.9) {you always keep P; during a partition drop C \emph{or} A};
\end{tikzpicture}

What CAP does not say

Four clarifications that separate people who understand CAP from people who quote it:

  1. It is not “throw away one of three forever.” You abandon C or A only while a partition is active; many systems are CP at heart and AP under partition, or vice versa.
  2. Consistency here is linearizability, a very strong guarantee. Weaker consistencies (eventual, causal) are not “C” in CAP but are perfectly valid designs.
  3. “Availability” is total — every node must answer every request. A system where one node is down but the cluster serves is still available under CAP.
  4. Partitions are rare but real. They are caused by network drops, GC pauses, crashed nodes, and cross-region latency. Assume they happen.

CP or AP: the practical read

Choice During a partition Example systems Use when
CP block or error the minority side; stay correct ZooKeeper, etcd, Spanner, HBase money, inventory, locks, config
AP keep serving; reconcile later Cassandra, DynamoDB, Riak, DNS feeds, likes, presence, carts

A useful test: if two users see different versions of this data for a few seconds, is that a bug or an inconvenience? If inconvenience, AP. If money, CP.

PACELC: the other 99.9%

CAP only describes the partition case. PACELC extends it: if there is a Partition, choose A or C; Else (normal operation), choose Latency or Consistency. Even with a healthy network, keeping replicas perfectly consistent costs round trips, and round trips cost latency.

\[ \text{P} \to \text{A or C}, \quad \text{E} \to \text{L or C} \]

A cross-region strongly-consistent write pays tens to hundreds of milliseconds to coordinate; an eventually-consistent write can acknowledge locally and propagate in the background. This is the trade you make every day, partition or not. For example: Spanner is PC/EC (consistent, willing to pay latency); DynamoDB is PA/EL (available and low-latency, eventual by default).

\begin{tikzpicture}[font=\rmfamily, >=stealth,
  b/.style={draw, thick, rounded corners=4pt, minimum width=2.3cm, minimum height=0.75cm, align=center, font=\rmfamily\small},
  a/.style={->, thick}]
  \node[b, fill=orange!16] (p) at (-2.6,0.9) {partition?};
  \node[b, fill=green!12] (a2) at (0.4,1.6) {serve stale\\$\Rightarrow$ AP};
  \node[b, fill=blue!12] (c) at (0.4,0.2) {block/error\\$\Rightarrow$ CP};
  \node[b, fill=gray!12] (n) at (-2.6,-0.9) {no partition};
  \node[b, fill=green!12] (l) at (0.4,-0.6) {low latency\\$\Rightarrow$ EL};
  \node[b, fill=blue!12] (c2) at (0.4,-2.0) {strong consistency\\$\Rightarrow$ EC};
  \draw[a] (p) -- (a2); \draw[a] (p) -- (c);
  \draw[a] (n) -- (l); \draw[a] (n) -- (c2);
\end{tikzpicture}

Availability vs consistency in one product

Consider a bank transfer and a social feed.

The mistake is applying one philosophy everywhere. Big systems are mixed: the payments table is CP, the analytics counter is AP. Design per data type, not per company.

Measuring availability (preview)

Availability is quoted in “nines”. Note the exponential difficulty:

Availability Downtime per year Downtime per day
99% (two 9s) ~3.65 days ~14.4 min
99.9% (three 9s) ~8.8 hours ~1.4 min
99.99% (four 9s) ~52.6 min ~8.6 s
99.999% (five 9s) ~5.3 min ~0.86 s

Moving from three nines to four means cutting downtime by 10×. That is a budget question (Chapter 5 goes deeper). Availability and consistency interact: a CP system that blocks during partitions loses availability, so its measured uptime reflects partition frequency and duration.

Reasoning about it in code

You rarely implement CAP; you choose components with a position. A rough selector:

def choose_store(needs):
    # needs: dict of booleans for a given data type
    if needs.get("must_be_latest") and needs.get("money"):
        return "CP: Spanner / etcd / Postgres with synchronous quorum"
    if needs.get("high_availability") and needs.get("cross_region"):
        return "AP: DynamoDB / Cassandra (eventual), reconcile later"
    if needs.get("single_region"):
        return "CP: Postgres primary/replica (block on failover)"
    return "AP: cache + async replication; accept staleness window"

This function is the whole chapter in miniature: pick the guarantee your data actually needs, and accept the latency or availability cost that follows.

Pitfalls

QQuestionSelf-check

What does CAP actually force you to choose, and when? What does PACELC add?

ASolution

CAP forces a choice between consistency and availability only while a network partition is active; partition tolerance is assumed because networks fail. PACELC adds the normal-case trade: even with no partition, achieving strong consistency costs latency (EL vs EC).

Next: Consistency Patterns (System Design #4) — from strong to eventual, and where each belongs.