Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

Handbooks  /  CP vs AP Databases
Engineering~9 min readComparison
Head to Head

CP vs AP databases: what happens during a partition?

CPvsAP

This trade-off only exists during an actual network partition. The rest of the time, a well-designed system can be both consistent and available — which is why the popular "pick 2 of 3, always" version of CAP theorem is a little bit wrong.

01

The one distinction that decides everything

Rooted in CAP theorem: during a network partition, a distributed database must choose between Consistency (every read sees the latest write, but the system may refuse some requests) and Availability (every request gets a response, but it might be stale). CP systems pick consistency under a partition; AP systems pick availability. Partition tolerance itself is treated as a given — real networks partition, so it isn't the choice being made.

→ The rule

Is stale data worse than no data for this piece of state? CP. Is refusing service worse than briefly-stale data? AP. This decision is made per data type, not once for a whole system.

02

Head to head

DimensionCPAP
Under a partitionSome nodes refuse to serve rather than risk stale dataAll reachable nodes keep serving, possibly stale/conflicting data
Consistency modelStrong / linearizableEventual — reads may be stale until convergence
Example systemsZooKeeper, etcd, HBase, Spanner (with tight clock sync)Cassandra, DynamoDB (default), CouchDB, Riak
Conflict handlingPrevented by design — writes serialized via consensusAllowed, then resolved — LWW, vector clocks, CRDTs
Write availability during partitionMinority side can’t write — waits or errorsEvery reachable node accepts writes, always
Read guaranteeLinearizable reads (served via the leader or a quorum) return the latest committed value or an error — never staleMay return an outdated value, but always returns something
Best forFinancial ledgers, config stores, distributed locksShopping carts, social feeds, session data
03

When to reach for each

Reach for CP

  • Correctness of the value matters more than uptime
  • Bank balances, distributed locks, leader election
  • Inventory counts where overselling is a real cost
  • Config that must never diverge across nodes

Reach for AP

  • Uptime matters more than perfect freshness
  • A briefly-stale shopping cart or feed is fine
  • Refusing service would be worse than serving old data
  • Per-query tunable consistency is available (quorum reads/writes)
04

Most real systems pick per data type, not once

The CP/AP label rarely applies uniformly across a whole product. An e-commerce platform commonly runs the shopping cart AP (a brief hiccup shouldn't block adding an item) and the final checkout/inventory-decrement CP (that specific write genuinely can't tolerate a stale double-sell). Many "AP" systems like Cassandra also let you tune consistency per query via quorum settings — meaning the CP/AP label often describes a DEFAULT or an extreme end of a dial, not a permanent, unchangeable binary choice.

→ The practical shape

Ask the question per piece of state, not per system: "if this specific write is briefly unavailable during a partition, is that better or worse than it being briefly wrong?"

05

This trade-off only bites during an actual partition

It's worth being precise about what CAP theorem actually constrains, because it's commonly overstated. The forced choice between consistency and availability applies ONLY during an actual network partition — nodes genuinely unable to reach each other. Most of the time, with no partition happening, a well-designed system can be both consistent and available simultaneously; there's no theorem forcing a permanent sacrifice of one for the other in normal operation. The popular shorthand "pick 2 of 3, always" is a mis-simplification of a narrower, more useful statement: partitions WILL eventually happen in any real distributed system — that part isn't optional or a choice you get to avoid by careful engineering — and CAP describes the forced choice you must make specifically for the DURATION of that partition, not a permanent architectural ceiling you live under forever.

This precision matters practically: a system doesn't need to decide "I am a CP system" or "I am an AP system" as an eternal identity — it needs to decide, for each piece of state, what should happen in the specific window where nodes can't talk to each other. That's a much narrower, more answerable design question than the theorem's popular framing suggests.

→ The trade you’re actually making

You're not choosing consistency or availability forever — you're pre-deciding what a specific piece of data should do during the finite window of an actual partition, which is a much smaller and more tractable decision than it sounds.

06

A worked scenario: cart vs checkout on the same platform

