The CAP theorem says that when a network partition splits a distributed system, the system cannot give both consistency (every read sees the latest confirmed write) and availability (every working node still answers) at the same time. Partitions will happen, so the real design question is simple: during a split, should an operation refuse to stay correct, or answer and risk old data?

In this article we follow one example the whole way: a movie ticket booking app that runs in two data centres, one in Hyderabad and one in Mumbai. Seat booking, showtimes, the watchlist and likes all live in the same system, yet each one needs a different answer to the CAP question. Every idea comes with runnable Java code, plus Python and C++.

Key takeaways

  • CAP is about replicated data during a network partition, not about normal days.

  • C in CAP means a read never returns a value older than a confirmed write. It is not the C in ACID.

  • A in CAP means every request to a working node gets a non-error answer. It is not the same as 99.99% uptime.

  • Partitions cannot be avoided, so "pick any two" is misleading. The choice is CP or AP, and it can differ per operation.

  • Quorums, majority votes, merges and version vectors are the tools that put CP and AP into practice.

  • PACELC adds the everyday trade-off: even without a partition, stronger consistency costs latency.

1. What Is the CAP Theorem?

Eric Brewer stated the idea in 2000, and Seth Gilbert and Nancy Lynch proved it formally in 2002, which is why it is also called Brewer's theorem. It talks about three properties of a system that keeps copies (replicas) of the same data on several machines:

LetterPropertyPlain meaningMovie app example
CConsistencyAfter a write is confirmed, every read returns that value or a newer oneIf Asha books a seat, nobody anywhere sees that seat as free
AAvailabilityEvery request that reaches a working node gets a real answer (not an error)Mumbai answers "seats left" even when it cannot reach Hyderabad
PPartition toleranceThe system keeps running when messages between nodes are lost or delayedBoth data centres stay up when the link between them breaks

The theorem says: while a partition is happening, an operation can have C or A, not both. When the network is healthy, a well-built system usually gives both.

2. The Booking App We Will Use

Users are sent to the nearest data centre by geo DNS. Each data centre runs the booking service and keeps a full copy of the database. A replication link copies every change from one city to the other.

The app has four kinds of data, and they will not all get the same treatment:

FeatureWhat goes wrong if it is staleHow bad is that?
Seat booking and paymentThe same seat is sold to two peopleVery bad: refunds, angry customers
Showtimes and movie pagesA user sees a show time that changed a minute agoMinor, it fixes itself quickly
WatchlistA movie added on the phone does not show on the laptop yetMinor, as long as nothing is lost
Likes on a trailerThe count is a little behindHarmless

3. What Happens During a Network Partition?

A network partition means the nodes are still running, but some of them cannot talk to the others. A cut fibre between cities, a bad router, a wrong firewall rule or a long garbage collection pause that makes a node miss heartbeats can all cause one. It may last a few seconds or a few hours.

Suppose the 9 PM show has 4 seats left on both copies. The link between the cities breaks. Asha books a seat in Hyderabad, so Hyderabad now says 3. That update cannot reach Mumbai, which still says 4. Then Ravi asks Mumbai how many seats are left.

Mumbai has exactly two options, and there is no third one:

The two choices in Java

This program books a seat, cuts the link, books again in Hyderabad and then reads from Mumbai in both modes.

Output

Booked 1 seat at Hyderabad -> seats left 4 (v2)
  replicated to Mumbai
--- partition: Hyderabad <-x-> Mumbai ---
Booked 1 seat at Hyderabad -> seats left 3 (v3)
  replication to Mumbai FAILED: link is down
CP read at Mumbai: ERROR 503 (cannot confirm the latest value)
AP read at Mumbai: seats left 4 (v2)
--- link restored, Mumbai catches up ---
CP read at Mumbai: seats left 3 (v3)

The CP read refuses with an error because Mumbai cannot prove it has the latest value. The AP read answers at once, but with the old count of 4 seats while the truth is 3. After the link comes back, Mumbai catches up and both copies agree again. No clever protocol can avoid this choice: a node cannot learn about a write that never reached it.

4. Consistency in CAP (and How It Differs From ACID)

CAP consistency, also called linearizability or strong consistency, means the replicated data behaves like one single, up-to-date copy. Once a booking is confirmed, any read that starts after that moment, in any city, must see it. One simple way to get this is to confirm a write only after the other replica has it too:

Do not mix this up with the C in ACID, which is a different idea that happens to use the same word:

ACID consistencyCAP consistency
AboutRules inside one databaseAgreement between copies of data
Example ruleSeats left can never be negativeA read after a booking sees that booking
Broken whenA transaction leaves data breaking a ruleA replica returns a value older than a confirmed write

5. Availability in CAP (and How It Differs From Uptime)

CAP availability is about single requests: every request that reaches a node that has not crashed must get a non-error response. An old value counts as available. A slow answer counts too. An error like 503 Service Unavailable from a healthy node does not count.

