CAP and PACELC
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.
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.
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.
(a)
(b)
(c)
(d)(a) CP. A transfer must not be applied on two sides of a partition (double spending).
Users on the cut-off side see an error or "try again later" for transfers;
read-only balance views may show a clearly labelled cached value.
(b) AP. Better to let people keep adding items; carts on both sides can be merged
later (union of items). Rarely an item reappears after removal - an acceptable cost.
(c) CP. Two workers both believing they hold the lock is exactly what the lock exists
to prevent. The side without a majority can't grant the lock, so the job may be
delayed rather than run twice.
(d) AP. Showing a slightly stale timeline is far better than an error; posts made
during the partition appear when it heals.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.