# CAP Theorem: Why a Split Network Forces a Choice

> When a network breaks a distributed system in two, the system must choose between fresh answers and answers for every request. Learn how designers read that choice.

**In short:** The CAP theorem says that when a network breaks a distributed system in two, it must choose between fresh answers and answers for every request.

From "System design", a free course by Venkat R. Murty: https://designervenkat.online/learn/system-design/cap-theorem

## What you will learn

Picture a shop that keeps its stock list on two computers in two cities, so both can take orders even if one goes down. Software that runs on several computers and shares work or data between them is called a distributed system. The CAP Theorem is a rule about what such a system can promise when the network between those computers breaks. This lesson explains the rule, why it forces a choice, and how designers make that choice.

You will learn the three properties behind the letters C, A and P, why P is never optional, and how to read the CP and AP labels. A worked example and a short quiz will help you use the idea.

- **Distributed system:** Software that runs on several computers at once and shares work or data among them.
- **Node:** One computer or one server process that is part of a distributed system.
- **Data center:** A building full of servers that run a company's software.
- **Network partition:** A break in the network that splits the nodes into groups that cannot send messages to each other.
- **Stale data:** An old value that has been replaced by a newer one but is still being shown or used.
- **Consistency (in CAP):** All nodes give the same answer, and that answer is the newest saved value.
- **Availability (in CAP):** Every request sent to a working node gets a response.
- **Partition tolerance:** The system keeps working even when messages between nodes are lost or arrive late.
- **CP system:** A system that keeps its data correct during a split, even if it must refuse or delay some requests.
- **AP system:** A system that keeps answering during a split, even if some of its answers are old.

## What are the three properties?

CAP stands for Consistency, Availability and Partition tolerance. Each word has a narrow meaning in this theorem. In everyday speech, 'consistent' and 'available' sound vague. Here, each one is a clear test that a system either passes or fails.

- **Consistency:** All nodes see the same data. In practice, once a new value is saved, a later read from any node should return that value and not an older copy.
- **Availability:** A system that is available never refuses a request. Silence or a refusal therefore counts as a failure, even if the reply that comes back is not the newest one.
- **Partition tolerance:** The system continues to operate despite network partitions, when messages between nodes are lost or delayed. A system with this property keeps running through a split.

Consistency and availability clash in only one situation: when nodes cannot reach each other. That situation is called a partition, and it is the next idea to understand.

Gilbert and Lynch wrote a formal 2002 paper that proves this idea. They built on a 2000 talk by Eric Brewer, which is why the rule is also called Brewer's theorem.

### What is a partition?

A network partition is a split. A network failure causes a distributed system to split into two or more groups of nodes that cannot communicate with each other. Each group is still running and still takes requests, but the groups cannot share messages.

Imagine four servers, two in each of two data centers. The cable between the two data centers is cut. Each side still runs and still takes requests. Neither side can see the writes made on the other side, so the two sides can drift apart.

> **Tip:** A partition is not a crash. Every server is still running. The problem is the road between them.

## Why is partition tolerance not optional?

Networks break. Cables get cut, switches fail and packets get lost. Partition tolerance is essential for distributed systems because network failures can and do happen.

No design can remove this risk. Partitions will happen in any networked system. That is why a system that cannot survive a split is not a safe choice for distributed work, and why partition tolerance is treated as required.

This is also why the common saying 'pick two of three' is misleading. You do not pick P from a menu. What you choose is which of the other two properties to give up when a partition is in effect.

### What happens when there is no split?

When the links work, a well-built system can offer fresh answers and an answer to every request at the same time. When the network is healthy, we can often provide both consistency and availability.

CAP speaks only to the moment a split happens. CAP only becomes relevant when the system experiences a partition. Outside that moment, the rule has nothing to say about the choice.

Some writers describe a CA system, meaning one that is consistent and available with no partitions. For a system spread across machines, that label is not a real option, because splits still happen. No system is truly CA.

> **Tip:** Treat CAP as a plan for the bad day, not as a description of every day.

## How do you read the choice between CP and AP?