This is narrower than the availability you measure with nines, such as 99.99% uptime per month, which we covered in the Availability article. A CP system can still have excellent uptime overall; it just rejects some requests during the minutes a partition lasts.

6. Partition Tolerance: Why "Pick Two" Is Misleading

CAP is often drawn as a triangle where you "pick any two". That picture suggests three options: CA, CP and AP. In a system that spans more than one machine, CA is not a real option, because you cannot promise the network will never fail. Choosing "CA" only means you have not decided what happens during a partition.

A single database server on one machine can be called CA, because there is no network between replicas to break. As soon as you add a replica in another city, partitions become possible and you must pick CP or AP. And you do not have to pick once for the whole system: each operation can choose differently, as the booking app will show.

OperationDuring a partitionWhy
Book seat A7CP: refuse unless a majority agreesSelling one seat twice is worse than a "try again" message
Pay for the bookingCP: refuse or waitMoney must never be charged twice or lost
Show movie timesAP: answer from the local copyA slightly old page is fine
Add to watchlistAP: accept, merge laterLosing an addition is worse than a short mismatch
Like a trailerAP: accept, add up laterCounts can be merged exactly

7. Quorums: Tuning Between CP and AP

Many databases (Cassandra, DynamoDB-style systems and others) keep N copies of each item and let you choose two numbers: W, how many copies must confirm a write, and R, how many copies a read asks. If R + W > N, every read set overlaps every write set, so at least one copy in the read has the newest version.

Output

N = 3 replicas. A partition leaves 2 nodes on one side and 1 on the other.
R=1 W=1 | strong reads: no  | 2-node side: read yes write yes | 1-node side: read yes write yes
R=1 W=3 | strong reads: yes | 2-node side: read yes write no  | 1-node side: read yes write no 
R=2 W=2 | strong reads: yes | 2-node side: read yes write yes | 1-node side: read no  write no 
R=3 W=1 | strong reads: yes | 2-node side: read no  write yes | 1-node side: read no  write yes

The table shows the trade-off clearly. R=1 W=1 is fast and keeps working on both sides of a split, but reads can be stale (AP). R=2 W=2 gives strong reads, but only the side with 2 nodes keeps working; the lonely node refuses both reads and writes (CP). In Java, the Cassandra driver exposes this per query, for example ConsistencyLevel.QUORUM for seat booking and ConsistencyLevel.ONE for likes.

8. CP in Practice: Booking a Seat With a Majority Vote

For seat booking we need CP. A common way is to let a booking succeed only when a majority of nodes agree. Consensus algorithms such as Raft and Paxos work this way, and so do stores built on them, like etcd and ZooKeeper. In any split, at most one side can hold a majority, so two sides can never both sell the same seat.

Output

CP mode (a booking needs votes from a majority, 2 of 3):
  Asha via Hyderabad side (2 nodes): seat A7 CONFIRMED
  Ravi via Mumbai side (1 node): seat A7 REJECTED (only 1 of 2 votes), please retry
  Seat A7 sold twice: false
AP mode (any reachable node may confirm):
  Asha via Hyderabad side (2 nodes): seat A7 CONFIRMED
  Ravi via Mumbai side (1 node): seat A7 CONFIRMED
  Seat A7 sold twice: true

With the majority rule, Asha's booking on the 2-node side goes through and Ravi's booking on the 1-node side is rejected with "please retry". Without it (AP mode) both are confirmed and seat A7 is sold twice. In a Java service, the rejected case should become a clear message to the user, such as "Booking is busy, please try again in a moment", not a stack trace.

9. AP in Practice: Accept Now, Merge Later

For the watchlist we prefer AP. Both data centres keep accepting changes during the split, and the copies are reconciled when the link returns. This is called eventual consistency: if writes stop, all copies will end up the same.

Output

During partition  HYD: [Kalki 2, Salaar]  MUM: [Pushpa 3, Salaar]
After merge (union): [Kalki 2, Pushpa 3, Salaar]
Last-writer-wins: keeps "Hyderabad", silently drops "Pune"

The watchlist is a set, so merging is easy: take the union and nothing is lost. A single value such as the preferred city is harder. Last-writer-wins keeps the change with the newer timestamp and silently throws the other away, here "Pune". That may be fine for a city, but it would be wrong for anything important, and clocks on two machines are never perfectly in sync.

10. Detecting Conflicts With Version Vectors

To merge correctly we first need to know whether two copies really conflict. A version vector stores, for each replica, how many writes it has accepted, for example [HYD=2, MUM=1]. Comparing two vectors tells us whether one copy is simply newer or whether both were changed independently during the split.

Output

A [HYD=2, MUM=1] vs B [HYD=1, MUM=1] -> A is newer, keep A
A [HYD=1, MUM=1] vs B [HYD=1, MUM=2] -> B is newer, keep B
A [HYD=2, MUM=1] vs B [HYD=1, MUM=2] -> CONFLICT: concurrent writes, the app must merge
A [HYD=3, MUM=2] vs B [HYD=3, MUM=2] -> same version

