# Availability vs Consistency: What to Protect When the Network Splits

> When servers cannot reach each other, a system must choose between answering now with old data and waiting to be sure. Learn how to choose for each feature.

**In short:** When servers cannot reach each other, a system must choose between answering now with old data and waiting to be sure, and that choice is made per feature.

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

## Start here

Imagine a shopping site that runs computers in two cities. A customer asks for a product page, but the two cities have lost their link. What should the site do? It could show what it knows right now, even if that is a little old. Or it could wait until it can check with the other city. This lesson explains that choice. You will learn what availability and consistency mean, why a broken link forces you to pick one, and how to decide for each feature.

- **Node:** One computer or server that runs part of a system.
- **Replica:** A copy of the same data, kept on another node.
- **Network partition:** A break where some nodes cannot reach other nodes, even though each side still runs.
- **Availability:** Every request sent to a working node gets a response.
- **Consistency:** Every read returns the most recent write, so people see the same up-to-date value.
- **Stale data:** An old value that has not been updated yet.
- **CP:** Consistent and partition tolerant. During a split, the system protects correct data, even if some requests wait or fail.
- **AP:** Available and partition tolerant. During a split, the system keeps answering, even if some answers are stale.

Partition tolerance is a term you will see often. It means the system keeps running when a split happens. Both CP and AP systems have it. The real question is what they do about the data.

## What are the two promises a system can make?

A system that runs on many nodes can make promises to the people who use it. Two of the most important promises are about availability and consistency. The words sound alike, but they measure different things.

- **The availability promise:** Every request that reaches a working node gets a response. The answer might not be the newest one, but the user is not turned away. A feed that loads with some old posts still keeps this promise.
- **The consistency promise:** Every read returns the latest write. If someone changes a price, the next person who reads that price sees the new one. This promise is about being right, not about being quick.
- **Why they are not the same:** A system can answer fast with old data. That is available but not consistent. It can also hold a request until it is sure. That is consistent but may not answer in time. Both matter, and you cannot always have both.

> **Tip:** A fast answer is not automatically a correct answer. When you read a design, ask two separate questions: will it respond, and will the response be the newest value?

### Why does a partition force a choice?

When the network is healthy, nodes talk all the time. A write on one node is sent to its replicas, so every read gets the same answer. Most systems can be both available and consistent in that state. The trouble starts when a split happens. A node cannot hear from the nodes it used to talk to, so it cannot tell whether it has the newest write. It must then pick one promise to keep. It can answer from its own copy, which keeps it available but may be stale. Or it can refuse or wait, which keeps the data right but makes the request fail or slow down.

## What happens during a partition?

1. **The network splits:** The nodes form two groups. Each group can talk within itself, but not across the break.
2. **A write lands on one side:** A shopper or staff member changes a value on one side. The other side never hears about the change.
3. **A read arrives on the other side:** The other side must decide what to return. Its own copy may now be out of date.
4. **The system picks a side:** It either answers with what it holds, or it holds the request back. This is the trade-off at the heart of this lesson.

### Should a system refuse or answer with old data?

- **CP: protect the right answer:** A CP system refuses or delays requests that could return wrong data. Users may see an error or wait longer, but they will not get a wrong answer.
- **AP: keep answering:** An AP system keeps replying during the split. Some replies may be stale. Once the network heals, copies that disagree must be reconciled by the system's own rule, such as keeping the write with the latest timestamp, or by returning the conflicting writes to the client to merge.

Neither choice is free. CP costs some availability. AP costs some consistency. Your job as a designer is to decide which cost the feature can carry.

## Why is the choice made per feature?

A common mistake is to label a whole system as CP or AP. In practice, the choice is made feature by feature, and sometimes operation by operation. One product can have a part that favors consistency and another part that favors availability. A shop might show product reviews that are a little old, while the stock count at checkout must be exact.

Why split it this way? Each feature has a different cost when it is wrong. A stale review costs very little. A wrong stock count can cost money, trust, and a refund. The cost of being wrong should decide the rule, not the name of the whole system.

> **Tip:** When you describe a design, name the choice for each feature. Say 'payments favor consistency and the feed favors availability.' That is clearer than saying 'the system is CP.'

### Same product page, two answers

Take one product page. The product name and photo can come from a copy kept close to the shopper, because a slightly old photo does no harm. The number of items left should come from a source that can confirm it, because a wrong number can sell the same item twice. The page is one screen, but it has an AP part and a CP part. Designers split it this way on purpose.

## A rule of thumb for choosing

How do you decide which side a feature should favor? Ask one question: what does it cost to show stale data, compared with showing an error or making someone wait? The answer usually points to a side.

- **Money and inventory lean consistency:** If a wrong number lets someone spend money twice or sell the last item twice, stale data is very costly. Choose CP and accept some waiting or errors.
- **Feeds and counters lean availability:** If a like count is off by a few for a short time, few people notice and nothing breaks. Choose AP and let the number catch up later.
- **Look at whether the mistake can be fixed:** If a wrong answer can be repaired later with little harm, AP is often fine. If it cannot be undone, such as a payment that was already sent, lean toward CP.

1. **Name the action:** Write the exact action, such as withdraw cash or show the like count.
2. **Name the worst stale case:** Ask what happens if the answer is old by a few seconds or a few minutes.
3. **Name the cost of an error or a wait:** Ask whether users can accept an error and retry, or whether waiting would hurt them.
4. **Pick the side and explain it:** If stale data is worse, choose CP. If an error or wait is worse, choose AP. Write one sentence that says why.

