Why CAP can not have both

Why in the distributed environment CAP can not have both? This question can be done on the board, so that the key to the problem is: Suppose there is a network partition, if you have P, that is, to tolerate the existence of network partition, then C and A can have both.

Can be divided into two cases to carry out further deduction.

Case 1: If there is no copy of the data in this distributed system, then the system must meet the strong consistency conditions, because only the unique data, there will be no data inconsistencies. At this point C and P elements are available, but if the system has a network partition status or machine downtime, will inevitably lead to some data is not accessible, then the availability conditions can not be met, that is, in this case obtained the CP system, But the CAP can not be met at the same time.

Case 2: If there is a copy of the data in the system (see Figure 2-2), assume that the variable x has two copies and is stored on different machines, the original data remains the same, the value is v1. At time = t1, a value update operation for x occurs on machine 1, which assigns the value of x to v2. Time to time to time = t2 time, machine 1 x has been given a new value v2, if there is no network partition at this time, the system can be x new value v2 synchronization to the machine 2, to achieve data consistency requirements. But if the network partition at this time led to the two machines can not communicate, then the new value of x can not be synchronized to the machine 2, this time we have to do a trade-off between C or A and choose. If you want the system to be highly available (select A), then the query request for x on read machine 2 must return a value within a limited time, and the return value is not the latest value v2, so there is a problem of data inconsistency C). If you choose strong consistency (choose C), the x read request for machine 2 must be rejected before the two machines resume communication and synchronize the data to a consistent state, and the availability of the system can not be guaranteed (discard A). So no matter which one to choose, at the expense of another factor as a price, that is either AP, or CP, but there is no perfect CAP.

评论