用通俗语言理解 CAP 定理
您将理解影响每个分布式数据库的一致性、可用性和分区容错性之间的权衡。
用通俗语言理解 CAP 定理 是 CoddyKit 上的免费 MongoDB Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 MongoDB Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 MongoDB Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Distributed Systems Are Hard
Spreading data across many servers buys scale but brings distributed systems headaches: nodes must agree even when the network hiccups. Enter the CAP theorem.
The Three Properties: C, A, P
The CAP theorem says a distributed store can guarantee only two of three: Consistency, Availability, and Partition tolerance. The catch: partitions always happen.
Consistency Explained
Consistency means every read sees the latest write, from any node. Deposit money and check any ATM — you'd see the new balance. Reliable, but it costs speed.
Availability Explained
Availability means every request gets a response — never an error, even if the answer is slightly stale. A cart that always lets you add items chose this.
Partition Tolerance Explained
A partition is when nodes can't talk to each other — and in real networks, it will happen. So the real choice is always Consistency vs Availability during one.
CP Systems: Choose Consistency
CP systems pick consistency: during a partition they'd rather return an error than stale data. Right for ledgers and inventory, where wrong answers are costly.
AP Systems: Choose Availability
AP systems pick availability: they always answer, even with slightly old data, then sync up later. Perfect for social feeds, reviews, and shopping carts.
Where MongoDB Falls in CAP
MongoDB is CP by default. Only the primary node takes writes, so during a partition it briefly pauses writes rather than risk conflicts. The code shows read options.
// MongoDB read preference configuration in Node.js driver
const client = new MongoClient(uri, {
readPreference: 'primaryPreferred'
// 'primary' - CP: always fresh, fails during election
// 'primaryPreferred' - slightly more available, mostly fresh
// 'secondary' - AP: always available, may be stale
// 'nearest' - lowest latency node
});Eventual Consistency Demystified
Eventual consistency means nodes may briefly disagree, but given a moment, all reads catch up to the latest value. Nothing is lost — it just takes a beat.
PACELC: Beyond CAP
CAP only covers partitions, which are rare. PACELC adds the everyday trade-off: even with no partition, you choose between low latency and strong consistency.
Practical Implications for Developers
CAP helps you design wisely: expect slightly stale data, handle retries gracefully, and pick majority write concern for critical operations. The code shows how.
// Use majority write concern for critical operations
db.orders.insertOne(
{ _id: 'ord_99', item: 'Headphones', qty: 1 },
{ writeConcern: { w: 'majority', j: true } }
);
// w:'majority' = acknowledged by majority of replica set members
// j:true = must be written to journal (durable on disk)Quick Check
Test your understanding of MongoDB & NoSQL Databases concepts from this lesson.
Lesson Recap
You learned the CAP theorem, why partition tolerance is non-negotiable, and that MongoDB is CP by default. Next: exactly where MongoDB fits best.
常见问题解答
「用通俗语言理解 CAP 定理」课时是免费的吗?
是的 — 「用通俗语言理解 CAP 定理」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 MongoDB Academy 课程的其余内容,请升级到 CoddyKit PRO。 MongoDB Academy 课程共包含 4 节课。
「用通俗语言理解 CAP 定理」这节课中我会学到什么?
您将理解影响每个分布式数据库的一致性、可用性和分区容错性之间的权衡。 你通过在浏览器中直接运行的动手代码来练习 MongoDB Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 MongoDB Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 MongoDB Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「用通俗语言理解 CAP 定理」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 MongoDB Academy 课中编写并运行代码吗?
能。每节 MongoDB Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 关系型数据库的瓶颈
- NoSQL 类型:文档、键值、列式与图
- 用通俗语言理解 CAP 定理
- MongoDB 的定位