Cap theorem

Time:2021-9-25

Reprinted from Ruan YifengMeaning of cap theorem

Distributed system is becoming more and more important. Almost all large websites are distributed.

The biggest difficulty of distributed system is how to synchronize the states of each node. Cap theorem is not only the basic theorem in this field, but also the starting point for understanding distributed systems.

This paper introduces the theorem. It’s actually easy to understand and obvious. The following content mainly refers to Michael Whittaker’sarticle

1、 Three indicators of distributed system

Cap theorem

In 1998, Eric brewer, a computer scientist at the University of California, proposed that distributed systems have three indicators.

  • Consistency
  • Availability
  • Partition tolerance

Their first letters are C, a and P.

Eric Brewer said that these three indicators cannot be achieved at the same time. This conclusion is called the cap theorem.

2、 Partition tolerance

Let’s first look at partition tolerance, which is called “partition tolerance” in Chinese.

Most distributed systems are distributed in multiple subnetworks. Each subnet is called a partition. Partition fault tolerance means that interval communication may fail. For example, one server is located in China and the other server is located in the United States. These are the two zones. They may not be able to communicate with each other.

Cap theorem

In the figure above, G1 and G2 are two cross region servers. G1 sends a message to G2, and G2 may not receive it. This situation must be taken into account when designing the system.

Generally speaking, partition fault tolerance cannot be avoided, so it can be considered that the P of cap is always true. The cap theorem tells us that the remaining C and a cannot be done at the same time.

3、 Consistency

Consistency is called “consistency” in Chinese. This means that the read operation after the write operation must return this value. For example, if a record is V0, the user initiates a write operation to G1 and changes it to V1.

Cap theorem

Next, the user’s read operation will get v1. This is called consistency.

Cap theorem

The problem is that the user may initiate a read operation to G2. Since the value of G2 has not changed, V0 is returned. The results of G1 and G2 read operations are inconsistent, which does not meet the consistency.

Cap theorem

In order to change G2 to V1, it is necessary to ask G1 to send a message to G2 during G1 write operation, requiring G2 to also change to V1.

Cap theorem

In this way, the user can also get V1 by initiating a read operation to G2.

Cap theorem

4、 Availability

Availability is called “availability” in Chinese, which means that as long as you receive a user’s request, the server must respond.

The user can choose to initiate a read operation to G1 or G2. No matter which server receives the request, it must tell the user whether it is V0 or V1, otherwise it will not meet the availability.

5、 Contradiction between consistency and availability

Consistency and availability, why can’t they be established at the same time? The answer is simple because communication may fail (i.e. partition fault tolerance).

If the consistency of G2 is guaranteed, G1 must lock the read and write operations of G2 during write operations. Only after data synchronization can read and write be reopened. During locking, G2 cannot read or write, and there is no availability.

If the availability of G2 is guaranteed, G2 cannot be locked, so the consistency is not tenable.

To sum up, G2 cannot achieve consistency and availability at the same time. Only one target can be selected during system design. If consistency is pursued, the availability of all nodes cannot be guaranteed; If you pursue the availability of all nodes, you can’t achieve consistency.


Where is availability above consistency?

For example, if you publish a web page to CDN, multiple servers have copies of this web page. Later, an error was found and the web page needs to be updated. At this time, each server can only be updated once.

Generally speaking, the update of web pages does not place special emphasis on consistency. In a short period of time, some users get the old version and others get the new version. The problem will not be particularly serious. Of course, everyone will eventually see the new version. Therefore, this occasion is that usability is higher than consistency.