Only the concurrent case needs a merge rule, and the rule depends on the data: a union for sets, adding both sides for counters such as likes, and an application rule (or asking the user) for single values. Data types designed to merge without conflicts are called CRDTs (conflict-free replicated data types); a grow-only set and a per-node counter are the simplest ones.

11. PACELC: The Trade-off When There Is No Partition

CAP only talks about partitions, which are rare. PACELC, proposed by Daniel Abadi, adds the everyday case: if there is a Partition, choose Availability or Consistency; Else, choose Latency or Consistency. Even on a normal day, waiting for the leader or a quorum makes reads slower than reading the nearest copy.

Output

Reads: 30, replication lag: 300 ms
Read from Mumbai replica : 5 ms each, stale answers 9 of 30
Read from Hyderabad leader: 25 ms each, stale answers 0 of 30
Extra wait for always-fresh data: 600 ms in total

Reading showtimes from the Mumbai replica is 5 times faster, but 9 of 30 answers were a few hundred milliseconds old. For showtimes that is a good deal (EL). For "seats left" just before payment, the booking service should read from the leader (EC). This is why the latency numbers and the CAP choice must be planned together.

12. Where Real Databases Sit

Database products are not simply "CP" or "AP". Most of them let you choose per query or per collection, so look at the setting you use, not the brand name:

Database / settingDuring a partitionTypical use in the booking app
PostgreSQL or MySQL with a synchronous replicaWrites wait or fail if the replica is unreachable (CP)Bookings and payments
etcd, ZooKeeper (Raft / ZAB consensus)Only the majority side accepts writes (CP)Seat locks, leader election
Cassandra with QUORUMMinority side rejects (CP for that query)Seat counts
Cassandra or DynamoDB with ONE / eventual readsBoth sides answer, may be stale (AP)Likes, recently viewed
MongoDB with majority write concernWrites need a majority (CP)Orders

13. How to Apply CAP in a System Design Interview

  1. List the operations, not the database: book a seat, view showtimes, add to watchlist.

  2. Ask what a stale answer costs for each one: money lost, user confusion, or nothing.

  3. Choose CP or AP per operation, and say what the user sees during a split ("please retry" or slightly old data).

  4. Name the mechanism: majority writes or quorum reads for CP; local writes plus a merge rule for AP.

  5. Mention PACELC: where you accept slightly old reads for speed on normal days.

Common Mistakes

  • Saying "we pick CA". In a distributed system partitions will happen, so CA is not a choice.

  • Labelling a whole database CP or AP. Most databases are tunable; the label belongs to an operation and its settings.

  • Mixing CAP consistency with ACID consistency. One is about copies agreeing, the other about rules inside one database.

  • Treating CAP availability as uptime. CAP availability is about every request during a partition.

  • Choosing AP without a merge plan. Last-writer-wins silently loses data; decide the merge rule for each data type.

  • Forgetting the normal case. PACELC reminds you that strong consistency adds latency even without failures.

Interview Questions

  • What does the CAP theorem say? During a network partition, a replicated system cannot guarantee both consistency and availability for the same operation; it must refuse some requests or risk stale answers.

  • Why is CA not possible for a distributed system? Because the network between nodes can always fail. A system that claims CA has simply not defined its behaviour during a partition.

  • Is MongoDB (or Cassandra) CP or AP? It depends on the settings: with majority writes and reads it behaves CP; with single-node reads and writes it behaves AP.

  • How does a quorum give strong consistency? If R + W > N, every read overlaps the latest write on at least one replica, so the reader can pick the newest version.

  • How would you design seat booking across two regions? Use a majority-based store or a single leader for seat state, reject bookings on the minority side, and show a retry message.

  • What is PACELC? If Partition, choose Availability or Consistency; Else, choose Latency or Consistency.

Summary

ChoiceDuring a partitionGood forTools
CPRefuse or wait, never return stale dataSeat booking, payments, inventory, locksMajority writes, quorum reads, consensus (Raft)
APAlways answer, may return old dataFeeds, likes, watchlists, product pagesLocal writes, version vectors, merges, CRDTs
PACELC (no partition)Latency vs consistencyReads that can be a little oldNearest replica, caches, read-your-writes where needed
  • CAP is about what replicated data does during a network partition.

  • Partitions are unavoidable, so the real choice is CP or AP, made per operation.

  • CP keeps data correct by refusing some requests; AP keeps answering and merges later.

  • Quorums (R + W > N) and majority votes are how CP is built; version vectors and merge rules are how AP stays safe.

  • PACELC adds the everyday trade-off between latency and consistency.

Previous topic: Latency vs Throughput vs Bandwidth. Next: Consistency Models.