CodeBlanks Logo Icon CodeBlanks

CAP theorem

Learn the CAP theorem and distributed system trade-offs.

CAP theorem

The CAP theorem says a distributed system can guarantee only two of three at once: consistency, availability and partition tolerance. When a network partition happens the system must pick C or A.

CP systems prefer consistency during a partition; writes are rejected. AP systems prefer availability; every node replies but data may be temporarily inconsistent. A capChoice(pick) function models the trade: it returns 'consistency over availability' for 'CP' and 'availability over consistency' for 'AP'. This reduces a distributed database's behavior to a single decision; later, when you choose between MongoDB, Cassandra and the like, this mental model guides you.

CP and AP systems

CP (consistency + partition) systems reject writes during a partition. This keeps data consistent but forces the user to wait. AP (availability + partition) systems reply to every request; data may be temporarily inconsistent but the system stays reachable.

Example: MongoDB leans CP by default; Cassandra leans AP. capChoice('CP') returns 'consistency over availability'; capChoice('AP') returns 'availability over consistency'. This simple model lets you see which side dominates when picking a database. In practice systems are not pure CP or AP; tunable settings place them between the two poles.

Practice Write the CAP trade function

Write function capChoice(pick). Start with if (pick === 'CP') return 'consistency over availability';. Add if (pick === 'AP') return 'availability over consistency';. End with return 'invalid';. Print console.log(capChoice('CP')) and console.log(capChoice('AP')).

Starter code

index.js
// Write the capChoice function
// Return 'consistency over availability' for 'CP'
// Return 'availability over consistency' for 'AP'
// Print two calls
// write here

Sample solution

index.js
function capChoice(pick) {
  if (pick === 'CP') return 'consistency over availability';
  if (pick === 'AP') return 'availability over consistency';
  return 'invalid';
}
console.log(capChoice('CP'));
console.log(capChoice('AP'));

Expected output

consistency over availability
availability over consistency

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which two options does the system pick from during a partition?

  • Consistency only
  • All three at once
  • Partition tolerance only
  • Consistency or availability Correct answer

Explanation: The system picks consistency or availability during a partition.

2 Complete the CP branch

Question
if (pick === '____') return 'consistency over availability';
Answer
if (pick === 'CP') return 'consistency over availability';

Explanation: The CP branch prioritizes consistency.

3 Match each concept to its role

Term Meaning
consistency Data stays correct
availability Every request gets a reply
partition Network split
CP Consistency first

All lessons in Expert