No history yet

Consistency and CAP Trade-offs

The Unavoidable Trade-off

When you move from a single server to a distributed system, you gain resilience and scale. But you also inherit a fundamental problem: you can't have everything. This reality is captured by the , a cornerstone of distributed systems design first presented by in 2000.

The theorem states that a distributed data store can only provide two of the following three guarantees at the same time:

Consistency

noun

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

Availability

noun

Every request receives a (non-error) response, without the guarantee that it contains the most recent write.

Partition Tolerance

noun

The system continues to operate despite an arbitrary number of messages being dropped (or delayed) by the network between nodes.

In any real-world distributed system, network partitions are a fact of life. Routers fail, connections drop. Because you can't choose to not have network failures, Partition Tolerance (P) is a must. This means the real choice is always between Consistency (C) and Availability (A) when a partition occurs.

If a partition happens, a CP system will refuse to answer some requests to avoid returning inconsistent data. An AP system will always answer, but might return stale data.

Beyond Partitions

The CAP theorem is crucial, but it only describes what happens during a network failure. What about the 99.9% of the time when the system is operating normally? This is where the comes in. It provides a more complete picture of the trade-offs.

PACELC states that if there is a Partition, a system must choose between Availability and Consistency. Else (during normal operation), it must choose between Latency and Consistency.

This highlights a constant tension. To achieve stronger consistency, nodes must coordinate to make sure a write is replicated before acknowledging success. This coordination takes time, increasing latency. A system that prioritizes low latency might respond immediately after receiving a write on one node, sacrificing immediate consistency across the system.

System TypeDuring Partition (P)Else (Normal)Example Systems
PA/ELPrefers AvailabilityPrefers LatencyCassandra, DynamoDB
PC/ECPrefers ConsistencyPrefers ConsistencyHBase, MongoDB, Spanner
PA/ECPrefers AvailabilityPrefers Consistency--- (Less common)
PC/ELPrefers ConsistencyPrefers Latency--- (Less common)

Shades of Consistency

Consistency isn't a simple on/off switch. It's a spectrum. The two most important points on this spectrum are Strong and Eventual consistency.

Strong Consistency means that after a write completes, any subsequent read will see that new value. It's the most intuitive model, behaving like a single-machine database. Within this category, you'll find more formal models:

  • Linearizability: The strictest model. All operations appear to have executed atomically at some single point in time, preserving real-time order. If operation A completes before operation B begins, A's effects must be visible to B.
  • Sequential Consistency: A slightly weaker model. All nodes see all operations in the same order, but that order might not match the real-time order of events. It's as if all operations were fed into a single queue and executed one by one.

Eventual Consistency, common in AP systems, guarantees that if no new updates are made to a given data item, all replicas will eventually converge to the same value. The system is always available for reads and writes, but reads might return old data for a short period.

The tradeoff between Strong Consistency and Eventual Consistency lies in balancing immediate data accuracy with system performance and availability.

Choosing a model depends entirely on the use case. A banking application needs strong consistency for transactions. A social media feed can tolerate eventual consistency; seeing a new post a few seconds late isn't a critical failure. Understanding these trade-offs is the first step toward designing a distributed system that is not just scalable, but also correct for its purpose.

Time to test your knowledge on these core distributed systems principles.

Quiz Questions 1/6

According to the CAP theorem, a distributed system can provide at most two of which three guarantees simultaneously?

Quiz Questions 2/6

In a real-world distributed system, network partitions are considered unavoidable. Given this reality, the CAP theorem forces a direct trade-off between which two properties during a partition?

Making these trade-offs consciously is the mark of a skilled system designer. There is no single 'best' choice, only the right choice for a specific problem.