This article explains the fundamental CAP Theorem, which posits that a distributed system can only guarantee two out of Consistency, Availability, and Partition Tolerance at any given time. It clarifies that Partition Tolerance is unavoidable in distributed systems, forcing the real design choice between Consistency (CP) and Availability (AP) during network partitions. Using MySQL master-slave replication as an example, the article demonstrates how replication lag illustrates the AP tradeoff, where availability is prioritized over strong consistency.
Read original on Dev.to #systemdesignThe CAP Theorem is a foundational concept in distributed systems, stating that it's impossible for a distributed data store to simultaneously provide more than two out of three guarantees: Consistency, Availability, and Partition Tolerance. This theorem highlights the inherent tradeoffs engineers face when designing distributed architectures.
The Real Choice: CP vs AP
Network partitions are a reality in any distributed system, making Partition Tolerance (P) an unavoidable requirement. This means the practical design decision is always between Consistency (C) and Availability (A) when a partition occurs.
When a network partition happens, a system must choose: either sacrifice consistency to remain available (AP system) or sacrifice availability to maintain strong consistency (CP system). There is no third option in the face of a partition.
The article uses an example of MySQL master-slave replication to demonstrate an AP (Availability + Partition Tolerance) system. In this setup, the master handles all writes, which are then asynchronously replicated to one or more slaves. Slaves are available to serve reads even if they haven't yet received the latest updates from the master. This leads to replication lag, where a read on a slave might return stale data immediately after a write on the master.
SHOW SLAVE STATUS; -- Observe Seconds_Behind_Master to see replication lagThis lag is the direct tradeoff for prioritizing availability over strong consistency. If strong consistency (CP) were required, synchronous replication would be used, where the master waits for slaves to acknowledge writes before confirming to the client, increasing write latency but ensuring consistency.
While CAP theorem simplifies the choice, in reality, consistency exists on a spectrum beyond simple "consistent" or "inconsistent." Key consistency models include:
Architectural Implications
Choosing the right consistency model is a critical system design decision, driven by application requirements and tolerance for data staleness versus uptime.