CAP theorem

Pick 2 of 3 · Part of Databases

Key idea

Practice

7 questions · about 4 min

Explained step by step

CAP theorem

The CAP theorem says a distributed system can only give 2 of 3 characteristics at once: Consistency (everyone sees the same data), Availability (everyone gets an answer) and Partition tolerance (it keeps working even if the network between nodes is cut). It's like a restaurant with 2 branches and the phone line between them down: either they wait to talk so they don't sell the same dish twice, or they keep selling and fix it later.

A CA database gives consistency and availability across all its nodes, but doesn't tolerate partitions: if the network between 2 nodes is cut, it can't keep giving both. So it's not tolerant of network failures. In practice these are databases running in 1 single place, like PostgreSQL or MariaDB on 1 server. It's like a family living in 1 house: everyone hears everything, but it doesn't work across 2 cities.

A CP database gives consistency and partition tolerance at the expense of availability. If the network between 2 nodes is cut, it shuts down the node that can't stay up to date until the network is fixed. That way nobody reads old data, but some requests get no answer. It's like an ATM that, if it loses its connection to the bank, stops giving out cash instead of taking the risk. Examples: MongoDB and Apache HBase.

An AP database gives availability and partition tolerance at the expense of consistency. If the network is cut, all nodes keep answering, but those on the wrong side may return old data. When the network comes back, the database syncs the nodes and fixes the differences. It's like 2 branches that keep selling without talking and balance the till at the end of the day. Examples: Apache Cassandra and CouchDB.

Consistency

In CAP, consistency means every client sees the same data at the same time, no matter which node they connect to. For that, every time data is written to a node, it has to be copied to all the others before the write counts as successful. It's like a group that doesn't start eating until all 5 have arrived: slower, but nobody misses anything.

Availability

In CAP, availability means every client asking for data gets an answer, even if 1 or more nodes are down. It doesn't promise the answer is the newest, only that it arrives. It's like a newsstand that's always open: maybe it doesn't have today's paper, but it sells you yesterday's instead of closing. With 3 nodes and 1 down, the other 2 keep answering.

Partition tolerance

Partition tolerance means the system keeps working even if messages are lost or the network between some nodes is cut. Since real-world networks fail, distributed systems must choose P no matter what. That's why the real CAP choice is between consistency (C) and availability (A). It's like a company with 2 offices: some day the internet between them will go down, and you must decide beforehand what to do.

Terms in this lesson

Arki, the beaver mascot

Your next interview starts with 5 minutes today.

Free on iOS and Android. Start from zero, no account needed.

Key idea

Pick 2 of 3