Why consistency is a choice, and when it matters
When data lives on several machines, copies can disagree. A write reaches one machine first. A network cable fails, and machines cannot talk to each other. Now a question has two bad answers: refuse to answer until the machines agree, or answer from what you have and risk being wrong. The CAP theorem says that during such a split you must choose.
The two sides give you things interviewers always ask about:
- Consistency: every read returns the latest write, as if there were one copy.
- Availability: every request to a working node gets an answer, even if the network is broken.
The third letter, partition tolerance, means the system keeps working when the network splits. In a distributed system you cannot choose to avoid partitions, so the real choice is CP (consistent, may refuse) or AP (available, may be stale).
Choose strong consistency (CP) when:
- Being wrong is worse than being briefly unavailable: money, stock at checkout, seat bookings, leader election, locks.
Choose availability and eventual consistency (AP) when:
- Being down is worse than being slightly wrong: likes, view counts, feeds, shopping carts, DNS, product browsing.
You may not need to choose at all when:
- Your data fits on one node (or one primary): there is no partition between copies to worry about. Many products never need more.
- You can partition the problem: use strong consistency for the few things that need it, and eventual for the rest.
Try it
Choose what the system favours when the network splits, and whether to cut node C off. Step through ten ticks of writes and reads.
Consistency or availability during a network split
Step 1 of 11 · Press Space to play, arrow keys to stepx = ?
x = ?
x = ?
No operation yet.
Start: three copies of one value, x. This system favours consistency (CP): if a node cannot reach a majority of the nodes, it refuses to answer rather than risk a wrong answer.
- writes rejected:
- 0
- reads rejected:
- 0
- stale reads:
- 0
- writes overwritten:
- 0
1 tick = one step of the simulation. A write or read goes through one node. A majority is 2 of 3 nodes. 'Stale' means the answer differs from the latest acknowledged write.
Things to try:
- Untick Cut node C off and run both behaviours. With no partition, both are always correct: no rejections, no stale reads. CAP only bites when the network splits.
- Tick the partition with Consistency (CP): the write via C and the read via C are rejected, but no stale answer is ever returned. The system is unavailable to those clients, and correct.
- Switch to Availability (AP): nothing is rejected, but 2 reads return stale answers.
- Watch the heal step in AP: the copies disagree, "last write wins" picks x = 3, and the earlier acknowledged write x = 2 is overwritten and lost. Always answering has a price.
Consistency choices
| Choice | What you get | Good when | Weakness |
|---|---|---|---|
| Strong consistency (linearizable) | Every read sees the latest write, as if one copy existed | Money, stock, locks, anything where a wrong answer is costly | Slower, and refuses requests it cannot confirm during a partition |
| Eventual consistency | Copies agree after some time if writes stop | Feeds, counters, carts, caches, high availability across regions | Stale reads; concurrent writes need a conflict rule |
| Read-your-writes (session consistency) | A user always sees their own updates | Profile edits, posting a comment | Others may still see old data for a while |
| Causal consistency | If B depends on A, everyone sees A before B | Comment threads, replies | More bookkeeping; weaker than strong |
| Tunable (quorum) consistency | You choose, per request, how many copies must agree | Systems that mix needs, such as Cassandra and DynamoDB | You must pick the numbers well, and mistakes cause stale reads |
CP vs AP
- CP (consistency over availability): during a partition, nodes that cannot reach a majority refuse requests. Reads are never stale, but part of the system is unavailable. Examples of use: coordination services, ledgers, bookings.
- AP (availability over consistency): during a partition, every node keeps answering from its own copy. The system stays up, but answers can be stale and conflicting writes must be merged afterwards. Examples of use: shopping carts, social counters, DNS.
Real systems are not purely one or the other. Many let you choose per operation, and a system can be CP for one table and AP for another.
How quorums work
A quorum is a minimum number of copies that must agree. With N copies, a write waits for W of them to confirm, and a read asks R of them and takes the newest answer.
- N = 3 copies.
- A write succeeds when W = 2 copies have it.
- A read asks R = 2 copies.
- Because R + W > N (2 + 2 > 3), any two copies you read must include at least one that has the latest write. So the read sees the newest value.
Changing the numbers changes the trade-off:
- W = N, R = 1: writes are slow (all copies must be up), reads are fast.
- W = 1, R = N: writes are fast, reads are slow.
- R + W at most N: reads can be stale, in return for speed and availability.
In the demo, a majority of 2 out of 3 is the quorum: the side with two nodes can keep working, and the lone node cannot.
A related idea, PACELC, adds that even with no partition there is a trade-off between latency and consistency: waiting for more copies is safer but slower.
Strong vs eventual consistency vs the alternatives
| Concern | Strong consistency | Eventual consistency | Single node |
|---|---|---|---|
| Reads see the latest write | Always | Not always | Always |
| Works during a partition | Only on the majority side | Yes, everywhere | Not applicable |
| Write and read latency | Higher (must coordinate) | Lower | Lowest |
| Conflicting writes | Prevented | Must be merged | None |
| Survives a node failure | Yes, with a majority | Yes | No |
| Best for | Money, stock, locks | Feeds, counters, carts | Small systems |
Use strong consistency where a wrong answer costs money or trust, and eventual consistency where speed and uptime matter more than being instantly current. Do not pay for strong consistency on data that does not need it.
Avoiding a new single point of failure
Strong consistency needs agreement, and agreement has its own weak spots:
- The majority. If you lose more than half the nodes, a CP system stops accepting writes. Use an odd number of nodes and spread them over failure zones.
- The leader. Many CP systems elect a leader. A slow or flapping leader means pauses. Make sure elections are quick and well tested.
- Clocks. "Last write wins" needs a clock, and clocks drift. Prefer version numbers or merge rules for important data.
- Conflict handling. In an AP system, merging is part of the design, not an afterthought.
The trade-off you accept: with CP, some requests fail during a split. With AP, some answers are stale or some writes are merged away.
Common mistakes
- "Pick any two of three." You cannot avoid partitions, so you choose between C and A only when one happens. Say that.
- Using "eventual" as an excuse. Eventual consistency still needs a plan for conflicts and for users who just made a change.
- Last write wins by default. It silently loses acknowledged writes and depends on clocks. Use version numbers, merge rules or data types designed for merging.
- Strong consistency everywhere. It costs latency and availability. Use it only for data that needs it.
- Mixing up the two Cs. The C in CAP (every read sees the latest write) is not the C in ACID (a transaction keeps data rules true).
- Reading your own write from a stale copy. A user changes something and sees the old value. Route their next read to a fresh copy.
How to talk about CAP and consistency in an interview
- Say what CAP really means: "during a partition you choose between consistency and availability", not "pick two".
- Say which data needs which, with reasons: "payments are CP, the like counter is AP."
- Name the mechanism: quorums (R + W > N), leader and majority, or last write wins with its risk.
- Mention unprompted: what users see during a partition, how conflicts are merged, and read-your-writes for the person who just wrote.
- Name the trade-off in one sentence: wrong-but-up against right-but-down.