MongoDB Academy · Lezione

Il teorema CAP in parole semplici

Comprenderà i compromessi tra consistenza, disponibilità e tolleranza al partizionamento che caratterizzano ogni database distribuito.

Lezione 3 di 413 passaggi

Il teorema CAP in parole semplici è una lezione MongoDB Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento MongoDB Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso MongoDB Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.

Gratis per iniziare

Impara JavaScript con un tutor IA — gratis

Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.

Corsi
30
Lezioni
120

Domande Frequenti

La lezione «Il teorema CAP in parole semplici» è gratuita?

Sì — il testo completo di «Il teorema CAP in parole semplici» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso MongoDB Academy, passa a CoddyKit PRO. Il corso MongoDB Academy include 4 lezioni in totale.

Cosa imparerò in «Il teorema CAP in parole semplici»?

Comprenderà i compromessi tra consistenza, disponibilità e tolleranza al partizionamento che caratterizzano ogni database distribuito. Eserciti MongoDB Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare MongoDB Academy?

Non è richiesta alcuna esperienza precedente. MongoDB Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.

Quanto tempo richiede la lezione «Il teorema CAP in parole semplici»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione MongoDB Academy?

Sì. Ogni lezione MongoDB Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Il collo di bottiglia dei database relazionali
  2. Varianti NoSQL: documenti, chiave-valore, colonne, grafi
  3. Il teorema CAP in parole semplici
  4. Il ruolo di MongoDB
← Torna a MongoDB Academy