cap theorem
system design

CAP Theorem and System Design

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

CAP Theorem in System Design

The CAP Theorem is a fundamental concept in distributed systems, introduced by Eric Brewer. It states that in any distributed data system, you can only guarantee two out of three properties at a time:

  • Consistency (C)
  • Availability (A)
  • Partition Tolerance (P)

Three Properties of CAP Theorem

  1. Consistency (C)
    Every read receives the most recent write, ensuring all nodes in the system return the same data.
    • Example: When you update data in one node, every other node reflects the same updated value immediately.
  2. Availability (A)
    Every request receives a response (success/failure), but it may not be the most up-to-date value.
    • Example: The system remains operational, and requests do not fail, but some data might be stale.
  3. Partition Tolerance (P)
    The system continues to operate even if there is a network partition (loss of communication between nodes).
    • Example: Nodes can communicate independently when network failures occur.

Why You Can Only Pick Two?

In a distributed system, if a network partition occurs (P), you must choose between:

  • Consistency (C): Stop serving requests until the partition resolves to ensure all nodes are synchronized.
  • Availability (A): Continue serving requests with potentially outdated (stale) data.

Thus, you cannot guarantee all three properties simultaneously.


CAP Combinations

  1. CP (Consistency + Partition Tolerance)
    • Guarantees consistency even during network partitions, but availability may be sacrificed.
    • Example Systems:
      • HBase
      • MongoDB (configured with strong consistency)
      • Google Bigtable
    • Use Case: Banking systems, where consistent data is critical.
  2. AP (Availability + Partition Tolerance)
    • Guarantees availability, but consistency may be relaxed (eventual consistency).
    • Example Systems:
      • Cassandra
      • DynamoDB
      • Riak
    • Use Case: Social media feeds or real-time services where downtime is unacceptable.
  3. CA (Consistency + Availability)
    • Works only when there are no partitions (single-node systems or centralized databases).
    • Example: Traditional RDBMS like PostgreSQL and MySQL.
    • Use Case: Systems operating on a single machine with no distributed components.

CAP Theorem in Real Systems

Real-world systems often aim for Partition Tolerance (since network failures are inevitable) and make trade-offs between Consistency and Availability.

  • Eventual Consistency: Systems like DynamoDB, S3, and Cassandra allow for slightly stale data but eventually converge to a consistent state.
  • Strong Consistency: Systems like HBase or Zookeeper ensure reads always return the latest data.

How CAP Impacts System Design?

  1. Understand System Requirements:
    • Does your system require strict consistency (e.g., bank transactions)?
    • Is high availability more critical (e.g., e-commerce checkout during failures)?
  2. Choose the Right Trade-offs:
    • CP for critical data systems.
    • AP for real-time, highly available systems with eventual consistency.
  3. Design for Failures:
    • Use techniques like replication, sharding, and leader election to maintain availability.

  1. Explain CAP Theorem and its implications in system design.
  2. What is the difference between CP and AP systems? Provide examples.
  3. How would you design a system for consistency vs. availability?
  4. Why is Partition Tolerance a must in distributed systems?

Examples in System Design Scenarios

  1. Design a Distributed Cache:
    • Example: Redis in a distributed setup can prioritize AP with eventual consistency.
  2. Design a Global File Storage System:
    • Example: Amazon S3 is AP with eventual consistency.
  3. Design a Leader Election System:
    • Example: Zookeeper prioritizes CP for ensuring leader consistency.

In interviews, when discussing CAP Theorem, be clear about:

  1. The system trade-offs you are making.
  2. Real-world examples of databases/systems.
  3. How your system ensures reliability and fault tolerance.

Understanding the CAP Theorem will give you a solid foundation for solving distributed system design problems! 🚀


Course illustration
Course illustration

All Rights Reserved.