Skip to the content

Lesson 237 of 296

CAP, consistency models and eventual consistency

What you will learn

  • The CAP theorem, explained correctly
  • PACELC: latency vs consistency
  • Strong vs eventual consistency
  • Other consistency guarantees users notice
  • Quorums
  • How to talk about consistency in a design
On this page

The CAP theorem

In a distributed data system, when a network partition happens (some nodes cannot talk to others), you must choose between:

  • Consistency (C): every read gets the latest write (or an error).
  • Availability (A): every request gets a non-error response, even if it might not be the latest data.

You cannot have both during a partition. Partition tolerance (P) is not really optional for systems spread over a network, because partitions happen. So the real choice is CP or AP when a partition occurs:

ChoiceDuring a partitionExamples
CPRefuse some requests rather than return stale or conflicting dataA bank ledger, a booking system's seat allocation, ZooKeeper, etcd, a single-primary relational database (unreachable from the cut-off side)
APKeep answering, possibly with stale data, and reconcile laterShopping basket, social feed, likes counter, DNS, Cassandra and DynamoDB in their default modes

Common mistake: saying "pick two of three". You do not pick P away; you pick what happens when a partition occurs.

PACELC

CAP only talks about partitions. PACELC adds the normal case: if Partition, choose A or C; Else, choose Latency or Consistency. Even without failures, making every replica agree (strong consistency) adds latency. Many systems choose lower latency and weaker consistency most of the time.

Consistency models

ModelMeaningExample
Strong (linearisable)Once a write is confirmed, every later read sees itReading from the primary database
EventualIf writes stop, all replicas eventually agree; meanwhile reads may be staleDNS changes, read replicas, DynamoDB default reads, CDN content
Read-your-writesA user always sees their own writesAfter editing a profile, you see the change, even if others see it a little later
Monotonic readsA user never sees data go backwards in timeYou do not see a comment, refresh, and see it vanish
CausalIf event B depends on A, everyone sees A before BA reply never appears before the message it replies to

Users mainly notice the last three. Designs often provide eventual consistency in general plus read-your-writes for the user's own actions (Lesson 235).

Quorums

Replicated stores like Cassandra and DynamoDB-style systems use quorums: with N replicas, a write must be acknowledged by W replicas and a read must query R replicas.

  • If R + W > N, every read set overlaps every write set, so a read sees the latest write (with N = 3, W = 2, R = 2).
  • Lower W or R gives faster, more available operations with weaker consistency.

Choosing per feature

Consistency is a per-feature decision, not one setting for the whole system:

FeatureNeeds
Account balance, payment status, stock for the last item, seat bookingStrong consistency (or careful locking and constraints)
Like counts, view counts, follower countsEventual is fine
News feedEventual, with read-your-writes for your own posts
Search indexEventual (seconds behind the database)
Analytics dashboardsEventual (minutes or hours are fine)

How to say it in an interview

"CAP says that during a network partition a distributed store must choose between consistency, refusing or failing some requests, and availability, answering with possibly stale data; partition tolerance is not optional across a network. PACELC adds that even without partitions there is a latency versus consistency trade-off. I choose per feature: strong consistency for money, bookings and stock, eventual consistency for counters, feeds, search and analytics, and I add read-your-writes and monotonic reads so users do not see confusing behaviour. Quorum systems are consistent when R plus W is greater than N."

Key words

WordMeaning
Network partitionNodes unable to communicate with each other
CAP theoremDuring a partition, choose consistency or availability
PACELCOtherwise, choose latency or consistency
Strong consistencyReads always see the latest confirmed write
Eventual consistencyReplicas agree after a delay
Read-your-writesUsers always see their own changes
QuorumThe number of replicas that must respond

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.