CAP and PACELC

Topic 4 of 9 in Week 5: Distributed systems and service communication9 min readIntermediateFree course

Overview

The CAP theorem is the most quoted idea in distributed systems and one of the most misunderstood. This lesson explains what it actually says, why “pick two of consistency, availability and partition tolerance” is the wrong way to remember it, and the more useful PACELC framing.

What CAP says

  • Consistency (C): here it means linearizability: every read sees the latest write, as if there were one copy.
  • Availability (A): every request to a non-failed node gets a (non-error) response.
  • Partition tolerance (P): the system keeps working when the network between nodes drops messages.

The theorem: when a network partition happens, a system can’t provide both C and A. It must choose.

CAP during a network partition. Client 1 writes x equals 2 to replica A, but a network partition stops replica A from reaching replica B, which still has x equals 1. Client 2 asks replica B. A CP system refuses or waits: B returns an error until it can confirm x. An AP system answers anyway: B returns x equals 1, which is stale, and the replicas reconcile later. PACELC: if partitioned, choose availability or consistency; else, in normal operation, choose latency or consistency. Examples of PC/EC systems are Spanner, etcd and ZooKeeper; examples of PA/EL systems are Cassandra and DynamoDB with default reads. Partitions are rare but certain; most of the time the real trade-off is latency versus consistency.

Why “pick two” is misleading

You can’t choose to give up partition tolerance: networks will partition (a failed switch, a cut cable, a misconfigured firewall, a long garbage-collection pause that looks the same). So the real question is how the system behaves when it happens:

  • CP: keep consistency. Nodes that can’t confirm they have the latest data refuse requests or wait. Some users see errors or timeouts, but nobody sees wrong data.
  • AP: keep availability. Every node keeps answering with what it has. Users may see stale data or make conflicting writes, which must be reconciled when the partition heals.

When there’s no partition, a well-built system can be both consistent and available.

Choosing CP or AP

Ask: what’s worse for this operation, an error or a wrong answer?

  • CP for: money movement, inventory decrements at checkout, unique usernames, locks and leader election, configuration that must be agreed. Systems: relational primaries with synchronous failover, etcd, ZooKeeper, Consul, Spanner.
  • AP for: shopping carts, social feeds, likes, presence, product browsing, DNS. Systems: Cassandra and DynamoDB at default settings, DNS, CDNs, eventually consistent caches.

The choice can be made per operation: an e-commerce site can let you browse and add to cart during a partition (AP) while refusing to complete payment until it can confirm stock (CP).

PACELC: the everyday trade-off

Partitions are rare. What you pay for every single request is latency. PACELC extends CAP:

If there’s a Partition, choose Availability or Consistency; Else, choose Latency or Consistency.

Keeping replicas strongly consistent means waiting for other replicas (often a majority, sometimes in other regions) on every write, and sometimes on reads. Weaker consistency lets a node answer immediately from local data.

  • PC/EC: consistent in both cases, paying latency. Spanner, etcd, ZooKeeper, traditional relational databases with synchronous replication.
  • PA/EL: available during partitions, low latency normally. Cassandra, DynamoDB default reads, Riak.
  • Mixed: many systems let you pick per request (Cassandra consistency levels, DynamoDB strongly consistent reads).

A note on “availability”

CAP’s availability is strict (every non-failed node must answer). Real-world availability, measured in nines, is about the user’s experience: a CP system with fast automatic failover can still offer 99.99% availability, because partitions are rare and short. So “CP” doesn’t mean “often down”; it means “during the rare partition, some requests fail rather than return wrong answers”.

In an interview

Avoid reciting “pick two”. Instead: “For the booking path I need consistency, so seat reservations go to a single primary per event; during a partition, bookings on the isolated side fail and the user retries. Browsing is served from eventually consistent caches and replicas, so it stays up.” Mention PACELC when discussing multi-region writes: strong consistency across regions means cross-region latency on every write.

Recap

CAP says that during a network partition a replicated system must choose consistency (refuse or wait) or availability (answer, maybe stale). Partitions can’t be avoided, so choose CP or AP per operation based on whether an error or a wrong answer is worse. PACELC adds the everyday trade-off: without partitions, strong consistency costs latency.

Exercise

CP or AP?

For each system, decide whether it should favour consistency (CP) or availability (AP) during a network partition, and what users would experience: (a) a bank's account-balance and transfer service; (b) a shopping cart; (c) a distributed lock service used to make sure only one worker runs a nightly job; (d) a social media timeline.

Text
(a)
(b)
(c)
(d)

Summary

  • CAP: during a network partition, a replicated system must choose between consistency (refuse or wait) and availability (answer, possibly stale).
  • Partitions aren't optional, so the real choice is CP or AP behaviour when one happens.
  • PACELC adds: Else, when there's no partition, the trade-off is latency vs consistency.
  • The choice can differ per operation and per feature within the same system.
  • Most of the time, the latency cost of strong consistency matters more than partitions.