Consistency in Distributed System - CAP、BASE
CAP Theorem
常见的解释是三者最多只能两者相互满足,
C-Consistency: All nodes see the same data at the same time.
A-Availability: Reads and writes always succeed
P-Partition tolerance: The system continues to operate despite arbitrary message loss or failure of part of the system
CA: Single site cluster, therefore all nodes are always in contact, when a partition occurs, the system blocks. Choose C and A with compromising of P (Partition Tolerance). e.g. type of applications: Banking and Finance application, a system which must have transaction
- e.g. connected to RDBMS.
AP: System is still available under partitioning, but some of the data returned may be inaccurate. Choose A and P with compromising of C (Consistency). When to choose AP to achieve what is itself a question. There is a use case: return the most recent version of the data you have, which could be stale. This system state will also accept writes that can be processed later when the partition is resolved. Sometimes architects choose eventual consistency with compromising Availability or Partition tolerance to some extent.
- Applications: shopping carts, any consumer-facing system, News publishing CMS, couchDB, cassandra.
- Typical protocol: Gossip
CP: Some data may not be accessible, but the rest is still consistent/accurate. choose based on the requirement analysis.
- Ecomerence/Pay System, MongoDB, zookeeper, Hbase etc.
分布式场景下其实是当P满足时候,A和C只能二选一, 因为满足CA其实就是单点, 比如当CA数据库节点用主从备份后,如果同时访问从节点,就失去一致性。
即当分布式内出现网络中断的时候,无法同时满足一致性和高可用。降低一点一致性要求,是可以达到,比如gossip协议,可以允许等网络恢复后,再把信息传播过去,提供eventual 一致性。
BASE - Achieve CAP basically
Basically Available、Soft State、Eventual Consistency. BASE targets to achieve CAP at the same time but not perfect in three aspects, like 80%A+80%C+80%P
- Basically-Available (基本可用): A distributed system should be available to respond with some acknowledgment — even if it’s a failure message, to any incoming request.
- Soft-state (软状态\柔性事务): The system may keep changing states as and when it receives new information. The eventual consistent system must have the soft state.
- Eventual Consistency(最终一致性): The components in the system may not reflect the same value/state of a record at a given point in time. They will settle it with time, eventually, though.