CAP theorem and how to choose CP or AP

CAP theorem

CAP theorem , also known as Brewer's theorem . For architects designing distributed systems (not just distributed transactions), CAP is your starting point.

Distributed systems are becoming more and more important, and large websites are almost always distributed.

The biggest difficulty in distributed systems is how to synchronize the states of each node. The CAP theorem is the fundamental theorem in this regard and a starting point for understanding distributed systems.

C ( Consistency ) :

Refers to the characteristic that the data can be kept consistent among multiple copies (strict consistency). Whether all data backups in a distributed system have the same value at the same time. (all nodes have the same data at the same time)

Consistency refers to data consistency in Replications problems. It can be divided into strong consistency and weak consistency.

strong consistency

In short, at any time, the data in all nodes is the same

For example, for relational databases, it is required that updated data can be seen by subsequent accesses, which is strong consistency.

weak consistency

After the data is updated, if it can tolerate that subsequent accesses can only access part or all of them , it is weak consistency. Eventual consistency is weak consistency.

A (Availability)

It means that the services provided by the system must always be available, and the server must give a response whenever a user's request is received. Return a reasonable response within a reasonable time (not an error and timeout response), only non-faulty nodes can meet the normal business; only within a reasonable time, the user can accept; only when a reasonable response is returned, the user can accept.

P (Partition tolerance)

In the case of inability to communicate between network nodes, the nodes are isolated, resulting in network partitions, and the entire system can still provide external services that meet consistency and availability unless the entire network environment fails.

What is a partition?

In a distributed system, different nodes are distributed in different sub-networks. Due to some special reasons, there is a state of network disconnection between these sub-nodes, but their internal sub-networks are normal. As a result, the environment of the entire system is divided into several isolated areas. This is the partition.

in conclusion

The essence of the CAP principle is either AP, CP, or AC, but there is no CAP.

Practical development options

Pursuit of CP: Users request payment, wait for payment order generation and inventory system data update, and then return results to users to pursue strong consistency Disadvantages: poor user experience

Pursuit of AP: The user requests payment, immediately responds to the user with the result, asynchronously processes the inventory system, abandons strong consistency, and adopts eventual consistency

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324032293&siteId=291194637