An e-commerce platform's shopping cart service is built AP (Cassandra/DynamoDB-style): if a data-center link partitions, the cart keeps accepting adds and removes on both sides of the partition and reconciles later — refusing to let someone add an item during a brief network hiccup would just lose a sale for no real benefit, since a cart being briefly inconsistent across replicas costs nothing serious.

The SAME platform's checkout-time inventory decrement is built CP, routed through a strongly-consistent store or leader — because during that same partition, letting both sides independently decrement the last unit of a sold-out item would double-sell it, a real, costly correctness failure. Refusing the write until consistency is restored — telling some users "try again" — is the correct trade here, even though it's the opposite choice the cart made one service over. Same platform, same partition event, two deliberately different answers.

→ The pattern generalizes

The question was never "is this platform CP or AP" — it was "for this specific write, is stale worse than unavailable," asked and answered separately for the cart and for the inventory decrement.

The picture

The picture: one partition, two behaviours

majority side: N1 N2 N3 minority side: N4 N5 CP AP leader + 3 voteswrite x=2 commitsreads see x=2 no majoritywrite x=3 refused or times outlinearizable reads error keeps servingwrite x=2 acceptedreads see x=2 keeps servingwrite x=3 acceptedreads see x=3: diverged on heal: CP minority copies x=2; AP must merge 2 vs 3
The same partition, two contracts. A CP store keeps one history by letting only the side with a majority decide. The two nodes that cannot reach it stop accepting writes, and their clients see errors or timeouts. An AP store lets both sides keep going, so a client on each side gets an answer, but the two sides can disagree. The bill arrives at heal time: the CP minority just catches up, while the AP system needs a rule for picking or merging the two values. That rule decides whether a write is lost. Notice that in the CP row, N4 and N5 have not crashed. They are running, their clients can reach them, and they refuse on purpose. Those clients need a timeout, a retry and a degraded path, not an endless wait.
Watch for

Common mistakes

Most CP/AP bugs are not a wrong choice of database. They come from assuming the label promises more than it does. Each of these lives at an edge the label hides: a write that half-failed, a replica that lags, two clocks that disagree, a request that lands on a different node.

  • Treating QUORUM as CP

    Cassandra at QUORUM for reads and writes gives R + W > N, so every read set overlaps the latest write set. That is not linearizable. A write that failed after reaching one replica is not rolled back, and a later read can return it. Concurrent writes are settled by timestamp, so one silently loses. On the minority side of a partition, QUORUM requests simply fail. You lose availability and still don’t get consistency. For a real compare-and-set, use a lightweight transaction (IF clauses, built on Paxos), or a CP store.

  • Assuming every CP read is fresh

    The head-to-head table’s “never stale” holds only for linearizable reads, the ones that go through the leader or a quorum, and not every CP read is one. ZooKeeper answers reads from whichever server the client is connected to, and that server can lag; call sync first when it matters. An etcd “serializable” read is answered by the local member and can be stale too. Even a leader answering from local state can be stale if it has been deposed without knowing. CP describes the writes; check how your reads are served.

  • Last-write-wins on data that must not lose writes

    Last-write-wins keeps whichever write has the later timestamp and throws the other away, with no error. Clock skew between nodes can even make the older write win. For a cart, two adds on two sides of a partition become one add. Merge instead: the Dynamo paper merged carts by taking the union, and accepted that a deleted item could reappear. CRDTs make that kind of merge exact. If you must use last-write-wins, take timestamps from one well-behaved source, such as a hybrid logical clock, and log the losing writes so a lost update can at least be found.

  • Forgetting the cost when nothing is broken

    CAP only forces a choice during a partition, but strong consistency is not free the rest of the time. A linearizable write must reach a majority, so if replicas sit in different regions, every write pays a cross-region round trip. The PACELC framing names this: when there is a Partition, choose Availability or Consistency; Else, choose Latency or Consistency. The latency side of that is the one you pay every day. So ask two questions of each piece of state: what should it do during a partition, and how much latency is consistency worth on every normal request?

  • Breaking read-your-writes on an AP store

    A user saves their profile, the next request goes to a different replica, and the old profile comes back. To them that looks like the save failed, and they save again. AP stores need session guarantees for this: route a user’s reads to the replica that took their write, read at a stronger level right after a write, or carry a version token the replica must reach before it answers. Monotonic reads is the sibling bug: a refresh lands on a staler replica, and a comment the user already saw disappears.