## Worked example: a shopping site in a split

Let us walk through a shopping site during a partition between two data centers. A data center is a building full of servers. Each one holds a copy of the product data, and the link between them has broken. Two parts of the same site behave differently.

1. **The link breaks:** The east data center cannot reach the west one. Both still serve customers, so the site stays up.
2. **The likes count is read:** A shopper opens a product page. The page asks for the like count. The east copy has an older count, and the page shows it. Nobody is turned away, and the number is only a little old. Likes are an AP feature.
3. **Checkout checks the stock:** The shopper clicks buy on the last item. Checkout must check the remaining stock. If the east side cannot confirm the count with the west side, checkout refuses or waits. It does not risk selling the same item twice. Stock is a CP feature.
4. **The link heals:** The data centers reconnect. The like count catches up to the true number. The item was never sold twice, because checkout waited for proof. The shopper who was asked to wait can try again.

Notice what each choice protected. The likes count protected the shopper's experience. The checkout protected the shop from selling one item twice.

## Try the idea in code

The sample below is a tiny model, not a real database. It shows how the same read can give a stale answer, an error, or the newest value, depending on the mode and on whether the network is split.

```python
def read_value(partitioned, mode, local_copy, latest):
    if not partitioned:
        return str(latest)
    if mode == "CP":
        return "error: cannot confirm"
    return "stale: " + str(local_copy)


print(read_value(True, "AP", 90, 100))
print(read_value(True, "CP", 90, 100))
print(read_value(False, "AP", 90, 100))

```

Output:

```text
stale: 90
error: cannot confirm
100

```

Read the output line by line. During the split, the AP mode answers with the old copy. The CP mode refuses to answer. When there is no split, both modes return the newest value.

## Quick checks

Try these two short scenarios. Each one needs the idea from this lesson to answer.

**Quick check:** A banking app runs servers in two regions. The regions lose contact with each other. A customer asks to withdraw cash at an ATM connected to region A. Region A's copy of the balance may be out of date. What should the system do?

A. Pay out at once from region A's balance, since the customer is waiting and the regions can be compared later
B. Refuse or delay until region B can confirm the balance, since a stale balance could let money be taken twice
C. Pay out a small amount, such as the daily average, and check the rest after the link heals
D. Show the old balance on the screen but block the cash until the next day

**Answer:** B. Money is a feature that favors consistency. A wrong balance could let the customer take more than they have, and that loss is hard to undo. So the system should choose CP and accept a short wait or an error. Paying first and checking later risks the bank's money.

**Quick check:** A video site shows view counts from two regions. The regions are split. Region A shows 1,190 views and region B shows 1,204. A viewer asks why the numbers differ. Which design fits the cost of being wrong?

A. Stop showing view counts anywhere until the regions agree, so no viewer ever sees a different number
B. Keep showing each region's count and let them converge later, since a small gap for a short time costs little
C. Refuse every page request until the link heals, so that no viewer can see an old count
D. Show the higher count everywhere, because the higher number is always the correct one

**Answer:** B. A view count is a counter. Being a little off for a short time costs almost nothing, and the count can catch up later. So this feature favors AP. Stopping the whole page would cost far more than a small gap in a number.

## Common mistakes to avoid

- **One label for the whole system:** Most products mix choices. Payments and feeds in the same app can follow different rules, so do not label the whole app with one word.
- **Thinking available means correct:** An available system can still answer with old data. Availability promises a response, not the newest value.
- **Thinking stale data is always a bug:** Some features accept stale data by design. It becomes a bug only when the feature needed the newest value, such as a stock count at checkout.

> **Tip:** For every feature, ask two questions: what is the worst stale answer, and what is the worst error or wait? Answer both before you choose a side.

## Where to go next

This lesson gave you the trade-off and a way to decide. Later lessons go deeper. They cover the formal CAP theorem, the different consistency models, ways to keep services up such as fail-over (moving work to a backup when a main node fails) and replication, the math behind uptime figures, and how to choose a database for each trade-off. As you read them, keep one question in mind: what does a wrong answer cost?

## Sources

- [Availability vs Consistency In System Design: A Practical Guide (System Design Handbook)](https://www.systemdesignhandbook.com/guides/availability-vs-consistency/)
- [CAP Theorem (Cycle.io learning article)](https://cycle.io/learn/cap-theorem)
- [CAP Theorem Real-World Examples: CP vs AP Databases (DesignGurus)](https://www.designgurus.io/answers/detail/what-is-availability-vs-consistency-in-terms-of-cap-theorem)
- [Understanding the CAP Theorem: Consistency, Availability, and Partition Tolerance in System Design (freeCodeCamp)](https://www.freecodecamp.org/news/understanding-the-cap-theorem-consistency-availability-and-partition-tolerance-in-system-design/)
- [CAP Theorem Explained (AlgoMaster blog)](https://blog.algomaster.io/p/cap-theorem-explained)
- [CAP Theorem & Strategies for Distributed Systems (Splunk)](https://www.splunk.com/en_us/blog/learn/cap-theorem.html)
- [CAP Theorem and Database Trade-offs (StackPractices guide)](https://stackpractices.com/guides/cap-theorem-guide/)
- [CAP Theorem Explained: Consistency vs Availability in Distributed Systems (BuildBeyondBackend)](https://buildbeyondbackend.com/blog/cap-theorem-explained/)
- [The CAP Theorem Explained Simply (Build Smart Engineering newsletter)](https://buildsmartengineering.substack.com/p/the-cap-theorem-explained-simply)
