Published on

Tradeoffs In CAP Theorem

Authors
  • avatar
    Name
    Skim
    Twitter

Imagine you're managing a vast online marketplace, with customers placing orders and vendors updating product listings. In a distributed system, you need to ensure three critical attributes:

  1. Consistency: In case a shopper checks if a product is in stock. consistency ensures that regardless of which server they query, the information they receive is always the same. In other words, once a product's stock is updated, that change is reflected in all queries across all servers.
  2. Availability: this guarantees that every customer request receives a response, even if some servers are experiencing issues. In our marketplace, this means that shoppers can continue to browse and buy products even if a few servers are temporarily (or indefinitely) failing.
  3. Partition Tolerance: network glitches and communication failures are inevitable. Partition tolerance ensures that the system can handle such hiccups without collapsing entirely. In our example, if there's a network disruption between servers, the marketplace can still function and serve customers.

The Trade-off: Choosing Two out of Three

Now comes the captivating aspect of the CAP theorem: you can't have all three attributes in their full glory simultaneously. It's like a delicate balancing act where you need to choose two attributes while potentially sacrificing the third:

Consistency and Availability (CA)

In some scenarios, like critical financial transactions, maintaining data consistency and ensuring uninterrupted availability are top priorities. This means that during network partitions or failures, the system might reduce availability to uphold data correctness.

Consistency and Partition Tolerance (CP)

Applications where data integrity is paramount might favor this option. It ensures that even in the face of network disruptions, data remains consistent. However, availability might be compromised.

Availability and Partition Tolerance (AP)

For systems that need to stay operational and responsive even during network partitions, prioritizing availability and partition tolerance could lead to eventual consistency. This means that data might be temporarily inconsistent across nodes but will converge over time.

Real-world Applications

CA: Amazon Web Services (AWS) Relational Database Service (RDS)

AWS RDS provides managed database solutions, including MySQL, PostgreSQL, and others. It emphasizes data consistency and availability. When you use RDS, you expect that the data you read from the database is always the most recent version, ensuring data integrity. Additionally, AWS RDS strives to maintain high availability by automatically handling server failovers and backups to minimize downtime and keep your applications running smoothly. However, in scenarios where there's a network partition or a server goes down, RDS might prioritize data consistency over immediate availability to ensure accurate data transactions.

CP: Google Cloud Spanner

Google Cloud Spanner is a globally distributed, horizontally scalable database that provides strong consistency and partition tolerance. It's designed to ensure data consistency across the globe while handling network partitions. Spanner achieves this by synchronizing data across multiple data centers using a technology called TrueTime, which ensures global consistency and accurate timestamps. While it sacrifices some degree of immediate availability during network partitions, it ensures data correctness and integrity, making it suitable for applications that require precise data synchronization, like financial systems.

AP: Apache Cassandra

Apache Cassandra is a NoSQL database designed for high availability and partition tolerance. It prioritizes availability and the ability to handle network partitions. Cassandra achieves this by providing eventual consistency, meaning that data changes will propagate to all nodes eventually, even in the presence of network disruptions. This approach is well-suited for applications that require constant availability and can tolerate temporary data inconsistencies. For instance, Cassandra is commonly used for IoT data storage, time series data, and content delivery networks.