CAP theorem
CAP-теорема
When servers in a distributed system lose contact with one another, the system cannot both guarantee that every working server completes its requests and ensure that reads reflect writes that have already finished.
The problem CAP describes
A database can be reachable from an application even when its replicas cannot reach one another. That's a network partition: the servers are still running, but messages can't cross from one group to another. The CAP theorem says that during a partition, a distributed system cannot guarantee both linearizable reads and writes and CAP availability.
What the three letters mean
- Consistency (C) means linearizability. Operations appear to happen against a single copy of the data, in an order that respects real time. If a write finishes before a read starts, the read must see that write or a later one.
- Availability (A) means every request received by a working node eventually completes with a result for the requested operation. Returning a service-unavailable error just to end the request does not count. This is not an uptime percentage or a promise to respond within a particular number of milliseconds.
- Partition tolerance (P) means the system is designed to handle loss of communication between groups of nodes. It does not mean all operations must still be available on both sides. Here, partition refers to the network, not to a table partition or shard.
Why the guarantees collide
Suppose two regions store a copy of a product description. The link between them fails, and an editor saves a correction in the first region. After that write is confirmed, a shopper loads the page from the second region. The second replica can't tell whether a correction was saved or nothing changed. Returning the description it has keeps that read available, but the result is stale if the write went through. Waiting for the link to recover or rejecting the read protects consistency at the cost of availability. The system could also prevent the dilemma by refusing to confirm the write in the first place, but then the write is unavailable.
How to use CAP in a design
Start with what a wrong answer would cost. Serving an old description may be better than showing an error. Confirming a reservation for the last item from two disconnected regions can oversell it, so declining a reservation may be the safer choice. Engineers often call these priorities AP and CP, respectively. Apply those labels to the behavior of an operation under a particular configuration, not automatically to every operation a database supports. An AP design also needs a separate plan for reconciling replicas if it accepts conflicting writes.
What interviewers look for
Avoid the slogan “pick any two.” The meaningful question is what happens when the network splits. For example, if a three-replica cluster requires two replicas to acknowledge a write, the majority side may keep accepting writes while the isolated replica cannot. The service is still working for some clients, but the write guarantee no longer holds for every working node. If the network is healthy, CAP itself doesn't prevent the system from providing both consistency and availability.