PrepZone Logo
PrepZone

CAP Theorem and PACELC

Why you cannot have perfect consistency, availability and partition tolerance at once.

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.

Consistencyevery read = latest write
Availabilityevery request gets a response
Partition tolerancenetwork splits happen

CP systems: bank ledgers. AP systems: social feeds, DNS.

During a network partition, pick consistency or availability — not both.

In practice, partitions happen. The real choice is CP (refuse writes/reads until consistent) vs AP (serve possibly stale data).

CP vs AP examples

AspectCP (Consistency + Partition tolerance)AP (Availability + Partition tolerance)
Behaviour during partitionReject requests or blockServe from local replica
ExamplesZooKeeper, etcd, traditional RDBMS primaryCassandra, DynamoDB (configurable)
Good forLeader election, inventory locksSocial feeds, analytics, session store
RiskDowntime during partitionStale reads, merge conflicts
  • Behaviour during partition

    CP (Consistency + Partition tolerance)Reject requests or block
    AP (Availability + Partition tolerance)Serve from local replica
  • Examples

    CP (Consistency + Partition tolerance)ZooKeeper, etcd, traditional RDBMS primary
    AP (Availability + Partition tolerance)Cassandra, DynamoDB (configurable)
  • Good for

    CP (Consistency + Partition tolerance)Leader election, inventory locks
    AP (Availability + Partition tolerance)Social feeds, analytics, session store
  • Risk

    CP (Consistency + Partition tolerance)Downtime during partition
    AP (Availability + Partition tolerance)Stale reads, merge conflicts

PACELC extension

If Partition → choose A or C (same as CAP). Else → choose Latency or Consistency.

SystemPACELC profileMeaning
DynamoDBPA/ELAvailable during partition; fast reads may be stale
Postgres (single primary)PC/ECConsistent; higher latency on cross-region reads
CassandraPA/EL (tunable)Quorum reads trade latency vs staleness
Redis (single primary)PC/ECStrong per shard; not HA without failover
  • DynamoDB

    PACELC profilePA/EL
    MeaningAvailable during partition; fast reads may be stale
  • Postgres (single primary)

    PACELC profilePC/EC
    MeaningConsistent; higher latency on cross-region reads
  • Cassandra

    PACELC profilePA/EL (tunable)
    MeaningQuorum reads trade latency vs staleness
  • Redis (single primary)

    PACELC profilePC/EC
    MeaningStrong 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.
Java
-- 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:

FeatureConsistency needTypical choice
Bank transferStrong — no double spendSingle-leader SQL + transactions
Video view counterEventual — approximate OKAsync increment + periodic flush
User profile after editRead-your-writesPrimary read after write; replica for others
Global config flagStrongConsensus store (etcd) or primary DB
  • Bank transfer

    Consistency needStrong — no double spend
    Typical choiceSingle-leader SQL + transactions
  • Video view counter

    Consistency needEventual — approximate OK
    Typical choiceAsync increment + periodic flush
  • User profile after edit

    Consistency needRead-your-writes
    Typical choicePrimary read after write; replica for others
  • Global config flag

    Consistency needStrong
    Typical choiceConsensus store (etcd) or primary DB

Handling partitions in design

Java
# 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.