In the wild

Where this shows up in production and interviews

Real databases rarely sit at one end of the dial. Each of these exposes the choice as a setting, made per table or per request, which is the page’s per-data-type rule built into the API.

  • DynamoDB: a dial per request

    DynamoDB reads are eventually consistent unless the request sets ConsistentRead, and an eventually consistent read costs half the read capacity. That is the trade-off with a price on it. Global tables replicate between regions asynchronously and settle conflicting writes by last writer wins. So data written in two regions at once has the cart problem above. Queries on a global secondary index are always eventually consistent; the flag does not apply there.

  • Cassandra: the level is a per-query choice

    ONE, QUORUM, LOCAL_QUORUM and ALL trade latency and availability against overlap with the latest write. Multi-datacenter deployments commonly use LOCAL_QUORUM so a request never waits on another region. Writes that must not race, such as claiming a username, use a lightweight transaction. A quick check: with replication factor 3, QUORUM is 2, so one replica can be down and both reads and writes still succeed. ALL fails as soon as any one replica is unreachable.

  • Spanner and CockroachDB: CP SQL

    Both replicate each piece of data through consensus, so the side of a partition without a majority cannot write it. Spanner also waits out its clock uncertainty before acknowledging a commit (“commit wait”), so timestamps match real time order. That wait is the latency cost from the PACELC mistake above, paid on every commit to keep consistency. CockroachDB serves reads from each range’s leaseholder, and offers follower reads as an opt-in for queries that can accept slightly stale data in exchange for a nearby replica.

  • The interview version

    In “design a key-value store” or “design a shopping cart”, the interviewer is waiting for the partition question. Answer per piece of state, as the page argues. Then go one level deeper: give N, R and W, say how conflicts resolve (last-write-wins, vector clocks or a merge), and say what a client on the minority side sees. For the cart, that might be N = 3 with W = 2 and R = 2 normally, so R + W > N. During a partition, accept writes on either side and merge by union when it heals. Checkout reads stock from the CP store. Ending with “we use Cassandra, so it’s AP” misses the point.

Your call

Which would you pick?

Three situations. Pick the side you'd actually build — the explanation follows.

A payments ledger. A network partition splits your cluster and a write arrives on the minority side.

A "last seen online" indicator on a social profile, replicated across three regions, during a partition between two of them.

A colleague says "we chose CP, so the system is consistent — we do not need to think about partitions."

Frequently asked

Quick answers

CP or AP — which should I use?

Decide per data type: if stale data is worse than no data (financial balances, distributed locks, inventory counts), choose CP. If refusing service is worse than briefly-stale data (shopping carts, feeds, session state), choose AP. Most real systems use both, for different pieces of state.

Does CAP theorem mean I always sacrifice consistency or availability?

No — the forced trade-off only applies during an actual network partition. Without a partition, a well-designed system can be both consistent and available. The theorem describes the choice you must make for the duration of a partition, not a permanent limitation.

Can a system be both CP and AP?

Not for the same piece of data during a partition — but a single system commonly runs different services or data types with different CP/AP choices (e.g. an AP cart and a CP checkout), and many AP systems let you tune consistency per query via quorum settings.

What happens to a CP system during a partition?

The minority side of the partition (the side that can’t confirm it has the latest state via consensus) refuses to serve some requests — typically writes, sometimes reads — rather than risk returning or accepting a value that could conflict once the partition heals.

▶  Watch it explained

CP vs AP databases: what happens when the network splits?

CP vs AP Databases · Vibe Engines · 2026
Finished this one? 0 / 209 Handbooks done
NextPartitioning, Sharding & Replication Next in “Distributed Systems Deep Dive” · step 3 of 20

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Handbooks