Tradeoffs

CAP Theorem Quick Reference

What is CAP?

C - Consistency

Every read returns the most recent write

All nodes see the same data at the same time

A - Availability

Every request gets a response

System never refuses requests (may return stale data)

P - Partition Tolerance

System works despite network failures

Required in distributed systems - not optional!

⚡ The Key Insight

In a distributed system, network partitions WILL happen. So you must choose:

CP (Consistency + Partition)

Refuse requests during partition to maintain consistency

→ Some requests fail, but data is always correct

AP (Availability + Partition)

Always respond, even if data might be stale

→ All requests succeed, but data may be inconsistent

Real-World Database Examples

DatabaseTypeBehavior During Partition
PostgreSQLCPPrimary blocks writes until partition heals
MongoDBCPMinority partition becomes read-only
Redis ClusterCPRequires majority of masters available
CassandraAPAll nodes accept writes, resolve conflicts later
DynamoDBAPEventually consistent by default
CouchDBAPMulti-master with conflict resolution

When to Choose What

Choose CP When:

  • 💰 Financial transactions - Can't have inconsistent balances
  • 📦 Inventory systems - Overselling is very costly
  • 🎫 Booking systems - Double-booking ruins UX
  • 🔐 Auth/permissions - Security must be consistent

Choose AP When:

  • 👍 Social media likes - Slight delay is fine
  • 📊 Analytics/metrics - Approximate is OK
  • 🛒 Shopping cart - Better available than down
  • 💬 Chat messages - Eventual delivery is acceptable

🎯 Interview-Ready Explanation

"CAP theorem states that in a distributed system, when a network partition occurs, you must choose between consistency and availability. There's no avoiding this trade-off."

"For [this system], I'd choose [CP/AP] because [specific reason]. If we chose [other option], we'd risk [specific consequence]."

⚠️ Common Misconception

CAP is not "pick 2 of 3". It's really: "When partition happens (it will), pick C or A". When there's no partition, you can have both C and A!