CAP, PACELC and consistency models

Lesson 4.5 · 11 min read · by Vinay C

We'll cover the following

Visual summary

Infographic summarizing CAP, PACELC and consistency models: Your app keeps user profiles in two data centers, one in the US and one in Europe, so each region is fast and one can…

Download imageFree to share, please link back to this lesson.

The problem: two data centers lose contact

Your app keeps user profiles in two data centers, one in the US and one in Europe, so each region is fast and one can fail without taking the app down. Every write is copied to the other side. Now the cable between them is cut. Both data centers are still running, but neither can talk to the other.

A user in Europe changes their profile picture. The European copy cannot tell the US copy. What should the system do? It has only two honest options:

  • Refuse: return an error (or wait) until the link heals, so no one ever sees two different pictures. You stay consistent but not available.
  • Accept: save it in Europe and say OK. The US keeps showing the old picture until the link heals. You stay available but not consistent.

That choice is the CAP theorem. Everything else in this lesson is about making that choice deliberately, one piece of data at a time.

CAP, stated precisely

A partition is a network failure that splits nodes into groups that cannot talk. You do not choose whether partitions happen. Networks fail, so partition tolerance is a given. CAP really says: when a partition happens, you choose C or A. With a healthy network you can have both. "Pick 2 of 3" oversimplifies, because P is not on the menu.

  • CP: refuse or error rather than return a possibly stale answer. Fits payments and inventory.
  • AP: always answer, even if the answer may be stale. Fits feeds and likes.

Eventual consistency is not just for read-heavy systems. It is about how much staleness you can tolerate. The two ends look like this when a write succeeds. Strong: the 200 means the write is on enough replicas (usually a quorum, explained below) that any later read sees it. Eventual: the 200 comes after one replica has it, and the others catch up.

PACELC: what about when nothing is broken?

Partitions are rare. Most of the time the network is fine, and CAP says nothing about that time. PACELC (say "pass-elk") fills the gap: if Partition, choose A or C; Else, choose Latency or Consistency.

The plain reason: to be more certain every copy has your write, you must wait for more replicas to confirm it, and waiting takes time. Even on a healthy network, more consistency costs speed. DynamoDB default reads are fast and eventually consistent. Spanner is consistent but pays in latency.

Consistency is a setting, not a product label

Do not say "Cassandra is AP" or "Postgres is CP" as if it were fixed. The same database can behave differently depending on how you configure and query it:

  • Postgres with a single primary and sync replicas behaves CP. With async replicas, replica reads are stale (eventual), but writes still stop if the primary dies.
  • Distributed SQL (Spanner, CockroachDB) is firmly CP.
  • Cassandra with QUORUM reads and writes acts CP for that query. Drop to ONE and it is AP.
  • DynamoDB and Cassandra let you pick per query.

Quorum: how the dial actually works

Data is copied to N replicas (say 3). W is how many replicas must confirm a write. R is how many replicas you ask on a read. If R + W > N, the write group and the read group must share at least one replica, so a read always touches a node that has the latest write.

Replicas A, B, C all hold picture v1. A user writes v2:

N=3, W=2, R=2   (R + W = 4 > 3)
  write v2: A=v2, B=v2 -> acked after 2.   C is slow, still v1.
  read asks any 2:  {A,B} -> v2, v2   ok
                    {A,C} -> v2, v1   newest wins -> v2
                    {B,C} -> v2, v1   newest wins -> v2
  Every pair overlaps A or B, so the read always sees v2.
  One node can be down and both reads and writes still work.

N=3, W=1, R=1   (R + W = 2 <= 3)
  write v2: A=v2 -> acked after 1.   B, C still v1.
  read asks C -> v1   STALE. Faster, but not safe.

So N=3, W=2, R=2 gives strong consistency and survives one node down. W=1, R=1 is faster but can read stale.

Read-your-own-writes: eventual is fine, except for me

Back to the profile picture. Other users may see the old one for a few seconds, and that is fine. But the person who just uploaded it must see the new one immediately, or they will think the upload failed. This is a session guarantee: weaker than strong consistency for everyone, but exact for the one user who wrote. Three ways to get it:

  • Read that user's own data from the primary for a short window after they write.
  • Update the UI optimistically, showing the new picture before the server confirms.
  • Pass a version or timestamp with the read, so a lagging replica refuses to serve and the request goes elsewhere.

What CP vs AP changes beyond a toggle

AreaWhat you decide
ReplicationSynchronous (CP-ish) vs asynchronous (fast, AP-ish).
QuorumsChoose R and W.
Failure behaviorReject during a partition vs keep serving stale.
Conflict handling (AP)Both sides accepted writes, so you need last-write-wins, vector clocks or CRDTs.
CachingTTLs and invalidation are consistency decisions. Cache seat availability for display with a short TTL, but never trust the cache for the booking itself. The final check hits the DB in a transaction.
Multi-regionSingle leader or active-active (next section).
Cross-service flows2PC or sagas (see Transactions across services).
UXOptimistic UI, "pending" states, retries and idempotency.

Multi-region: single leader vs active-active

Our two data centers are the multi-region case. You can let one region own all writes, or let both accept them.

Single leaderActive-active
WritesAll go to one region (e.g. US). Far users pay about 200ms.Each region accepts writes locally.
ProsOne source of truth, no conflicts.Fast everywhere.
ConsSlow for distant users.The same record edited in two regions at once means conflicts you must merge.
FitsTicket booking.Collaborative or feed-like data.

Pick consistency per piece, not per system

A common mistake is answering a CAP question with one word for the whole system: "I pick availability." Real systems are made of many small pieces, and each can choose differently. Ask: which piece of data must everyone agree on, and how big is that piece?

In a chat app, what must agree is the order of messages inside one conversation, not across every conversation in the world. So give each conversation a single owner, one server that numbers its messages. That owner keeps the order consistent.

Now the partition case. If that owner is unreachable for a moment (a crash, a failover), that one conversation briefly stops accepting new messages rather than risk two different orders. That is the CP choice, but only for that conversation. Every other conversation has its own owner and keeps working, so the system as a whole stays available. How the owner numbers messages is covered in Ordering, acks and delivery guarantees.

Check yourselfA product page shows seats left on a flight, and users can book seats. Where do you use CP and where AP?

The displayed count can be AP: cached with a short TTL, slightly stale is harmless. The booking itself is CP: the final check hits the database in a transaction so the same seat is never sold twice, and saying "try again" during a failure is better than a double sale.

Check yourselfWith N=3, why does W=2, R=2 never read stale, but W=1, R=1 can?

R + W > N. With 2 + 2 = 4 > 3, any 2 replicas you read must overlap with the 2 that confirmed the write, so one of them has the newest value. With 1 + 1 = 2, the one replica you read may be a different one from the one that took the write.

Check yourselfA user posts a comment and refreshes, but it is missing because the read hit a lagging replica. How do you fix it without making every read strongly consistent?

Give that user read-your-own-writes: read their own data from the primary for a short window after they write, update the UI optimistically, or pass a version so a lagging replica refuses to serve. Everyone else can stay eventual.

Summary: say it in the interview

  • "I pick consistency per piece, not per system. Payments and inventory are CP with sync replication. Feeds and likes are AP with async replicas, plus read-your-own-writes for the user's own actions. In chat, order is CP per conversation, so one conversation pauses during its owner's failover while all others stay up."