Skip to main content
  1. Posts/

What is the CAP theorem?

·3 mins

First, what is a theorem? #

This could sound obvious but it is better to understand the meaning, if you already know this you can safely skip this section.

A theorem is like a math fact proven to be true.

Imagine you’re doing a puzzle. You figure out that certain pieces always fit together in a specific way. Once you’ve tested it enough and know it’s always true, you can tell others, “Hey, these pieces will always fit like this!” That’s what a theorem is—it’s a proven rule or fact in math that people can rely on because it’s been checked and confirmed.

Ok, that was easy, let’s go into the details about the CAP theorem.

What means CAP? #

In the CAP theorem, “CAP” stands for Consistency, Availability, and Partition Tolerance. These are three properties that a distributed system can strive to achieve:

  1. Consistency: Every read returns the most recent write, ensuring that all nodes see the same data at the same time.

  2. Availability: The system is always available for read and write operations, even if some nodes in the network fail.

  3. Partition Tolerance: The system continues to function despite network partitions that may separate nodes in the distributed system, meaning the system can handle communication breakdowns between nodes.

As a note, (Eric Brewer)[https://www2.eecs.berkeley.edu/Faculty/Homepages/brewer.html] introduced this in 2000 and it has been used to understand the trade-offs when building distributed systems.

So, what’s the CAP theorem then? #

The CAP theorem states that in a distributed system, it is impossible to achieve all three of these properties simultaneously. You can only have two out of the three at any given time. For example, you can have a system that is consistent and partition-tolerant, but it may not always be available.

Why should I care about CAP theorem? #

The CAP theorem matters because it highlights the inherent trade-offs in designing distributed systems, particularly distributed databases. Understanding the CAP theorem helps engineers and architects make informed decisions about how to design systems that best meet the needs of their applications. Here’s why it’s important:

Guides Design Decisions: #

  • Consistency, Availability, or Partition Tolerance?: In a distributed system, you often need to prioritize two out of the three properties. The CAP theorem helps teams decide what trade-offs to make based on the specific requirements of their application. For example:
    • If consistency is critical (like in a banking system), you might sacrifice availability during network partitions.
    • If availability is more important (like in a social media app), you might tolerate eventual consistency during partitions.

Real-World Constraints: #

  • Network Partitions Are Inevitable: In distributed systems, network failures are a fact of life. The CAP theorem forces designers to acknowledge that perfect consistency and availability cannot both be guaranteed in the presence of network partitions. This understanding helps prevent over-engineering and sets realistic expectations.

Informs Trade-offs: #

  • Tailored Solutions: Different applications have different needs. The CAP theorem encourages designers to choose the most appropriate consistency and availability guarantees based on how the system will be used. For example, some applications can tolerate “eventual consistency,” where data becomes consistent over time, in exchange for higher availability.

System Resilience: #

  • Handling Failures Gracefully: By understanding the CAP theorem, developers can design systems that gracefully handle failures and maintain an acceptable level of service even when certain guarantees (like consistency or availability) must be temporarily relaxed.

Impact on Performance: #

  • Performance Optimization: The choices made regarding consistency, availability, and partition tolerance can significantly affect the performance of a distributed system. Understanding the CAP theorem helps in optimizing systems to meet performance goals under the expected operating conditions.

Conclusions #

The CAP theorem is crucial because it provides a framework for understanding the limitations of distributed systems and helps guide the design of systems that meet specific application needs while managing the inevitable trade-offs.