CAP Theorem


The CAP theorem, also known as Brewer’s theorem, states that it is impossible for a distributed data store to simultaneously provide more than two out of the following three guarantees:

In simpler terms, if there’s a network partition, you have to choose between consistency and availability. You can’t have both.

Let’s illustrate with a simplified example of two database servers, Server A and Server B, replicating data.

Scenario 1: Network Partition and Prioritizing Consistency

Imagine a network issue separates Server A and Server B. A client writes new data to Server A. If we prioritize consistency, Server A won’t acknowledge the write until it can confirm with Server B (which it can’t reach). This leads to an error or timeout for the client, upholding consistency (no stale data read) but sacrificing availability (client request failed).

// Conceptual JavaScript example (not real implementation)

// Server A receives write request
function writeData(data) {
  if (networkPartitioned) {
    return "Error: Unable to confirm write due to network partition"; // Prioritizing consistency
  } else {
    // ... replicate to Server B ...
    return "Write successful"; 
  }
}

Scenario 2: Network Partition and Prioritizing Availability

In the same scenario, if we prioritize availability, Server A would acknowledge the write immediately, even though it can’t communicate with Server B. This preserves availability (client request succeeds) but sacrifices consistency (Server B has stale data).

// Conceptual JavaScript example (not real implementation)

// Server A receives write request
function writeData(data) {
  if (networkPartitioned) {
    return "Write successful"; // Prioritizing availability (potential inconsistency)
  } else {
    // ... replicate to Server B ...
    return "Write successful"; 
  }
}

Choosing the Right Balance:

The CAP theorem forces you to make a trade-off. The best choice depends on your specific application needs:

Remember, you cannot choose CA (Consistency and Availability) in a distributed system subject to network partitions. The CAP theorem ensures that in the presence of a partition, you must choose either consistency or availability.