Explain the CAP theorem with an example
Model answer
The CAP theorem says that a distributed data store cannot guarantee all three of these at the same time during a network partition:
- Consistency: every read sees the most recent write (linearizability).
- Availability: every request to a working node gets a non-error response.
- Partition tolerance: the system keeps working even when messages between nodes are lost or delayed.
Networks do fail, so partition tolerance is not optional in a distributed system. The real choice is what to give up when a partition happens: consistency or availability.
Example: two data centers hold copies of an account balance and the link between them breaks. A CP system refuses writes (or reads) on the minority side to avoid conflicting values, so some requests fail. An AP system keeps serving both sides, accepting that they may diverge and must be reconciled later.
When there is no partition, you can have both, but there is still a trade-off between latency and consistency (the PACELC extension).
Key points
- During a partition, choose between consistency and availability
- Partition tolerance is not optional in distributed systems
- CP rejects requests to stay correct; AP serves possibly stale data
Likely follow-ups
- Would you choose CP or AP for a shopping cart and for a bank ledger?
- What does PACELC add to CAP?
Common mistakes
- Saying you can pick any two of the three as a permanent choice
- Confusing CAP consistency with ACID consistency