DEV Community

designbench.dev
designbench.dev

Posted on Originally published at designbench.dev

CAP theorem for system design interviews: what actually happens when the network splits

The interview question: "Your app runs in two regions. The link between them goes down. What does your database do, and how did you decide?"

Most candidates recite "consistency, availability, partition tolerance: pick two." That line is where interviewers start digging, because it is subtly wrong. Here is the precise version, round by round, using the simulations in the Designbench CAP lesson.

Round 1: What CAP actually says

Interviewer: State the CAP theorem precisely.

You: In a system that copies data across machines, when the network partitions (some replicas can't reach others), each request has to choose one of two behaviours:

  • Consistent (C): every read sees the latest write, as if there were one copy. To guarantee that, a replica that can't confirm it is up to date must refuse or wait.
  • Available (A): every replica that is up keeps answering, even if its answer might be stale.

The "pick two" framing misleads because partitions are not optional. Networks do split, so P is a fact you live with. CAP is really about what you give up during a partition: C or A. And a "CA" system is just one that hasn't met a partition yet.

Round 2: Watch it happen

Interviewer: Make it concrete.

You: Three replicas hold one value, x. Alice talks to a replica in us-east, Bob to one in eu-west. Now cut the ocean link.

  • CP: a write needs a majority, 2 of 3, to agree. The side with 2 replicas keeps working. Bob's side has 1, so his writes return an error. Nobody ever reads a wrong value, but Bob is down.
  • AP: each replica answers from its local copy and syncs in the background. Bob keeps working. But Alice and Bob can now write different values for x, and each reads their own until the link heals.

The CAP simulation on Designbench
CP mode on Designbench with the ocean link cut: Alice’s side (2 of 3 replicas) keeps writing, while Bob’s lone replica returns errors instead of stale data.

Round 3: Most of the time there is no partition

Interviewer: Partitions are rare. Does CAP matter day to day?

You: Not directly, which is why PACELC is the more useful framing: if Partitioned, choose A or C; Else, choose Latency or Consistency. Even with a healthy network, a consistent write that needs a reply from across the ocean pays roughly 80 ms. An AP write returns at once, but a read on another replica can be stale until replication catches up.

  • PC/EC, always consistent: waits for a majority on every request. etcd and ZooKeeper work this way, which is why they hold config, leader elections and locks.
  • PA/EL, always fast: answers locally and syncs later. DynamoDB's default eventually consistent reads and Cassandra at consistency level ONE behave like this.

Round 4: Pick per feature, not per company

Interviewer: So is your system CP or AP?

You: That is the wrong unit. One product has features that want different answers:

  • Account balance, payments, "last seat" inventory, unique usernames: CP. Refusing is better than double-spending or selling a seat twice.
  • Locks, leader election, config: CP. Two leaders is worse than none.
  • Feeds, likes, view counts, product reviews: AP. Slightly stale is fine; down is not.
  • Shopping cart: AP, with a merge on heal. Losing an "add to cart" costs a sale.

Many databases let you choose per request. In Cassandra you set the consistency level on each query:

// Balance: needs a majority of replicas (2 of 3), so it refuses on the minority side
SimpleStatement balance = SimpleStatement
        .newInstance("SELECT balance FROM accounts WHERE id = ?", accountId)
        .setConsistencyLevel(DefaultConsistencyLevel.QUORUM);

// Feed: any one replica will do, so it keeps answering during a split
SimpleStatement feed = SimpleStatement
        .newInstance("SELECT * FROM feed WHERE user_id = ?", userId)
        .setConsistencyLevel(DefaultConsistencyLevel.ONE);
Enter fullscreen mode Exit fullscreen mode

With 3 replicas, writing at QUORUM and reading at QUORUM means every read overlaps every write in at least one replica (2 + 2 > 3), so the read sees the latest acknowledged write. More on that in Replication.

Round 5: What happens when the link comes back?

Interviewer: The AP side accepted writes on both sides. Now what?

You: This is the follow-up that separates good answers from great ones. You need a rule for conflicting versions:

  • Last write wins: keep the newest timestamp. Simple, but it silently drops the other write, and clocks on different machines disagree.
  • Keep both and merge: track versions (version vectors) and let the application merge. A cart becomes the union of both carts. The catch: an item deleted on one side can reappear.
  • CRDTs: data types designed to merge automatically, such as counters and sets. Great for likes and presence, not for a bank balance.

Saying which rule you use, and what it costs, is the part interviewers remember.

Say it in the interview

"CAP is about behaviour during a partition. Partitions happen, so the real choice is consistency or availability while one lasts, and I'd make it per feature: payments, inventory and locks refuse on the minority side; feeds and carts keep answering and reconcile on heal. Most of the time there's no partition, so the everyday trade-off is latency versus consistency, which is PACELC. For conflicts I'd use last write wins only where losing a write is harmless, and merges or CRDTs elsewhere."

Common mistakes

  • "Pick two of three" as if partition tolerance were optional.
  • Calling a single-node database "CA" as an achievement.
  • Labelling a whole system CP or AP instead of each feature.
  • Confusing CAP consistency (latest write, everywhere) with ACID consistency (constraints hold).
  • Never saying what happens when the partition heals.
  • Forgetting the latency cost of consistency when there is no partition at all.

Practise it

Cut the ocean link, switch between CP and AP, and decide four systems under a partition in the CAP lesson on Designbench. Related: Consistency models, Replication and Availability and the nines.

Top comments (0)