Resourcesโ€บSystem Designโ€บCAP Theorem Explained
๐Ÿ—๏ธSystem Designโ€” CAP Theorem Explainedโฑ 7 min

CAP Theorem Explained

What CAP actually means, why the tradeoffs matter, and how real systems choose between consistency and availability.

๐Ÿ“…January 18, 2025โœTechTwitter.iodistributedfundamentalsdatabases

The Three Properties

CAP stands for:

  • Consistency โ€” every read returns the most recent write (or an error)
  • Availability โ€” every request receives a response (never an error), even if it's stale
  • Partition Tolerance โ€” the system continues operating even if network partitions split nodes

Brewer's theorem: you can guarantee at most two of the three simultaneously.


Why Partition Tolerance Is Not Optional

In any distributed system, network partitions will happen. Nodes go offline, switches fail, packets get dropped. You cannot opt out of P.

This means the real choice is: CP or AP when a partition occurs.


CP Systems

Choose consistency over availability. During a partition, refuse requests that might return stale data.

Examples: HBase, Zookeeper, etcd, MongoDB (with majority write concern)

Use when: financial transactions, inventory counts, any place where returning wrong data is worse than returning an error.


AP Systems

Choose availability over consistency. During a partition, serve potentially stale data rather than returning errors.

Examples: Cassandra, DynamoDB, CouchDB, DNS

Use when: social media feeds, product catalogs, caches โ€” places where a slightly stale response is fine.


PACELC โ€” The Full Picture

CAP only describes behaviour during a partition. PACELC extends it to the normal case:

If there is a Partition, choose between Availability and Consistency. Else (when running normally), choose between Latency and Consistency.

Most "CP" systems still trade consistency for latency in the normal path (e.g., reading from a replica).


Practical Takeaways

ScenarioChoose
Bank balance, order stockCP โ€” never show wrong number
User profile, social feedAP โ€” stale is fine
Config values, leader electionCP โ€” correctness critical
Shopping cart, recommendationsAP โ€” availability wins
Real-time chat deliveryDepends on requirements

Common Misconception

CAP doesn't say you pick two and ignore the third permanently. It says during a network partition you must sacrifice one. Healthy systems can aim for all three โ€” the tradeoff only activates under failure.