CAP theorem basics
Consistency — every read returns the latest write or an error. Availability — every request receives a non-error response (not necessarily the latest data). Partition tolerance — the system continues despite network splits between nodes.
CP systems: bank ledgers. AP systems: social feeds, DNS.
In practice, partitions happen. The real choice is CP (refuse writes/reads until consistent) vs AP (serve possibly stale data).
CP vs AP examples
| Aspect | CP (Consistency + Partition tolerance) | AP (Availability + Partition tolerance) |
|---|---|---|
| Behaviour during partition | Reject requests or block | Serve from local replica |
| Examples | ZooKeeper, etcd, traditional RDBMS primary | Cassandra, DynamoDB (configurable) |
| Good for | Leader election, inventory locks | Social feeds, analytics, session store |
| Risk | Downtime during partition | Stale reads, merge conflicts |
Behaviour during partition
CP (Consistency + Partition tolerance)Reject requests or blockAP (Availability + Partition tolerance)Serve from local replicaExamples
CP (Consistency + Partition tolerance)ZooKeeper, etcd, traditional RDBMS primaryAP (Availability + Partition tolerance)Cassandra, DynamoDB (configurable)Good for
CP (Consistency + Partition tolerance)Leader election, inventory locksAP (Availability + Partition tolerance)Social feeds, analytics, session storeRisk
CP (Consistency + Partition tolerance)Downtime during partitionAP (Availability + Partition tolerance)Stale reads, merge conflicts
PACELC extension
If Partition → choose A or C (same as CAP). Else → choose Latency or Consistency.
| System | PACELC profile | Meaning |
|---|---|---|
| DynamoDB | PA/EL | Available during partition; fast reads may be stale |
| Postgres (single primary) | PC/EC | Consistent; higher latency on cross-region reads |
| Cassandra | PA/EL (tunable) | Quorum reads trade latency vs staleness |
| Redis (single primary) | PC/EC | Strong per shard; not HA without failover |
DynamoDB
PACELC profilePA/ELMeaningAvailable during partition; fast reads may be stalePostgres (single primary)
PACELC profilePC/ECMeaningConsistent; higher latency on cross-region readsCassandra
PACELC profilePA/EL (tunable)MeaningQuorum reads trade latency vs stalenessRedis (single primary)
PACELC profilePC/ECMeaningStrong per shard; not HA without failover
PACELC reminds you that consistency vs latency matters even on a healthy network — cross-region replication lag is not a partition, but reads are still stale.
Consistency models in practice
Consistency spectrum
- Strong — read always sees latest write (single-leader DB, linearisable reads).
- Eventual — replicas converge given no new writes; window of staleness.
- Read-your-writes — user sees their own updates immediately; others may lag.
- Causal — preserves ordering of causally related operations.
-- Strong consistency: read from primary
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 'a1';
COMMIT;
SELECT balance FROM accounts WHERE id = 'a1'; -- same connection → latest
-- Eventual: read from replica (may lag milliseconds to seconds)
SET default_transaction_read_only = on;
-- connection to replica.endpoint
SELECT view_count FROM videos WHERE id = 'v_9182';
Choosing for your product
Map consistency to business rules:
| Feature | Consistency need | Typical choice |
|---|---|---|
| Bank transfer | Strong — no double spend | Single-leader SQL + transactions |
| Video view counter | Eventual — approximate OK | Async increment + periodic flush |
| User profile after edit | Read-your-writes | Primary read after write; replica for others |
| Global config flag | Strong | Consensus store (etcd) or primary DB |
Bank transfer
Consistency needStrong — no double spendTypical choiceSingle-leader SQL + transactionsVideo view counter
Consistency needEventual — approximate OKTypical choiceAsync increment + periodic flushUser profile after edit
Consistency needRead-your-writesTypical choicePrimary read after write; replica for othersGlobal config flag
Consistency needStrongTypical choiceConsensus store (etcd) or primary DB
Handling partitions in design
# StreamHub feed service — AP-leaning
during_partition:
strategy: serve_stale_from_local_cache
max_staleness_sec: 30
user_message: optional "feed may be updating"
# StreamHub payment adjacency — CP-leaning
during_partition:
strategy: fail_closed_on_wallet_ops
fallback: queue intent, reconcile when healed
Document what users experience when the system cannot meet its ideal guarantees.
Common misconceptions
CAP pitfalls
- CAP is not "pick two of three forever" — P is assumed; you choose C or A during partition.
- SQL is not always CP — read replicas make it AP for reads.
- NoSQL is not always AP — MongoDB and others offer tunable write concerns.
- "Eventually consistent" does not mean "random" — convergence is guaranteed.
Quick recall
Everything you need if you only revisit this box.
- During a partition, choose consistency (CP) or availability (AP) — not both.
- PACELC adds latency vs consistency trade-off when the network is healthy.
- Strong consistency for money and inventory; eventual for feeds and counters.
- Read-your-writes is a practical middle ground for user-facing edits.
- SQL with replicas is AP for reads, CP for writes to the primary.
- State user-visible behaviour during partitions — fail closed or serve stale.
Test yourself
Answer these before moving on — recall is what makes it stick.