<< back to Guides

βš–οΈ Guide: CAP Theorem β€” A Systems Design Deep Dive

CAP Theorem describes the fundamental trade-offs in distributed systems. It states that a system can guarantee at most two out of three properties:

This guide explains what each term means, how real systems handle the trade-offs, and how to make architecture decisions based on CAP constraints.


🧠 1. What Is the CAP Theorem?

Formulated by Eric Brewer and proven in 2002, CAP says:

In the presence of a network partition, you must choose between consistency and availability.

Meaning:
You cannot simultaneously guarantee all three of:


πŸ” 2. Definitions

| Property         | Description                                                         |
|------------------|---------------------------------------------------------------------|
| **Consistency (C)** | Every read gets the most recent write or an error                 |
| **Availability (A)**| Every request receives a response (may be stale)                  |
| **Partition Tolerance (P)** | System continues to operate despite communication failure |

πŸ”Ί 3. CAP Triangle

You can only pick 2 out of 3:

         Consistency
          /       \
         /         \
    CP  /           \  CA
       /             \
      /               \
Availability ------- Partition Tolerance
               AP

βš™οΈ 4. Real-World Interpretations

Scenario C A P Example Systems
Relational DB (no partitions) βœ… βœ… ❌ MySQL on one node, SQLite
MongoDB (eventual consistency) ❌ βœ… βœ… AP by default
HBase / Spanner (quorum) βœ… ❌ βœ… CP
Cassandra / DynamoDB ❌ βœ… βœ… Tunable AP
Zookeeper / Etcd βœ… ❌ βœ… CP (election-based)

πŸ§ͺ 5. Network Partitions in Practice

A partition is a network failure between parts of the system β€” one server can't talk to another, but they're both still running.


πŸ”„ 6. CP vs AP: What’s the Difference?

βœ… CP (Consistency + Partition Tolerance)

// Example: quorum-based write in CP
write(data) {
  sendToReplicas(data)
  wait for majority ack
  return success
}

βœ… Predictable behavior
❌ Less tolerant to latency or downtime


βœ… AP (Availability + Partition Tolerance)

// Example: AP behavior
write(data) {
  storeLocally(data)
  sync with peers later
}

βœ… Highly available
❌ May serve stale or conflicting data


🧱 7. Mitigating CAP Limitations


πŸ”§ 8. Tools and How They Handle CAP

Tool / DB CAP Preference Notes
Cassandra AP (Tunable) Can configure consistency level per request
DynamoDB AP Eventually consistent by default, strongly consistent option available
Spanner CP Global consistency using TrueTime API
Etcd / Zookeeper CP Used for coordination, must avoid split-brain
MongoDB AP Defaults to eventual, can be made strongly consistent
Redis Sentinel AP Asynchronous replication, brief inconsistency on failover

❌ 9. Common Misconceptions

Myth Reality
"You can’t ever get all 3" You can get all 3 in absence of partitions
"Availability = uptime" CAP availability = always returns a response
"CAP explains everything" Other models (e.g. PACELC, BASE) offer better nuance

🧠 10. Related Concepts


βœ… Summary

Property Description
Consistency All nodes return same, latest data
Availability Always respond, even if stale
Partition Tol. Works during network failures
Trade-off Pick 2 out of 3 when network partitions

πŸ“š Further Reading


<< back to Guides