Understanding the CAP Theorem

The CAP theorem is a fundamental principle in distributed systems that every database architect should understand.

What is the CAP Theorem?

The CAP theorem states that a distributed database system can only guarantee two out of the following three properties simultaneously:

C - Consistency

Every read receives the most recent write or an error. All nodes see the same data at the same time.

A - Availability

Every request receives a response (without guarantee that it contains the most recent write). The system remains operational 100% of the time.

P - Partition Tolerance

The system continues to operate despite network partitions. The system works even when messages are dropped between nodes.

The Trade-offs

CA (Consistency + Availability)

Traditional relational databases like MySQL, PostgreSQL. Sacrifice partition tolerance.

Note: In distributed systems, network partitions are inevitable, so true CA systems don't exist in practice.

CP (Consistency + Partition Tolerance)

Systems like MongoDB, HBase, Redis. Sacrifice availability during partitions to maintain consistency.

Use case: Banking systems, inventory management

AP (Availability + Partition Tolerance)

Systems like Cassandra, DynamoDB, CouchDB. Sacrifice consistency for availability during partitions.

Use case: Social media feeds, caching, shopping carts

Real-World Examples

Banking (CP)

A bank cannot show inconsistent account balances. It's better to be unavailable temporarily than to show wrong data.

Social Media Feed (AP)

It's acceptable if different users see slightly different feeds. Availability is more important than perfect consistency.

E-commerce Cart (AP)

Users can continue shopping even if the system is partially down. Temporary inconsistency is acceptable.

Choosing the Right Trade-off

When designing your system, ask yourself:

  • Can my system tolerate showing stale data?
  • Is it acceptable to be temporarily unavailable?
  • What happens if network partitions occur?
  • What are the business consequences of inconsistency?

Key Takeaway

The CAP theorem isn't about choosing one option forever. Modern databases often allow you to tune these trade-offs based on your specific use case.