CAP Theorem

alt text (Image generated through ChatGPT)

 

Eric Brewer in the year 2000, while he was a researcher at UC Berkley, first described the trade offs involved in designing and building distributed systems. This concept was named after his own name and called Brewer's CAP Theorem. Today it is popularly called as CAP Theorem.

CAP Theorem states that, in a distributed system, it is impossible to provide guarantee on all three - Consistency (C), Availability (A), and Partition tolerance (P). Hence the name CAP Theorem from Consistency, Availability and Partition tolerance.

CAP Theorem does not provide a formal proof, but it is rather a theoretical construct highlighting the difficulties in designing distributed systems that delivers on all three aspects - Consistency, Availability and Partition tolerance.

Lets first look at the three aspects of a distributed systems that this theorem deal with - Consistency, Availability and Partition tolerance.

alt text

Consistency:

In a distributed system with multiple nodes, all the nodes should have same data at all time. What this means is that whenever a change is affected in data belonging to one node then that cahnge should immediately reflected or replicated on all the nodes. This is so that immediately after any write if a client were to make read request to any of the nodes for that same data then the client should see the latest change.

alt text

Consistency deals with:

  • Correctness of data
  • Reliability of data
  • Integrity of data

There are many different types of consistency like:

  • Strong Consistency
  • Eventual Consistency
  • Casual Consistency
  • Read your Writes Consistency
  • Monotonic Consistency
  • Monotonic Reads and Writes
  • Week Consistency

In the context of CAP theorem the focus would be to achieve guarantee of strong consistency.

Availability:

Availability refers to the state of the application or system during which it is responsive, ready, operational and usable at any given time. High Availability means system is operational and usable without significant disruptions and downtime. It is measured as a percentage and expressed in terms of uptime vs downtime.

A Highly available system will need to build redundancies, health check, load distribution mechanism, automatic recovery and disaster recovery features.

alt text

Availability deals with:

  • User Experience
  • Business Continuity
  • Disaster Recovery

Availability is achieved through:

  • Redundancy
  • Load Balancing
  • Health Checks
  • Failover Mechanism
  • Disaster Recovery

In the context of the CAP Theorem the focus would be to achieve high availability.

Partition Tolerance:

In a distributed systems there are always chances of network failures causing nodes in clusters being unable to talk to each other or maintain data in sync. Partition tolerance in distributed systems implies that the system continues to function reliably even when there are network failures.

A partition tolerant system tries to get back to a consistent state eventually, resulting in an eventual consistent system.

alt text

Partition tolerance deals with:

  • Handling inconsistent data
  • Handling conflicting operations
  • Handling duplicate operations

Partition tolerance is handled through:

  • Quorum based system
  • Leader Election
  • Fencing Tokens
  • CRDTs

In the context of CAP Theorem the focus is on achieving Partition tolerance in a distributed system.

So now coming back to CAP Theorem, which says it is not possible to build a distributed system that guarantees - Strong Consistency, High Availability and Partition Tolerance.

What this means is that we can possibly optimize for two aspects of CAP at any given time and end up compromising on the third aspect. This provides us with the following three types of systems - CA, CP and AP optimized systems.

alt text

CA System:

This type of system strives to meet the guarantees of Consistency and Availability but not partition tolerance.

Examples of these would be Relational Databases like MySQL, PostgreSQL in a non-distributed setup ensuring consistency and available as long as no network partition occur. These would be setup as single node or clustered with synchronous replication.

CP System:

In this type of systems consistency and partition tolerance is maintained but become unavailable on network partition. Basically, it will optimize for consistency over availability.

Examples of these would be Hbase, Redis and MongoDB that will ensure consistency but sacrifice availability in case of partition events.

AP System:

In this type of systems availability is maintained even during partition event but consistency is sacrificed. This means that in the event of a network partition, it will respond to the requests but it will not be the most recent state or consistent state.

Examples of these would be Cassandra and DynamoDB that are designed to provide high availability and partition tolerance. They will return stale data in the event of partition and will become eventually consistent.

CAP Theorem helps us evaluate what type of system we need. Which two aspects of distributed system we want to optimize on from the three - Consistency, Availability and Partition tolerance.



Try few questions