What is cap theorem?

Time:2022-1-14

Cap theorem

Cap theorem means that in a distributed system, consistency, availability and partition tolerance cannot be taken into account, and at most two of them can be realized at the same time.

uniformity

Consistency means that in a distributed system, all data backups have the same value at the same time.

In other words, when the client accesses all nodes, the data at the same time should be completely consistent.

usability

Availability refers to whether the system can return the expected results within the normal response time when the client accesses data.

In other words, whether it is success or failure, there should be an expected response, not stuck there.

Partition tolerance

Partition tolerance means that when a node or network partition in the system fails, it will not affect the continuous operation of the system.

In other words, you can’t spoil a pot of soup because of a mouse shit.

Cap is not available at the same time

Fish is what I want, and bear’s paw is what I want. You can’t have both. It’s also the one who gives up fish and takes bear’s paw.

Cap cannot be implemented at the same time, so we must make a choice.
Either implement AP, CP or ca.

In distributed systems, partition fault tolerance is the most important. No one can guarantee that such a large system will not make mistakes, can it?
Therefore, generally speaking, P must be guaranteed, and CA will not be considered.

When to choose AP and CP?

Trade off of cap

Take an example of an e-commerce website. After placing an order, users need to create a new order in the order system, and then go to the inventory system to reduce inventory.

If the order system successfully creates new orders, but the inventory system fails to successfully reduce inventory, oversold will occur, so compensation is required.

  1. If CP is selected, i.e. consistency and partition fault tolerance are guaranteed, after the order is created, it will wait until the compensation operation in the inventory system is completed, i.e. after the inventory is reduced, and then the result of successful order creation will be returned
  2. If AP is selected, that is, availability and partition fault tolerance are guaranteed, the result of successful order creation will be returned directly after the order is created without waiting for the compensation operation in the inventory system to be completed

Therefore, the CP based architecture will give users a poor experience, because they have to wait until all consistency operations are completed before continuing other operations, but it is very safe. The AP based architecture will give users fast enough response to ensure the user experience, but consistency needs additional consideration.

CP is mainly used in banking, finance, securities and other systems that require high data consistency, such as circle after online payment;
AP is mainly used in Internet applications.

Reference reference

Beep beep beep:https://www.bilibili.com/vide…