During a split, a system leans one of two ways. The name tells you what it keeps and what it gives up. The letters show what a system gives up when the network breaks.

- **CP: consistency first:** On the side that cannot confirm the newest data, a CP system refuses or delays requests. It prefers making you wait over showing you something stale. A CP system refuses or delays some requests until it can confirm that the answer is current.
- **AP: availability first:** An AP system keeps answering on both sides of the split. The answers may be old, and the differences are merged after the link returns. An AP system keeps answering on both sides and reconciles the divergence afterwards.

Two everyday examples make the idea concrete. A bank balance is a typical CP case, because a wrong balance can let money move that should not. If you are building a banking ledger, rejecting a write is often better than accepting contradictory balances. A social feed is a typical AP case. Social media feeds are usually labelled AP.

A helpful test is to compare two harms: an error right now, or old and conflicting data right now. Which harm hurts the user more? Partitions are inevitable, so the real choice is CP vs AP, made per operation rather than per product.

> **Tip:** CP and AP both keep partition tolerance. They differ only in what the system does while the split lasts.

## Worked example: a bank screen and a like counter

Here is one app with two screens. Screen one is a bank transfer. Screen two shows a like counter on a photo. A cable between two data centers is cut, and both screens are affected. Follow what each screen should do, one step at a time.

1. **Set the scene:** Each data center holds a copy of the account data and of the like count. The link between them is down, so each side can see only its own copy.
2. **Bank transfer: choose CP:** A user on one side asks to move money out of an account. That side cannot confirm the newest balance, so the screen shows an error and blocks the transfer. The balance stays correct, but the user cannot pay yet.
3. **Like counter: choose AP:** A user on the other side taps like. The local node accepts the write. The counter shows the new total on that side, which may lag the true count.
4. **Reconcile when the link returns:** After the partition heals, the conflict is resolved. For the like counter, the new likes from each side are added together, so the number settles to the right value.
5. **Compare the harm:** A wrong balance could let money leave an account that has none left. A like count that is a few taps out of date harms no one. So the bank picks CP and the photo screen picks AP.

The small program below repeats the two choices with one shared balance.

Run the sample below to see the two behaviors side by side.

### Run the idea in Python

This toy program is not a bank. It shows only the two choices. The variable split means the link between the two copies is down.

```python
balance = {'A': 500, 'B': 500}
split = True

def withdraw_cp(node, amount):
    if split:
        return 'error: cannot confirm balance, withdrawal blocked'
    balance['A'] -= amount
    balance['B'] -= amount
    return 'ok'

def withdraw_ap(node, amount):
    balance[node] -= amount
    return 'ok, balance on ' + node + ' is now ' + str(balance[node])

print(withdraw_cp('A', 100))
print(withdraw_ap('A', 100))
print(balance)

```

Output:

```text
error: cannot confirm balance, withdrawal blocked
ok, balance on A is now 400
{'A': 400, 'B': 500}

```

The first line is the CP answer: a refusal, so nothing changes. The second line is the AP answer: the local copy changes at once. The last line shows that the two copies now disagree. That disagreement is the stale-data risk an AP system accepts.

## Check your understanding

Try these two short scenarios. Each one needs the CP and AP ideas to answer.

**Quick check:** A chat app runs servers in two regions. The link between the regions goes down, but users in each region can still reach their local server. Should the app keep accepting and showing messages on both sides, possibly out of sync, or block sending until the link returns? Which CAP choice is each option, and which would you pick?

A. Blocking sends is AP, because no user ever sees an old message.
B. Accepting messages on both sides is AP, and the two sides are synced when the link returns.
C. Accepting messages on both sides is CP, because each region's messages stay correct.
D. Blocking sends is AP, because each user still gets a reply from the local server.

**Answer:** B. Accepting on both sides keeps every user able to send, which is availability, so that choice is AP. Blocking until the link returns keeps one agreed order, which is consistency, so that choice is CP. Many chat designs pick AP, because a short delay in seeing a message often matters less than a chat that stops working. That is a design judgment, and a team with different needs could choose CP.

