0Pricing
MongoDB Academy · Aula

O teorema CAP em linguagem simples

Entenda os compromissos entre consistência, disponibilidade e tolerância a particionamento que moldam todo banco de dados distribuído.

O teorema CAP em linguagem simples é uma aula grátis de MongoDB Academy no CoddyKit. Esta é a aula 3 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de MongoDB Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de MongoDB Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

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.

Perguntas Frequentes

A aula “O teorema CAP em linguagem simples” é grátis?

Sim — o texto completo de “O teorema CAP em linguagem simples” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de MongoDB Academy, atualize para CoddyKit PRO. O curso de MongoDB Academy inclui 4 aulas no total.

O que vou aprender em “O teorema CAP em linguagem simples”?

Entenda os compromissos entre consistência, disponibilidade e tolerância a particionamento que moldam todo banco de dados distribuído. Você pratica MongoDB Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar MongoDB Academy?

Nenhuma experiência prévia é necessária. MongoDB Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 3 de 4.

Quanto tempo leva a aula “O teorema CAP em linguagem simples”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de MongoDB Academy?

Sim. Cada aula de MongoDB Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. O gargalo dos bancos de dados relacionais
  2. Tipos de NoSQL: documentos, chave-valor, colunas e grafos
  3. O teorema CAP em linguagem simples
  4. Onde o MongoDB se encaixa
← Voltar para MongoDB Academy