CAP Theorem in System Design: Consistency, Availability and Partition Tolerance
The CAP theorem says that when a distributed system suffers a network failure between its nodes, it must choose between staying consistent and staying available. It cannot guarantee both at that moment. This single idea explains why some databases refuse requests during an outage while others keep answering with possibly outdated data.
This tutorial explains each letter of CAP in plain language, walks through a concrete failure scenario, compares CP and AP systems, shows how real databases fit, introduces quorums and the PACELC theorem, and gives you an interview-ready way to discuss CAP.
1. What Is the CAP Theorem?
The CAP theorem was proposed by computer scientist Eric Brewer in 2000 and formally proved by Seth Gilbert and Nancy Lynch in 2002. It applies to distributed data stores, meaning systems that keep copies of the same data on more than one machine.
It names three desirable properties:
- C – Consistency: every read sees the most recent write.
- A – Availability: every request to a working node gets a non-error response.
- P – Partition tolerance: the system keeps operating even when messages between nodes are lost or delayed.
The popular summary is "pick any two of three", but that phrasing is misleading. In a real distributed system, network problems will happen, so partition tolerance is not optional. The honest version is:
When a network partition happens, a distributed system must choose either consistency or availability. When there is no partition, it can provide both.
2. The Three Properties Explained
Consistency (C)
In CAP, consistency means that all clients see the same, latest data, no matter which node they ask. Once a write succeeds, every later read must return that value or a newer one. The formal name for this strong guarantee is linearizability: the system behaves as if there were only one copy of the data.
Example: you change your profile name from "Ravi" to "Ravi Kumar". With consistency, a friend who loads your profile one second later sees "Ravi Kumar", never the old name.
Availability (A)
Availability means that every request received by a node that has not crashed gets a meaningful response, not an error or a timeout. The response does not have to contain the latest data. It just has to be an answer.
Example: during an outage, a shopping site still shows product pages and accepts items in the cart, even if some numbers are a little out of date.
Partition tolerance (P)
A network partition is a break in communication between groups of nodes. Each group is still running, but they cannot talk to each other. Partition tolerance means the system continues to work, in some defined way, despite this split.
Example: a cable between two data centres is cut. Both data centres are healthy and serving users, but they can no longer replicate data to each other.
| Property | Question it answers | Guarantee |
|---|---|---|
| Consistency | Will I see the latest data? | Every read returns the most recent write, or an error |
| Availability | Will I get an answer? | Every request to a live node gets a non-error response |
| Partition tolerance | Does the system survive a network split? | The system keeps operating despite lost messages between nodes |
3. What Is a Network Partition?
Beginners sometimes think of partitions as rare, exotic events. In practice they happen regularly in large systems, for many ordinary reasons:
- A network switch or router fails or is misconfigured.
- A cable between buildings or regions is damaged.
- A firewall rule change accidentally blocks traffic between services.
- A server is overloaded or paused by a long garbage-collection pause, so other nodes believe it is unreachable.
- Network congestion delays messages so much that they time out.
The tricky part is that a node cannot tell the difference between "the other node crashed" and "the network to the other node is broken". It only knows that it is not getting replies. CAP is about what the system should do in that uncertain situation.
4. Why You Cannot Have All Three
Let us walk through the classic scenario. A bank stores an account balance on two nodes, Node A in Mumbai and Node B in London. Both start with a balance of ₹1,000. Then the network between them fails.
Client 1 withdraws ₹500 through Node A, so Node A's balance becomes ₹500. Node A tries to tell Node B, but the message cannot get through. Now Client 2 asks Node B for the balance. Node B has only two options:
- Refuse to answer (or wait until the network heals) because it cannot be sure it has the latest value. The system stays consistent but is not available for this request.
- Answer with ₹1,000, the value it has. The system stays available but is not consistent, because Client 2 sees outdated data.
There is no third option. Node B cannot return ₹500 because it simply does not know about the withdrawal. That is the heart of the CAP theorem.
5. CP Systems: Choosing Consistency
A CP system prefers being correct over being responsive. During a partition, nodes that cannot confirm they have the latest data stop serving the affected reads or writes and return errors or time out.
Most CP systems use a majority (quorum) rule. With five nodes, any group that can reach at least three nodes may continue, while the smaller side becomes read-only or unavailable. Consensus algorithms such as Raft and Paxos work this way.
Choose CP when wrong data is worse than no data:
- Bank balances, payments and money transfers.
- Stock inventory where overselling is costly, such as limited concert tickets.
- Unique usernames or seat reservations, where two people must never get the same item.
- Configuration and coordination data, such as leader election and distributed locks.
Examples of systems usually classed as CP: ZooKeeper, etcd, Consul, HBase, and MongoDB when it uses majority read and write concerns.
6. AP Systems: Choosing Availability
An AP system prefers responding over being perfectly up to date. During a partition, every node keeps accepting reads and writes using its local data. When the network heals, the nodes exchange updates and resolve any conflicts. The data becomes consistent again eventually, which is why this model is called eventual consistency.
Conflicts can be resolved in several ways:
- Last write wins: keep the value with the latest timestamp. Simple, but one update can be silently lost.
- Merging: combine both changes, for example adding items from both versions of a shopping cart.
- CRDTs (Conflict-free Replicated Data Types): special data structures, such as counters and sets, designed to merge automatically without conflicts.
- Application decides: keep both versions and let the application or the user pick.
Choose AP when an answer now is better than a perfect answer later:
- Social media likes, view counts and follower counts.
- Product catalogues, reviews and recommendations.
- Shopping carts, where losing a sale is worse than merging carts later.
- DNS, caches and content delivery.
Examples of systems usually classed as AP: Cassandra, DynamoDB with eventually consistent reads, Riak, CouchDB and DNS.
7. What About CA Systems?
A "CA" system would be consistent and available but not partition tolerant. That only makes sense if partitions cannot happen, which is true for a single machine, such as one PostgreSQL or MySQL server. With only one node, there is no network between copies to break.
The moment you replicate data across machines connected by a network, partitions become possible, and you must decide what happens when one occurs. So for distributed systems, CA is not a real option. When people describe a clustered database as "CA", they usually mean it assumes a very reliable network and will lose either consistency or availability if that assumption breaks.
8. Real Databases and Where They Fit
Treat these labels as typical defaults, not fixed facts. Many databases let you tune the behaviour per query.
| System | Typical CAP choice | What happens during a partition |
|---|---|---|
| Single-node MySQL / PostgreSQL | Not distributed | No partition between replicas is possible; if the server fails, it is simply down |
| ZooKeeper, etcd, Consul | CP | The minority side refuses writes; the majority side keeps working |
| HBase | CP | Data regions on unreachable servers are unavailable until reassigned |
| MongoDB (replica set, majority concern) | CP | Writes pause until a new primary is elected on the majority side |
| Google Spanner | CP | Prioritises consistency, with very high availability from a carefully engineered network |
| Cassandra | AP by default, tunable | Any replica accepts writes; consistency level per query (ONE, QUORUM, ALL) |
| Amazon DynamoDB | AP by default, tunable | Eventually consistent reads by default; strongly consistent reads available on request |
| CouchDB, Riak | AP | All nodes accept writes; conflicts resolved after the partition heals |
9. Tunable Consistency with Quorums
Many AP-style databases let you choose consistency per request using three numbers:
- N: how many replicas store each piece of data.
- W: how many replicas must confirm a write before it counts as successful.
- R: how many replicas must respond to a read.
If R + W > N, the set of replicas you read from always overlaps with the set that accepted the latest write, so at least one replica in every read has the newest value.
| Setting (N = 3) | Behaviour | Trade-off |
|---|---|---|
| W = 1, R = 1 | Fastest, most available | Reads can be stale |
| W = 2, R = 2 (quorum) | Reads see the latest successful write in normal operation | Needs a majority of replicas reachable |
| W = 3, R = 1 | Fast reads, every replica updated on write | A single unreachable replica blocks writes |
| W = 1, R = 3 | Fast writes | A single unreachable replica blocks reads |
This shows that CAP is not a one-time choice for a whole database. You can choose CP-like behaviour for a payment record and AP-like behaviour for a page-view counter, even in the same cluster.
10. Beyond CAP: The PACELC Theorem
CAP only describes what happens during a partition, which is a small fraction of the time. Daniel Abadi proposed PACELC in 2010 to cover normal operation too:
If there is a Partition (P), choose Availability (A) or Consistency (C); Else (E), choose Latency (L) or Consistency (C).
The "else" part matters because keeping replicas perfectly in sync costs time even when the network is healthy: a write must wait for other replicas, often in other regions, to confirm. Systems that skip that wait respond faster but may serve slightly old data.
| Type | During a partition | Normal operation | Typical examples |
|---|---|---|---|
| PA/EL | Stay available | Prefer low latency | Cassandra and DynamoDB with default settings |
| PC/EC | Stay consistent | Prefer consistency | Spanner, etcd (linearizable reads by default) |
| PA/EC | Stay available | Prefer consistency | Some document stores in certain configurations |
11. How to Choose: CP or AP for Each Feature
Real systems rarely pick one model for everything. Decide per feature by asking: "What is worse for this data: showing something outdated, or showing an error?"
| Feature | Better choice | Reason |
|---|---|---|
| Money transfer | CP | A wrong balance can cause real financial loss |
| Booking the last seat on a flight | CP | Two passengers must not get the same seat |
| Username registration | CP | Usernames must stay unique |
| Like count on a post | AP | Being off by a few likes for a moment is harmless |
| News feed and timeline | AP | A slightly old feed is better than an error page |
| Shopping cart | AP | Carts can be merged later; refusing to add items loses sales |
| Product reviews | AP | A new review appearing a few seconds late is acceptable |
| Checkout payment step | CP | Orders and payments must be recorded exactly once |
12. Common Misconceptions
- "Pick any two of three." In distributed systems you cannot give up partition tolerance, so the real choice is C or A during a partition.
- "An AP system is never consistent." Without a partition, AP systems are usually consistent within milliseconds. The trade-off only bites during failures or replication delays.
- "C in CAP is the same as C in ACID." It is not. ACID consistency means a transaction keeps data valid according to rules such as constraints. CAP consistency means all replicas show the latest write.
- "Availability in CAP means 99.99% uptime." CAP availability is a strict theoretical property: every live node must answer every request. A CP system can still have excellent real-world uptime.
- "A database is permanently CP or AP." Many databases let you choose consistency per query, so the label depends on how you use them.
- "CAP covers all trade-offs." It ignores latency and performance during normal operation, which is why PACELC exists.
13. How to Discuss CAP in a System Design Interview
- State the real trade-off: "Partitions are unavoidable in a distributed system, so I need to decide what each feature does during one."
- Classify the data: separate data where correctness is critical (payments, inventory) from data where freshness can lag (feeds, counters).
- Pick a behaviour per category: CP with quorum writes or a single leader for critical data; AP with eventual consistency for the rest.
- Explain conflict handling: for AP data, say how conflicts are resolved, such as last-write-wins, merging or CRDTs.
- Mention latency: bring in PACELC when discussing multi-region deployments, where strong consistency adds cross-region delay to every write.
- Name concrete tools: for example, "PostgreSQL with synchronous replication for orders, Cassandra for activity events."
14. Summary
- The CAP theorem says a distributed system cannot guarantee both consistency and availability while a network partition is happening.
- Consistency means every read sees the latest write; availability means every live node answers; partition tolerance means surviving network splits.
- Because partitions are unavoidable, the practical choice is between CP (correct but may reject requests) and AP (always answers but may be stale).
- Quorums (R + W > N) let many databases tune consistency per operation.
- PACELC extends CAP: without partitions, systems still trade latency for consistency.
Use CAP as a thinking tool: for each piece of data, decide whether stale data or an error is the lesser evil. Open the Interview Questions tab to practise the questions most often asked about the CAP theorem.
CAP Theorem: Interview Questions and Answers
The questions on this topic that come up most often in system design interviews for senior engineers, tech leads and architects. Each answer covers the reasoning, trade-offs and real-world examples that interviewers look for.
Q1. What does the CAP theorem actually state?
In a distributed data store, when a network partition occurs, the system must choose between consistency (every read sees the latest write, linearizability) and availability (every request to a non-failed node gets a non-error response). Partitions cannot be ruled out in real networks, so the practical choice is CP or AP behaviour during a partition. When there is no partition, a system can provide both.
Q2. Why is "pick two out of three" a misleading way to describe CAP?
Because partition tolerance is not optional for a distributed system: networks do fail, so "CA" only describes a single-node system or one that stops being distributed. The choice also need not be global. It can differ per operation (strongly consistent writes, relaxed reads) and per data type. And CAP's consistency means linearizability specifically, not the "C" in ACID.
Q3. How would you classify common databases under CAP?
- CP-leaning: ZooKeeper and etcd (consensus; the minority side refuses requests), HBase, and MongoDB with majority write and read concerns.
- AP-leaning: Cassandra and DynamoDB-style stores with low consistency levels, and Couchbase across data centres with XDCR.
Many are tunable. Cassandra with QUORUM reads and writes (R + W > N) behaves much more like CP for individual keys. In an interview it is better to explain the configuration than to give one label.
Q4. Give a real example where you would choose availability over consistency, and one where you would not.
- Availability: a shopping cart, social feed, view counters or product reviews. Showing slightly stale data, or merging two cart updates later, is better than refusing service. Amazon's Dynamo paper was motivated by exactly the cart case.
- Consistency: account balances, inventory reservation for the last item, seat booking, distributed locks or leader election. A stale read could double-spend money or oversell, so refusing or delaying the request during a partition is the safer failure.
Q5. How does PACELC extend CAP?
PACELC says: if there is a Partition, choose A or C; Else, in normal operation, choose between Latency and Consistency. It captures the trade-off that matters most of the time, because synchronous replication for strong consistency adds latency even when the network is healthy. Cassandra and DynamoDB are typically PA/EL, while systems like Spanner and other consensus-based databases are PC/EC.
Q6. What happens to a CP system during a partition, from the client's point of view?
Nodes that cannot reach a quorum stop accepting writes, and often linearizable reads, so clients connected to the minority side get errors or timeouts. Clients that can reach the majority continue normally. Client libraries should therefore retry against other nodes, treat timeouts as "outcome unknown" (the write may have succeeded on the majority), and use idempotent operations so a retry is safe.
Q7. How do AP systems resolve conflicting writes after a partition heals?
Common strategies are last-write-wins with timestamps (simple, but can silently lose updates and relies on clocks), version vectors to detect concurrent writes and keep both versions for the application to merge, and CRDTs (conflict-free replicated data types) such as counters and sets that merge automatically. Read repair, hinted handoff and anti-entropy with Merkle trees then bring replicas back into agreement. The choice depends on whether losing a concurrent update is acceptable.