← Back to System Design, page 1

system-design-0003 · Medium fresher

Explain the CAP theorem. What does it really say about a distributed database?

CAP says that when a network partition splits a distributed system, it must choose between consistency (every read sees the latest write or gets an error) and availability (every request to a working node gets a non-error response). Partitions cannot be ruled out, so the real choice is CP or AP behaviour during a partition. When there is no partition, a system can offer both.

Full answer

The three properties

  • Consistency (C): in CAP, this means linearizability. Every read returns the most recent completed write, as if there were one copy of the data.
  • Availability (A): every request received by a non-failed node gets a non-error response, without a guarantee that it is the latest value.
  • Partition tolerance (P): the system keeps working when messages between nodes are lost or delayed.

What the theorem says

During a partition, a node that cannot reach the others has two options when it gets a request:

  1. Refuse or wait (stay consistent, give up availability). This is CP.
  2. Answer with its local, possibly stale data (stay available, give up consistency). This is AP.

It cannot do both. Because real networks do drop packets, a distributed system cannot "choose CA". The popular "pick 2 of 3" phrasing is misleading for this reason.

Example

A bank stores account balances in two data centres. The link between them fails. A customer tries to withdraw money in data centre B.

  • A CP design rejects the withdrawal (or makes it wait) because B cannot confirm the latest balance with A. No double spending, but some users see errors.
  • An AP design allows it using B's local balance and reconciles later. Users are served, but the account might go negative.

A like-counter on a post can be AP: a slightly wrong count for a few seconds is fine. A seat booking for an exam centre should be CP for the final booking step.

Where systems sit

ZooKeeper and etcd are CP: a minority side of a partition stops accepting writes. Cassandra and DynamoDB-style stores lean AP by default, with tunable consistency per request. Many systems let you choose per operation, which is more useful than a single label.

Limits of CAP

CAP only covers behaviour during partitions and only one strict kind of consistency. It says nothing about latency in normal operation; PACELC extends it for that.

By level

Fresher: Define the three properties and explain the CP versus AP choice with one simple example. Recognise that partition tolerance is not optional. The answer should make clear that partitions are not optional.

Mid: Map real systems and operations to CP or AP and explain tunable consistency with quorums. Explain why the same product can mix both behaviours. Expect one example of a product that mixes CP and AP operations.

Senior: Discuss the limits of CAP, PACELC, how partitions show up in practice (one-way links, GC pauses, slow nodes), and how to design user-facing behaviour for the minority side, including reconciliation and conflict resolution. It should also explain how users on the minority side experience the system.

Key points

  • C in CAP means linearizability, not the C in ACID
  • Partitions will happen, so the real choice is CP or AP during a partition
  • CP refuses or delays requests; AP answers with possibly stale data
  • Choice can differ per operation in the same product
  • CAP says nothing about normal-time latency; see PACELC

Common mistakes

  • Saying a system is CA as if partitions can be avoided
  • Confusing CAP consistency with ACID consistency
  • Treating availability as uptime percentage rather than the CAP definition
  • Labelling a whole product CP or AP when it is tunable per request

Follow-ups

  • How does PACELC extend CAP?
  • Which parts of an e-commerce checkout would you make CP, and which AP?
  • How does a quorum system behave during a partition?

Tips

  • Define each letter precisely before giving the trade-off
  • Use one concrete partition scenario and walk through both choices
  • Say early that "pick 2 of 3" is a simplification

Evaluation rubric

  • Correct definitions, especially C as linearizability
  • Explains that the choice is forced only during a partition
  • Gives a scenario showing CP versus AP behaviour
  • Notes limitations of CAP