The CAP theorem
In a distributed data store, when a network partition splits the nodes, you must choose between consistency (every read sees the latest write, or gets an error) and availability (every request gets a non-error response, possibly stale). You can't have both during the partition.
Partitions happen in any real network, so "pick two of three" is misleading: the real choice is CP or AP when a partition happens. When the network is healthy you can have both.
| Choice | During a partition | Examples | Good for |
|---|---|---|---|
| CP | Refuse some requests to stay correct | ZooKeeper, etcd, HBase, a single SQL primary | Payments, inventory, locks, leader election |
| AP | Answer everything; reconcile later | Cassandra, DynamoDB (default), DNS | Feeds, likes, carts, presence |
PACELC
PACELC extends CAP: if there's a Partition, choose A or C; Else (normal operation) choose Latency or Consistency. Even without failures, waiting for replicas to agree costs latency. Cassandra and DynamoDB are PA/EL by default; most SQL setups with synchronous replicas are PC/EC.
Consistency models
| Model | Guarantee |
|---|---|
| Linearizable (strong) | Behaves like a single copy; once a write is acknowledged, every later read sees it |
| Sequential | All nodes see operations in the same order, though not necessarily real-time order |
| Causal | Operations that depend on each other are seen in order (a reply never appears before its question) |
| Read-your-writes | A user always sees their own updates |
| Monotonic reads | A user never sees data go backwards in time |
| Eventual | If writes stop, all replicas converge eventually |
Choose per feature, not per system. A shop can keep its catalogue eventually consistent while checkout and stock reservation are strongly consistent.
Interview questions
Try answering out loud first, then open the model answer.
Q1Explain the CAP theorem.intermediate
If a network partition separates the replicas of a distributed store, it can either stay consistent (refuse requests it can't serve correctly) or stay available (answer with possibly stale data), not both. Since partitions can't be ruled out, systems decide in advance whether they lean CP or AP.
Q2Is a single-node database CA?intermediate
In CAP terms, a single node has no partition between replicas, so it's trivially consistent and available while it's up, but it's not fault-tolerant: if it's down, it's unavailable. CA only describes systems that don't have to tolerate partitions, which in practice means one machine.
Q3Give an example where eventual consistency is fine and one where it isn't.beginner
Fine: a like count or a follower list being a few seconds behind. Not fine: two people buying the last concert ticket, or withdrawing from an account; those need strong consistency (a transaction or a lock) so the invariant holds.
Q4How do eventually consistent systems resolve conflicts?advanced
Last-write-wins using timestamps (simple, but can silently drop writes), version vectors to detect concurrent updates and let the app merge them, or CRDTs: data types like counters and sets designed so concurrent updates always merge to the same result.
Quick check
0 of 3 answered