đź§ What is the CAP Theorem?
The CAP Theorem, proposed by Eric Brewer, states that a distributed system can only guarantee two out of the following three properties at the same time:
| Letter | Stands for | Means… |
|---|---|---|
| C | Consistency | Every read receives the most recent write or an error |
| A | Availability | Every request receives a non-error response, even if it’s not the latest |
| P | Partition Tolerance | The system continues to operate even if network failures (partitions) occur |
🧪 Let’s Break Each One Down
âś… 1. Consistency
• All nodes see the same data at the same time.
• If you write something, and then read it immediately, you get the updated value.
Like in a relational database with ACID transactions.
âś… 2. Availability
• The system always responds to requests, even if it’s not 100% up-to-date.
• Reads/writes will not fail (though they might return older data).
Think of it like: “The show must go on”, even during problems.
âś… 3. Partition Tolerance
• The system can keep running even if network links between nodes fail (i.e., communication between parts of the system is broken).
This is non-negotiable in real distributed systems — partitions will happen.
⚠️ The Trade-off
In the presence of a network partition (P), a system can choose either Consistency (C) or Availability (A) — not both.
So you get one of the following:
| Type | Guarantees | Compromises | Example DBs |
|---|---|---|---|
| CP | Consistency + Partition Tolerance | Might reject requests | HBase, MongoDB (strong mode), PostgresSQL, Spanner |
| AP | Availability + Partition Tolerance | Might return stale data | CouchDB, Cassandra, DynamoDB |
| CA (not possible if partitioned) | Consistency + Availability (in perfect networks only) | Not partition-tolerant | Traditional single-node RDBMS |
📌 Real-World Examples
| Scenario | What’s sacrificed? | Why? |
|---|---|---|
| A global chat app always shows old messages during network lag | ❌ Consistency | Prioritizes availability |
| A bank refuses your transaction if it can’t talk to the central server | ❌ Availability | Prioritizes consistency |
| A relational DB on one server (no partition) | ❌ Partition Tolerance | Works well locally, not in distributed systems |
📝 Summary
In a network partition, a distributed system must choose between: • Consistency (C): Return accurate, up-to-date data — even if it means denying service • Availability (A): Always respond — even if the data is stale or inconsistent And it must tolerate partitions (P) because networks are inherently unreliable.