# CAP定理

CAP定理是分布式计算中的一项不可能性结论，指出一个联网的共享数据系统无法同时保证一致性、[可用性](https://www.edgepedia.org/zh/availability)和分区容忍。该定理由埃里克·布鲁尔（Eric Brewer）在2000年的一次主题演讲中提出猜想，并于2002年由麻省理工学院的塞思·吉尔伯特（Seth Gilbert）和南希·林奇（Nancy Lynch）在《ACM SIGACT新闻》上给出证明。<sup>[1](https://www.cs.princeton.edu/courses/archive/spring21/cos418/papers/cap.pdf)</sup><sup> • </sup><sup>[2](https://www.cs.utexas.edu/~dsb/cs386d/Projects14/CAPConsistencyPN.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 陈述 | 一个分布式系统最多只能提供一致性、可用性和分区容忍三者中的两个。<sup>[2](https://www.cs.utexas.edu/~dsb/cs386d/Projects14/CAPConsistencyPN.pdf)</sup> |
| 起源 | 由埃里克·布鲁尔于2000年提出猜想；由塞思·吉尔伯特和南希·林奇证明，《ACM SIGACT新闻》33(2):51–59，2002年。<sup>[1](https://www.cs.princeton.edu/courses/archive/spring21/cos418/papers/cap.pdf)</sup> |
| 一致性的含义 | 线性一致性（原子一致性）：操作表现得如同在单个节点上逐一执行，而非ACID中的C。<sup>[3](https://www.cs.umd.edu/~abadi/papers/abadi-pacelc.pdf)</sup> |
| 实践解读 | 分区容忍并非可选，因此真正的选择是在分区期间取一致性还是取可用性。<sup>[4](https://prism.nicofortuin.com/systems/cap-partition/)</sup> |
| PACELC扩展 | 若发生分区（Partition），在可用性与一致性之间取舍；否则（Else），在正常运行中，在延迟与一致性之间取舍（丹尼尔·J. 阿巴迪，2012年）。<sup>[3](https://www.cs.umd.edu/~abadi/papers/abadi-pacelc.pdf)</sup> |
| 法定人数规则 | 若有N个副本、R个同步读节点和W个同步写节点，当R + W > N时一致性得以保持。<sup>[3](https://www.cs.umd.edu/~abadi/papers/abadi-pacelc.pdf)</sup> |
| 批评 | 马丁·克莱普曼（Martin Kleppmann，2015年）认为CAP的定义过于狭窄，许多系统既非线性一致，也非CAP意义上的可用。<sup>[5](https://arxiv.org/abs/1509.05393)</sup> |

## 三个属性

正式陈述中的**一致性**是原子一致性，等价于线性一致性：所有操作之间必须存在一个全序，使得每个操作看起来都在某个单一瞬间完成，就如同所有请求都在单个节点上执行一样。<sup>[3](https://www.cs.umd.edu/~abadi/papers/abadi-pacelc.pdf)</sup>这比ACID中的C更强，后者涉及的是数据库约束。<sup>[6](https://scaledojo.dev/papers/brewers-conjecture-and-the-feasibility-of-consistent-available-partition)</sup>

**可用性**意味着非故障节点收到的每个请求都必须产生响应；按此定义，返回错误或超时的系统不算可用。<sup>[7](https://theorempath.com/topics/cap-theorem)</sup><sup> • </sup><sup>[8](https://docs.aws.amazon.com/whitepapers/latest/availability-and-beyond-improving-resilience/cap-theorem.html)</sup>

**分区容忍**意味着即便节点之间丢失任意数量的消息，系统仍能继续运行。<sup>[8](https://docs.aws.amazon.com/whitepapers/latest/availability-and-beyond-improving-resilience/cap-theorem.html)</sup>

## 定理究竟说了什么

流行的概括“三选二”具有误导性。分区是一种网络事件，例如交换机故障、链路饱和或可用区不可达，无论设计者在设计评审中是否选择了分区容忍，它都会发生。正确的陈述更为狭窄：在分区期间，系统必须在一致性与可用性之间做出选择。系统可以拒绝少数派分区上的写入以保持一致性，也可以允许两个分区都接受写入而任其分叉——不存在中间地带，而且这一决定是网络强加的，而非工程师做出的。<sup>[4](https://prism.nicofortuin.com/systems/cap-partition/)</sup>

吉尔伯特与林奇的证明结构很小：两个节点、一次写入、一次读取、一条被丢弃的消息。如果写入在分区的一侧完成，而读取随后在另一侧开始，可用性要求该读取必须返回，但一致性要求它能看到那次写入，而分区恰恰阻止了这一点。该结论在异步网络模型中成立，该模型中消息传递时间没有上界。<sup>[6](https://scaledojo.dev/papers/brewers-conjecture-and-the-feasibility-of-consistent-available-partition)</sup><sup> • </sup><sup>[9](https://pld.cs.luc.edu/database/gilbert_lynch_brewer_proof.pdf)</sup>用理论术语来说，一致性是一种安全性属性（每个响应都正确），可用性是一种活性属性（每个请求最终都收到响应），而该定理是在不可靠分布式系统中无法同时保证二者的一个实例。<sup>[10](https://groups.csail.mit.edu/tds/papers/Gilbert/Brewer2.pdf)</sup>

## CP、AP与逐操作行为

系统通常被标记为CP（一致且分区容忍，牺牲可用性）或AP（可用且分区容忍，牺牲一致性）。CP系统在分区期间拒绝写入，而不是提供可能过时的数据；AP系统持续响应，但数据可能不一致，这需要一种放宽的一致性模型，如最终一致性。<sup>[11](https://mortalapps.com/system-design/distributed-coordination/cap-theorem-in-practice/)</sup><sup> • </sup><sup>[8](https://docs.aws.amazon.com/whitepapers/latest/availability-and-beyond-improving-resilience/cap-theorem.html)</sup>

这些标签描述的是分区期间的行为，而非整个架构。一个数据库可能对账户余额读取采用强一致性，而对商品描述返回最终一致性的结果，因此成熟的设计会明确哪项保证适用于哪些数据。部分分区——即一个节点能到达半个集群，或一条链路丢弃一部分数据包——也无法被这两种标签干净地刻画。<sup>[4](https://prism.nicofortuin.com/systems/cap-partition/)</sup>时任[剑桥大学](https://www.edgepedia.org/zh/university-of-cambridge)分布式系统研究员的[马丁·克莱普曼](https://www.edgepedia.org/zh/martin-kleppmann)在2015年的一篇批评中指出，单领导者复制数据库——大多数关系数据库的标准配置——不是CAP可用的，因为与领导者分区隔离的客户端无法写入，而其异步跟随者读取也不是线性一致的，因此在严格定义下它既不是C也不是A。<sup>[5](https://arxiv.org/abs/1509.05393)</sup><sup> • </sup><sup>[12](https://martin.kleppmann.com/2015/05/11/please-stop-calling-databases-cp-or-ap.html?mode=reply)</sup>

## PACELC：延迟权衡

CAP只约束故障期间的行为；它不限制系统在正常运行中能做什么。<sup>[3](https://www.cs.umd.edu/~abadi/papers/abadi-pacelc.pdf)</sup>当时在耶鲁大学任职的丹尼尔·J. 阿巴迪在2012年《计算机》杂志的一篇文章中提出PACELC，以刻画始终适用的权衡：若发生分区（Partition），在可用性与一致性之间权衡；否则（Else），在正常运行时，在延迟与一致性之间权衡。<sup>[3](https://www.cs.umd.edu/~abadi/papers/abadi-pacelc.pdf)</sup>“否则”分支支配着日常运行，因为分区是罕见的，而协调每次写入的延迟代价是永久的。<sup>[4](https://prism.nicofortuin.com/systems/cap-partition/)</sup>

一旦数据被复制，延迟权衡便随之而来。系统可以同时向所有副本发送更新，或先发给主节点，或先发给任意节点，每种选择都在一致性与延迟之间做取舍。从任意副本提供读取可降低延迟，但在更新传播期间可能返回不一致的值；将所有读取路由到主节点可保持一致性，但增加延迟并失去负载均衡的选项。<sup>[3](https://www.cs.umd.edu/~abadi/papers/abadi-pacelc.pdf)</sup>在法定人数系统中，要求R + W > N以更高的延迟保持一致性，而R + W ≤ N则允许不一致的读取。<sup>[3](https://www.cs.umd.edu/~abadi/papers/abadi-pacelc.pdf)</sup>一项关于[Cassandra](https://www.edgepedia.org/zh/apache-cassandra)的研究发现，法定人数读取——即检查多个副本是否一致——相对于弱读取，延迟代价可达四倍甚至更多。<sup>[3](https://www.cs.umd.edu/~abadi/papers/abadi-pacelc.pdf)</sup>

PACELC将系统分为PA/EL、PC/EC、PA/EC或PC/EL。Dynamo、Cassandra和Riak的默认版本是PA/EL：它们在分区期间放弃一致性以换取可用性，在正常运行中放弃一致性以换取更低延迟，并提供用户可调的设置来移动权衡点。PNUTS是雅虎的托管数据服务平台，默认是PC：它在正常运行中为降低延迟而降低一致性，但当其主节点落入少数派分区时，会使数据无法更新。<sup>[3](https://www.cs.umd.edu/~abadi/papers/abadi-pacelc.pdf)</sup>

## 批评与后续视角

布鲁尔本人于2012年在《CAP十二年后： “规则”如何改变》一文中重新审视了该定理，认为在三者之间取舍的说法过于简化：真实的分区罕见且短暂，通过显式处理分区，设计者可以优化一致性与可用性，从而实现三者某种程度的权衡。<sup>[13](https://apice.unibo.it/bin/view/Publication/Cap12Computer45)</sup>在2015年的一篇批评中，克莱普曼进一步推进了这一论点：由于CAP对一致性和可用性的定义过于严格，无法为实际设计选择提供指导，该定理常被引用来支持它并不真正蕴含的决策；他转而提出了一个延迟敏感性框架，考察操作的延迟如何随网络延迟变化。<sup>[5](https://arxiv.org/abs/1509.05393)</sup>

谷歌的Spanner说明了形式定理与实践之间的差距。Spanner是一个高度可用的全球数据库，为所有事务提供强一致性，这种组合在CAP下常被认为是不可能的。布鲁尔2017年的分析得出结论：Spanner在技术上是CP，因为在某些分区期间它选择一致性而放弃可用性，但实际上是CA：多区域Cloud Spanner的目标是五个九的可用性<sup>5</sup>，而网络在Spanner本就罕见的中断中贡献不到10%，这得益于它运行在谷歌的私有网络上。<sup>[14](https://cloud.google.com/blog/products/databases/inside-cloud-spanner-and-the-cap-theorem)</sup><sup> • </sup><sup>[15](https://research.google/pubs/spanner-truetime-and-the-cap-theorem/)</sup>

## 在真实系统中的应用

亚马逊的Dynamo在2007年的一篇SOSP论文中描述，是典型的AP设计：它在某些故障场景下牺牲一致性以提供“始终在线”的体验，使用对象版本控制和应用程序辅助的冲突解决来处理分叉的副本。<sup>[16](https://web.stanford.edu/class/cs244/papers/amazon-dynamo-sosp2007.pdf)</sup>[CockroachDB](https://www.edgepedia.org/zh/cockroachdb)则采取相反立场，选择一致性，是一个CP系统：每份数据至少存在于三个副本上，写入需要多数副本通信，在发生分区时系统会阻塞而非返回不一致的数据，而基于租约的读取能从故障中快速恢复。<sup>[17](https://www.cockroachlabs.com/blog/limits-of-the-cap-theorem/)</sup>

该定理的实际用途是框定决策，而非决定决策。吉尔伯特与林奇自己的回顾指出，大型系统常被分割为子系统，每个子系统沿数据、运维、功能、用户或层级维度选择不同的权衡，从而使整体能良好响应大多数请求，同时在需要的地方提供强一致性。<sup>[10](https://groups.csail.mit.edu/tds/papers/Gilbert/Brewer2.pdf)</sup>

## References

1. [Seth Gilbert and Nancy Lynch (2002). Brewer's Conjecture and the Feasibility of Consistent, Available, Partition-Tolerant Web Services. Princeton COS 418 course copy.](https://www.cs.princeton.edu/courses/archive/spring21/cos418/papers/cap.pdf)
2. [Introducing the CAP Theorem, University of Texas at Austin course notes.](https://www.cs.utexas.edu/~dsb/cs386d/Projects14/CAPConsistencyPN.pdf)
3. [Daniel J. Abadi (2012). Consistency Tradeoffs in Modern Distributed Database System Design. IEEE Computer.](https://www.cs.umd.edu/~abadi/papers/abadi-pacelc.pdf)
4. [CAP in practice, Prism.](https://prism.nicofortuin.com/systems/cap-partition/)
5. [Martin Kleppmann (2015). A Critique of the CAP Theorem. arXiv:1509.05393.](https://arxiv.org/abs/1509.05393)
6. [Brewer's Conjecture and the Feasibility of Consistent, Available, Partition-Tolerant Web Services, ScaleDojo paper summary.](https://scaledojo.dev/papers/brewers-conjecture-and-the-feasibility-of-consistent-available-partition)
7. [CAP Theorem: Consistency, Availability, TheoremPath.](https://theorempath.com/topics/cap-theorem)
8. [CAP theorem, AWS whitepaper: Availability and Beyond.](https://docs.aws.amazon.com/whitepapers/latest/availability-and-beyond-improving-resilience/cap-theorem.html)
9. [Gilbert and Lynch proof copy, Loyola University Chicago.](https://pld.cs.luc.edu/database/gilbert_lynch_brewer_proof.pdf)
10. [Seth Gilbert and Nancy Lynch (2012). Perspectives on the CAP Theorem. MIT CSAIL.](https://groups.csail.mit.edu/tds/papers/Gilbert/Brewer2.pdf)
11. [CAP Theorem in Practice, MortalApps.](https://mortalapps.com/system-design/distributed-coordination/cap-theorem-in-practice/)
12. [Martin Kleppmann (2015). Please stop calling databases CP or AP.](https://martin.kleppmann.com/2015/05/11/please-stop-calling-databases-cp-or-ap.html?mode=reply)
13. [Eric Brewer (2012). CAP twelve years later: How the "rules" have changed. Computer 45(2), APICe record.](https://apice.unibo.it/bin/view/Publication/Cap12Computer45)
14. [Eric Brewer (2017). Inside Cloud Spanner and the CAP Theorem. Google Cloud Blog.](https://cloud.google.com/blog/products/databases/inside-cloud-spanner-and-the-cap-theorem)
15. [Eric Brewer (2017). Spanner, TrueTime and the CAP Theorem. Google Research.](https://research.google/pubs/spanner-truetime-and-the-cap-theorem/)
16. [DeCandia et al. (2007). Dynamo: Amazon's Highly Available Key-value Store. SOSP 2007.](https://web.stanford.edu/class/cs244/papers/amazon-dynamo-sosp2007.pdf)
17. [Ben Darnell. What are the limits of the CAP theorem? Cockroach Labs.](https://www.cockroachlabs.com/blog/limits-of-the-cap-theorem/)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 19, 2026; Oct 15, 2026 · Edited: Oct 15, 2026; Oct 25, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Databases and data systems › Database theory and data modeling › Transactions and concurrency theory

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/cap-theorem

English original: https://www.edgepedia.org/cap-theorem
