What is the CAP theorem?
MediumConceptNoSQL, Replication & Sharding
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
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | What is the difference between SQL and NoSQL databases? | Concept | Easy | Read the answer to What is the difference between SQL and NoSQL databases? | |||
| 2 | What are the different types of NoSQL databases? | Concept | Easy | Read the answer to What are the different types of NoSQL databases? | |||
| 3 | What is the CAP theorem? | Concept | Medium | Read the answer to What is the CAP theorem? | |||
| 4 | What is database replication, and what are its types? | Concept | Medium | Read the answer to What is database replication, and what are its types? | |||
| 5 | What is sharding, and how is it different from partitioning? | Concept | Medium | Read the answer to What is sharding, and how is it different from partitioning? |