The introduction

In theoretical computer science, CAP theorem, also known as Brewer’s Theorem, states that it is impossible for a distributed computing system to satisfy three things at the same time:

  • Consistency (equivalent to all nodes accessing the same latest data copy)
  • Availability (An error-free response for each request — but no guarantee that the data retrieved is up to date)
  • In practical terms, partitioning is equivalent to a time-limit requirement for communication. If the system cannot achieve data consistency within the time limit, it means that partitioning has occurred and it must choose between C and A for the current operation.)

According to the theorem, a distributed system can satisfy only two of three terms but not all three. The easiest way to understand CAP theory is to imagine two nodes on either side of a partition. Allowing at least one node to update the state results in data inconsistency, i.e., loss of C property. If the nodes on one side of the partition are made unavailable to ensure data consistency, then the A property is lost. Unless the two nodes can communicate with each other, they can guarantee both C and A, which leads to the loss of P property.

Is this definition confusing to read, or does it make sense to read it a few times? CAP theory sounds very abstract. This paper tries to explain the meaning of CAP theory in simple language with examples in life.

CAP little story

This story feel about or fun, you click on the link zhuanlan.zhihu.com/p/265670196… It makes a lot more sense.

Cap on the balance of

According to CAP theory, we can not satisfy the three characteristics of consistency, availability and fault tolerance of partition at the same time, so what should we abandon?

Choose CA over P

This is almost impossible in distributed systems. Since partitioning is inevitable in a distributed environment, if we abandon P, we will abandon the distributed system, so there is no need to talk about CAP theory,

Choose CP over A

A distributed system availability if you can’t do it, often goes down or to stop providing services, so that the user experience is very bad, like once “micro au delete library event”, until all the data are back will continue to provide services, this period how long downtime, how much loss caused businesses. Common CP distributed systems such as Distributed database (Redis) and Zookeeper give priority to ensuring strong consistency of data to abandon system availability.

Give up AP give up C

If you want to ensure high availability and allow partitioning, you need to give up consistency. Once a network problem occurs, nodes may lose contact with each other. To ensure high availability, users need to be immediately returned when they access the node. In this case, each node can only provide services with local data, which results in global data inconsistency. Nowadays, most scenarios will choose usability at the expense of consistency (to keep consistency), just like when we grab the red envelope at Spring Festival, it won’t tell you how much money you grabbed immediately, but it will tell you how long to check again. And when we were scrambling for tickets during the Spring Festival, it was obvious that this high-speed train was still a stamp, but when you filled in the verification code and submitted the order with the passenger information, you were told that there was no ticket. When you returned to the list page to check the number of tickets, the ticket was still displayed. The user experience is slightly unfriendly, but acceptable.

summary

There is no better choice for CAP, only according to their own business scenarios to choose the best choice for themselves.

The Base theory of

BASE: Full name: Basically Available, Soft state, and Eventually consistent, coined by architects at ebay. Base theory is the result of tradeoff between consistency and availability in CAP. It is derived from the summary of distributed practice of large-scale Internet and gradually evolved based on CAP theorem. Its core idea is:

Although Strong consistency cannot be achieved, each application can adopt an appropriate method to achieve Eventual consistency according to its own service characteristics.

Basically Available

What is basic availability? Sacrifice performance (service response time), experience (partial functional experience) to ensure basic availability. Sacrifice performance: for example, when we query goods, the normal response time is about 1s, but if it is basically available, the return result is 10s. Sacrifice experience: For example, on Double 11, Taobao will only ensure that the core functions are available (order, payment, etc.), and other non-core functions (return, modify address, etc.) will be degraded. For the degradation, you can see the previous article “The degradation of the three powerful tools of high concurrency system”.

Soft State

Allowing intermediate states that do not affect overall availability allows the system to have data delays in copies of data on multiple different nodes.

Eventual Consistency

It says soft state, and then you can’t always be soft state, you have to have a period of time. After the expiration of the period, data consistency should be guaranteed for all copies. To achieve the final consistency of data. This time frame depends on network latency, system load, data replication scheme design, and so on.

The system can guarantee that the data will eventually reach a consistent state without any new update operations, so that all client data access to the system will eventually get the latest value.

The end of the

  • As a result of their talent and learning, it is inevitable that there will be mistakes, if you found the wrong place, but also hope that the message to me pointed out, I will correct it.
  • If you think the article is good, your forwarding, sharing, appreciation, like, message is the biggest encouragement to me.
  • Thank you for reading. Welcome and thank you for your attention.

Picking apples on the shoulders of giants:Zh.wikipedia.org/wiki/CAP%E5… www.hollischuang.com/archives/66… www.cnblogs.com/stateis0/p/…