CAP Theorem
The CAP theorem describes a fundamental trade-off in distributed systems when a network partition occurs: a system cannot simultaneously guarantee both strong consistency and availability. Partition tolerance is generally treated as a requirement for distributed systems, so the practical choice during a partition is between consistency and availability.
CAP concerns what happens when nodes cannot reliably communicate. Consistency means that a successful read returns the most recent successfully written value according to the system's consistency guarantee; availability means that every request to a non-failing node receives a non-error response. Partition tolerance means the system continues operating despite messages being lost or delayed between nodes.
The important qualification is that CAP applies specifically when a partition occurs. If communication is healthy, a system can often provide both consistency and availability. During a partition, however, replicas cannot coordinate normally, so a design must either reject or delay some operations to preserve consistency, or continue serving operations and risk replicas temporarily disagreeing.
In practice, engineers often describe systems as favoring CP or AP behavior. A CP-oriented design preserves consistency during a partition by sacrificing some availability. An AP-oriented design continues accepting operations during a partition and reconciles divergent state later, accepting weaker consistency during the failure window.
CAP is not the same as saying that a system can only ever provide two of three properties. The theorem is about simultaneous guarantees under partition. It also does not specify which consistency model an application should use; real systems may offer several consistency levels and may make different choices for different operations.
Key points
- CAP matters when a network partition prevents normal communication between replicas.
- During a partition, strong consistency and availability cannot both be guaranteed.
- Partition tolerance means continuing to operate despite communication failures between nodes.
- CP systems favor consistency during partitions; AP systems favor availability.
- CAP is about distributed-system guarantees, not a claim that only two properties can ever exist at once.
Interview questions
What is the CAP theorem?
CAP says that during a network partition, a distributed system cannot guarantee both strong consistency and availability at the same time. Consistency means clients see a correctly coordinated view of the data, while availability means requests continue receiving responses. Partition tolerance means the system keeps operating despite communication failures between nodes. Therefore, during a partition, a design must choose whether to preserve consistency or availability.
Why is partition tolerance usually considered necessary in distributed systems?
A distributed system communicates over a network, and network links can fail or messages can be delayed or lost. A system that has no partition tolerance cannot continue meeting its distributed guarantees when nodes become unable to communicate. Consequently, practical distributed-system designs normally assume partitions can happen. CAP then describes the choice made when that failure occurs.
What is the difference between a CP and an AP system?
A CP-oriented system preserves its consistency guarantee during a partition by refusing, delaying, or otherwise restricting operations that cannot be safely coordinated. An AP-oriented system continues accepting operations so that clients can receive responses even when replicas cannot communicate. AP designs may therefore expose temporarily divergent data and need reconciliation or convergence mechanisms. The exact behavior depends on the system's consistency and availability definitions.
Does CAP mean a system can only have two of the three properties?
That slogan is an oversimplification. The relevant constraint appears when a partition occurs: the system cannot simultaneously guarantee both consistency and availability under that partition. A system can provide consistency and availability while communication is healthy, so CAP is not a permanent two-out-of-three classification. Also, the exact meaning of consistency and availability must be stated before applying the theorem.
How would CAP influence the design of a Java backend service?
First, I would identify which operations require the strongest consistency guarantees and which can tolerate stale or conflicting data. For operations requiring coordinated state, I might choose a CP-oriented design that rejects or delays requests when replicas cannot agree. For workloads where serving a response is more important and temporary divergence is acceptable, an AP-oriented approach with reconciliation may be appropriate. The decision should come from application requirements rather than from labeling the entire service as simply CP or AP.