CAP, PACELC & consistency models
Explain strong vs eventual consistency and quorum reads and writes.
By Invicly Tech, , in System Design Deep Dive. About 12 minutes to read.
In short
CAP says that during a network partition a distributed system must choose consistency or availability. PACELC adds that even without partitions you trade latency against consistency. Quorums let you tune where you sit.
Explained simply
When data is copied to several machines, a question appears: if I write a value and then read it, will I always see my new value? Consistency models are the rules that answer this. They describe what a reader is allowed to see while copies are being updated.
Strong consistency means the system behaves as if there were only one copy. Once a write is done, every reader sees it. Eventual consistency means the copies may disagree for a short time, but if writes stop, they all end up the same. Strong is easier to reason about; eventual is faster and keeps working during failures.
The CAP theorem explains why you cannot always have both. If the network between machines breaks, called a partition, each machine must choose: answer requests with possibly old data (stay available), or refuse until it can talk to the others (stay consistent). Networks do break, so this choice is real.
PACELC adds the everyday case. Even when the network is fine, waiting for other copies to agree takes time. So you trade speed against consistency all the time, not just during failures. Good designs make this choice per feature: strict for payments, relaxed for like counts.
Press Play on a diagram to walk through it one step at a time.
- Reads see the latest write
- Coordination on every write
- Higher latency
- Banking, inventory, locks
- Replicas converge over time
- Reads may be stale
- Low latency, highly available
- Likes, feeds, view counts
- Linearizable
- Strongest
- Acts like one copy
- Sequential
- One agreed order of operations
- Causal
- Cause seen before effect
- Session guarantees
- Read-your-writes
- Monotonic reads
- Eventual
- Weakest
- Converges if writes stop
- Minority side rejects requests
- No stale or conflicting data
- Some users see errors
- etcd, ZooKeeper, Spanner
- Both sides keep answering
- Reads may be stale
- Conflicts merged after healing
- Cassandra at ONE, DNS
In depth
What CAP actually means
CAP is often summarised as pick two of three, which is misleading. In the theorem, C means linearizability (every read sees the latest completed write, as if there were one copy), A means every request to a working node gets a non-error answer, and P means the system keeps running when messages between nodes are lost.
You cannot opt out of partitions, so the real choice only appears when one happens. A CP system refuses some requests during the partition to avoid stale or conflicting data: etcd, ZooKeeper and a PostgreSQL primary with synchronous replicas behave like this. An AP system keeps answering on both sides and reconciles later: Cassandra at consistency level ONE, or DNS.
Most of the time there is no partition at all, which is why CAP alone says little about everyday behaviour.
- Client to Node A: Write x = 21Write x = 2
- Node A to Node B: Replicate x = 2 (lost: network split)2Replicate x = 2 (lost: network split)
- Client to Node B: Read x3Read x
- Node B to Client: CP: error, cannot confirm latest4CP: error, cannot confirm latest
- Node B to Client: AP: x = 1, possibly stale5AP: x = 1, possibly stale
PACELC: the everyday trade-off
PACELC reads: if there is a Partition, choose Availability or Consistency; Else, choose Latency or Consistency. The second half matters more day to day, because waiting for other replicas costs a network round trip, and across regions that is tens to hundreds of milliseconds.
Real systems expose this as a setting. DynamoDB reads are eventually consistent by default; a strongly consistent read (ConsistentRead set to true) returns the latest value but uses twice the read capacity and is not supported on global secondary indexes. Cassandra lets you choose a consistency level per query. Google Spanner chooses consistency in both cases and uses tightly synchronised clocks (TrueTime) to keep the latency cost acceptable.
So the useful question is not which letter a database is, but what each read and write in your app actually needs.
- Cassandra and DynamoDB defaults: PA/EL (fast, may be stale)
- Spanner, etcd: PC/EC (consistent, pays latency)
- Many systems let you choose per request
Quorum math and where it falls short
With N replicas, a write waits for W acknowledgements and a read asks R replicas. If W + R > N, every read set overlaps every write set in at least one replica, so the read can see the latest acknowledged write. A majority quorum is floor(N/2) + 1: 2 of 3, or 3 of 5. Each setting has a failure budget: with N = 3, W = 2 and R = 2, one replica can be down and both reads and writes still work.
Quorums alone do not give full linearizability. A write that reaches only one replica before failing can still be seen by some reads and not others. Concurrent writes need a rule like last-write-wins, which can drop data. Sloppy quorums (writing to substitute nodes during failures) break the overlap guarantee. Cassandra exposes these choices as ONE, QUORUM, LOCAL_QUORUM and ALL.
- 1Client asks coordinator
Read key x at QUORUM
- 2Query replicas
Send to replicas holding x
- 3Wait for 2 replies
Ignore the slowest one
- 4Pick newest version
Compare timestamps or versions
- 5Repair and reply
Fix a stale replica, return value
The models in between
Between strong and eventual there are useful middle levels. Causal consistency means if one action could have caused another, everyone sees them in that order: a reply never appears before the message it answers. Unrelated actions may appear in different orders to different users. It can be provided without the cost of global coordination, which makes it attractive for social and collaborative apps.
Session guarantees are promises to one user. Read-your-writes: you always see your own changes. Monotonic reads: once you have seen a value, you never see an older one. Monotonic writes: your writes are applied in the order you made them. Writes-follow-reads: a write you make after reading something is ordered after that thing.
MongoDB offers causally consistent sessions, and many apps get session guarantees by pinning a user to one replica or reading from the leader after a write.
Choosing consistency per feature
One system can mix levels. In an online shop, the stock count during checkout must be strongly consistent, or you sell the same last item twice. The product page's stock badge (in stock, low stock) can be a few seconds stale. Reviews and ratings can be eventually consistent. The user's own cart needs read-your-writes but nothing stronger.
Ask two questions for each piece of data. What is the cost if a reader sees a value that is a few seconds old? And what is the cost if the feature is unavailable during a network problem? If stale data causes money loss, double-booking, or security holes (like a revoked permission still working), pay for strong consistency. If unavailability is worse than staleness, choose eventual consistency and design the UI to tolerate it.
- Strong: money, stock at checkout, unique usernames, locks, permissions
- Session guarantees: a user's own profile, cart, settings
- Eventual: counts, feeds, recommendations, analytics
Learn it in 3 short steps
About 1h each. Track them with a timer in your plan.
- 1Explain CAP and PACELC~1h
- 2Compare strong and eventual consistency~1h
- 3Calculate quorum reads and writes~1h
Key points
- Partitions are unavoidable, so the real CAP choice is C or A during one.
- PACELC: else, when healthy, choose latency or consistency.
- With N replicas, W + R > N guarantees reads overlap the latest write.
- Pick consistency per feature, not for the whole system.
Key terms
Tap a card to flip it. 0/5 known
Examples
N = 3 replicas
W=2, R=2 -> W+R=4 > 3 strong-ish reads, tolerates 1 failure
W=1, R=1 -> W+R=2 <= 3 fast, may read stale data
W=3, R=1 -> fast reads, writes fail if any node is down
Rule: W + R > N means read and write sets overlapTuning quorums trades latency for consistency.
Tuning consistency in Cassandra
A keyspace keeps 3 replicas in one data centre. Step 1: for order writes and the checkout read, use QUORUM, which waits for 2 of 3 replicas: W + R = 4 > 3, so reads overlap writes, and one replica may be down. Step 2: for an activity log that is read rarely, write at ONE for speed. Step 3: in a multi-region setup, LOCAL_QUORUM keeps the wait inside the local data centre, avoiding cross-region latency on every request. CONSISTENCY is a cqlsh command; drivers set the level per statement.
CREATE KEYSPACE shop WITH replication =
{'class': 'NetworkTopologyStrategy', 'dc1': 3};
CONSISTENCY QUORUM;
INSERT INTO shop.orders (id, status) VALUES (1001, 'PAID');
SELECT status FROM shop.orders WHERE id = 1001;
CONSISTENCY ONE;
INSERT INTO shop.activity (user_id, ts, event)
VALUES (42, toTimestamp(now()), 'viewed');Strong reads in DynamoDB only where needed
A booking service stores seat holds in DynamoDB. Step 1: the seat map shown while browsing uses default eventually consistent reads, which are cheaper and faster. Step 2: right before confirming a booking, the service reads the seat with a strongly consistent read so it sees the latest hold. Step 3: the final write uses a condition expression so it only succeeds if the seat is still free, which protects against races even between two strong reads.
aws dynamodb get-item --table-name Seats \
--key '{"seat_id": {"S": "F12"}}' \
--consistent-read
aws dynamodb put-item --table-name Seats \
--item '{"seat_id": {"S": "F12"}, "holder": {"S": "u42"}}' \
--condition-expression 'attribute_not_exists(holder)'Quick reference
- CAP: during a partition, a replica either refuses (CP) or may answer stale (AP)
- PACELC: else, when healthy, trade latency against consistency
- Quorum overlap rule: W + R > N; majority = floor(N/2) + 1
- N=3, W=2, R=2 tolerates one down replica for reads and writes
- DynamoDB strong reads cost 2x read capacity and are not available on GSIs
- Cassandra levels: ONE, QUORUM, LOCAL_QUORUM, ALL, chosen per query
- Causal consistency: replies never appear before the message they answer
- Pick consistency per feature by asking what a stale read would cost
Common mistakes
- Saying a system is CA; real networks partition.
- Applying strong consistency everywhere and paying latency for nothing.
- Assuming quorums alone give linearizability without careful conflict handling.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
For N = 3, list every (W, R) pair from 1 to 3 that satisfies W + R > N, and say which pair gives the fastest reads.
- Exercise 2medium
Classify each feature of a social app as needing strong, session, or eventual consistency and justify: unique username signup, own profile edits, follower count, blocking a user.
- Exercise 3hard
A Dynamo-style store with no blocking read repair uses N = 3 and reads and writes at quorum (2 of 3). Describe a sequence of events where a read still returns an old value even though W + R > N.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Is MongoDB CP or AP?
- It depends on settings. With majority write concern and majority or linearizable read concern it behaves like CP; reading from secondaries with weaker settings gives AP-style behaviour. Labels matter less than the settings you choose per operation.
- Can a system be CA?
- Only if network partitions never happen, which is true for a single machine but not for a distributed system. In practice, calling a distributed database CA means it was not designed for partitions.
- Does eventual consistency mean data can be lost?
- Not by itself. It means replicas may temporarily disagree. Data loss comes from other choices, such as last-write-wins conflict resolution or acknowledging writes before they are safely stored.