Skip to content
We areTOP 1%

What is the CAP theorem?

MediumConceptNoSQL, Replication & Sharding
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews

Answer

The CAP theorem states that when a network partition occurs, a distributed data store cannot guarantee both consistency and availability; it has to give up one of them.

  • Consistency (C): every read sees the most recent write. This is linearizability, not the C of ACID.
  • Availability (A): every request to a working node receives a non-error response.
  • Partition tolerance (P): the system keeps working even when messages between nodes are lost.
  • Partitions cannot be ruled out in a real network, so the practical choice is CP (refuse some requests during a partition to stay consistent, as HBase and ZooKeeper do) or AP (keep answering, possibly with stale data, as Cassandra and DynamoDB do with default settings). When there is no partition, a system can provide both C and A.

Example: Two replicas of a bank balance lose contact, and a customer withdraws 500 at replica 1. If replica 2 is then asked for the balance, a CP system returns an error until the link is restored, while an AP system returns the old balance, which becomes correct later (eventual consistency).

More in NoSQL, Replication & Sharding

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account