The CAP theorem in System Design Interviews: When to Use It and What to Say
The CAP theorem is a compact way to talk about the fundamental trade-offs any distributed storage system faces. In a single sentence: when a network partition happens, a distributed system must give up either consistency or availability. The theorem helps you reason about what will happen when the network drops packets or a node crashes. In an interview you use it to justify why a design picks a particular database or why you add a cache layer.
The Problem CAP Solves
Imagine a web service that stores user profiles across three data centers. Users expect their profile to be visible instantly after an update (consistency) and also expect the service to respond even if one data center is unreachable (availability). The network can fail between data centers (partition). CAP tells you that while the data centers cannot reach each other, you cannot have both consistency and availability: either some requests are refused until the partition heals, or the service answers with data that may be stale. You must decide which matters more for your use case and design accordingly.
How CAP Works – Step by Step
- Define the three properties
- Consistency – every read sees the most recent write.
- Availability – every request to a node that has not failed receives a non-error response.
- Partition tolerance – the system continues to operate despite arbitrary network partitions.
- Identify the failure mode – a partition is any loss of communication between nodes. In practice this could be a router failure or a cloud region outage.
- Treat partition tolerance as a given, then choose consistency or availability for the duration of a partition – the choice depends on the SLA and user expectations.
- Implement the chosen trade-off – for example, using a quorum-based protocol to achieve consistency while still being available when the partition is small.
Small Worked Example
Suppose you have a key-value store replicated on three nodes (A, B, C). You decide to use a majority quorum for writes and reads.
- Write: client sends
PUT key=K, value=Vto all three nodes. The write is considered committed when at least two nodes acknowledge. - Read: client sends
GET Kto all three nodes. It returns the value from the node that has the latest timestamp among the two responses.
If a partition isolates node C, A and B can still form a majority. The system remains available and consistent for those two nodes. However, node C on the minority side cannot reach a majority, and if all three nodes are cut off from each other no node can. A side without a majority can either:
- Reject writes (maintaining consistency) – CP.
- Accept writes on the isolated node (maintaining availability) – AP.
The choice you make is the interview talking point.
Where CAP Shows Up in Classic Interview Questions
| Interview Question | Exact Place CAP Appears |
|---|---|
| Design a globally distributed social feed | When you discuss data replication across regions and need to choose between strong consistency for ordering vs. always-on reads. |
| Build a URL shortener service | In the storage layer, deciding whether short-URL lookups must always return the latest mapping or can tolerate stale reads during a data-center outage. |
| Design a real-time multiplayer game backend | When you talk about state synchronization: do you guarantee that every player sees the same game state instantly (C) or keep the game running even if some servers lose contact (A)? |
In each case the interviewer expects you to name the trade-off and to propose a concrete mechanism (e.g., Dynamo-style quorum, read-after-write consistency, or an AP cache).
Trade-offs and the Alternative Interviewer May Ask About
When you pick CP, you typically use a strongly consistent store like Spanner or a relational DB with synchronous replication. The downside is higher latency and the possibility of rejecting requests during a partition.
When you pick AP, you often rely on eventually consistent stores such as DynamoDB or Cassandra. The upside is low latency and high availability, but you must handle stale reads and conflict resolution (last-write-wins, vector clocks, or custom merge logic).
You will sometimes hear CA, which assumes the network never partitions. That only holds for a single node; replicas in one region on a reliable LAN still lose contact (a switch fails, a node stalls), so a replicated system still has to say what it does then. Mentioning a quorum approach shows you understand the middle ground: you can provide partial consistency while still being available for most partitions.
30-Second Spoken Version
"CAP says that when the network partitions, a distributed system cannot keep both Consistency and Availability. Partitions will happen, so the real choice is which one you give up while a partition lasts. For a globally replicated account balance you might choose Consistency and Partition tolerance, using a quorum protocol, which means you may reject writes during a network split; a social feed usually goes the other way, since a slightly stale feed is fine. If you need the service always on, you go for Availability and Partition tolerance, accepting eventual consistency. The key is to explain the trade-off and show a concrete mechanism."
Common Mistakes Candidates Make
- Treating CAP as a strict rule – saying you must always pick exactly two properties. In reality you can design systems that approximate the third property under normal conditions.
- Ignoring the partition case – many candidates discuss only latency vs. consistency and forget to mention what happens when nodes lose contact.
- Mis-labeling eventual consistency – calling a read-through cache “consistent” when it can serve stale data.
- Over-engineering – proposing a full Paxos implementation for a simple key-value store when a quorum write/read is sufficient.
- Forgetting the client perspective – focusing on internal protocols without explaining how the trade-off affects user experience.
Avoid these by keeping the explanation grounded in a concrete example and by explicitly stating what the system does during a partition.
How to Practice This
- Read the free worked design on distributed systems foundations, Distributed Systems Foundations: from Consistent Hashing to B-Trees. It applies CAP, replication and quorums to a key-value store, each with a figure.
- Draw your own design on paper or a whiteboard for a classic interview prompt (e.g., a global chat service). Identify where the CAP trade-off sits and annotate your diagram with the chosen properties.
- Record a 30-second pitch and replay it. Make sure you mention the definition, the chosen two properties, and the concrete mechanism you’d use.
Questions people ask
What does “partition tolerance” really mean?
It means the system keeps functioning even when network links fail and nodes become isolated. It does not guarantee that all nodes have the same data during the outage.
Can a system be both strongly consistent and highly available?
Only when there is no network partition. In a real distributed environment you must accept higher latency or occasional unavailability during a split.
Is eventual consistency the same as weak consistency?
Eventual consistency is a type of weak consistency where, after writes stop, all replicas converge to the same value. Not all weak consistency guarantees convergence.
How do quorum reads/writes relate to CAP?
Quorum protocols let you pick a consistency level that works with availability. Requiring a majority for reads and writes gives CP behaviour: the side that cannot reach a quorum rejects operations. Relaxing that, with a sloppy quorum and hinted handoff as in Dynamo, keeps accepting operations during a partition, which is AP.