**Quick check:** A concert site sells seats from two data centers. The link between them fails. One side cannot confirm whether the last seat in a section is still free. What should that side do to avoid selling the same seat twice?

A. Keep selling the seat, and fix any double sale after the link returns, which is CP.
B. Ignore the split and assume the link will never fail, which keeps the system CA.
C. Stop selling that seat on the side that cannot confirm it, which is the CP choice.
D. Show a saved seat map and let every request succeed, which is the CP choice.

**Answer:** C. Selling one seat twice is a stale-data problem. Refusing to sell on the side that cannot confirm the seat is the CP answer. Option 0 describes an AP choice, not CP. Option 3 is also AP, because it keeps answering with old data. Option 1 ignores the split, which no networked system can do.

## Common mistakes

- **Treating P as optional:** Partition tolerance is not a feature you switch off. A system spread over a network must plan for splits from the start.
- **Applying CAP all the time:** The theorem speaks only to the time a partition is active. Normal operation is shaped by other trade-offs, which other lessons cover.
- **Reading availability as perfect uptime:** In CAP, availability means that a working node answers each request during a split. It does not promise that a service is never down.

## Summary

1. **Name the three properties:** Consistency means everyone sees the newest value. Availability means working nodes answer. Partition tolerance means the system survives lost or late messages.
2. **Accept that splits happen:** Plan for partitions as a normal event. The choice is only about what the system does while one is active.
3. **Choose per feature:** For each feature, ask whether an error or stale data is the worse harm. Then label the feature CP or AP.

CP systems refuse or delay requests to stay correct. AP systems keep answering and may return old data, then reconcile later. Other lessons go deeper into the levels of consistency and into the trade-off between availability and consistency.

## Sources

- [Gilbert and Lynch, Brewer's Conjecture and the Feasibility of Consistent, Available, Partition-Tolerant Web Services (2002 paper, PDF)](https://www.cs.princeton.edu/courses/archive/spr22/cos418/papers/cap.pdf)
- [ACM record for the Gilbert and Lynch paper](https://dl.acm.org/doi/10.1145/564585.564601)
- [Medium: CAP Theorem deep dive (CP, AP and CA choices)](https://medium.com/@shivanimutke2501/cap-theorem-a-deep-dive-into-cp-ap-ca-choices-in-distributed-systems-f19496a62d84)
- [AlgoMaster: CAP Theorem chapter](https://algomaster.io/learn/system-design/cap-theorem)
- [AlgoMaster blog: CAP Theorem Explained](https://blog.algomaster.io/p/cap-theorem-explained)
- [Luminousmen: CAP Theorem and PACELC Explained in Plain English](https://luminousmen.com/post/cap-and-pacelc-theorems-in-plain-english/)
- [Matthew Swong: CAP Theorem Explained, How It Shapes Your Database Choice](https://www.matthewswong.com/en/blog/cap-theorem-explained-practical-examples/)
- [System Design Tutorial: CAP Theorem Explained, CP vs AP and PACELC](https://systemdesigntutorial.com/m21-cap-theorem.html)
- [System Design Handbook: CAP Theorem in System Design](https://www.systemdesignhandbook.com/guides/cap-theorem-in-system-design/)
- [Design Gurus: CAP Theorem real-world examples, CP vs AP](https://www.designgurus.io/answers/detail/what-is-availability-vs-consistency-in-terms-of-cap-theorem)
- [StackPractices: CAP Theorem and Database Trade-offs](https://stackpractices.com/guides/cap-theorem-guide/)
- [LearnCodeCamp: CAP Theorem Explained](https://learncodecamp.net/cap-theorem-explained/)
- [Ratnesh Maurya blog: CAP Theorem](https://blog.ratnesh-maurya.com/technical-terms/cap-theorem/)
- [Techoral: CAP Theorem Explained with Real Examples](https://techoral.com/systemdesign/cap-theorem-explained.html)
- [Robin Ali blog: CAP Theorem comprehensive guide](https://robinali34.github.io/blog_system_design/2025/11/29/cap-theorem-guide/)
