DEV Community

Cover image for CAP Theorem Explained: Why Distributed Systems Can't Have Everything
Moinul Islam
Moinul Islam

Posted on

CAP Theorem Explained: Why Distributed Systems Can't Have Everything

Imagine you have a database running on multiple servers (nodes) spread across different locations. These nodes talk to each other over a network. CAP Theorem says: when something goes wrong with that network connection, you are forced to make a choice.
The three letters stand for:

Consistency — if you write data to one node, anyone reading from any other node sees that same data immediately. No one gets an old copy.
Availability — the system always gives you a response. Even if some nodes are down or disconnected, it never says "I can't answer right now."
Partition Tolerance — the system keeps running even when nodes cannot talk to each other (a network partition). Messages get lost or delayed between servers.

The theorem says: you cannot have all three at the same time when a network partition happens. And network partitions do happen in real systems. So in practice, the real choice is: when two nodes get disconnected from each other, do you want the system to stay correct, or stay available?

If you choose Consistency over Availability (CP): the system stops responding until it can confirm all nodes agree on the same data. You get correct data or no data. Example: a bank account balance — you would rather get an error than a wrong number.

If you choose Availability over Consistency (CA): the system always responds, but different nodes might give you slightly different data for a short time until they sync up. Example: a social media post like count — it is fine if two users see 1,021 and 1,023 likes for a few seconds.

That short period where nodes are out of sync is called eventual consistency — the data will become consistent eventually, just not instantly.
Here is the diagram showing exactly what happens when a partition occurs:

#CAPTheorem #DistributedSystems #SystemDesign #SoftwareEngineering #BackendDevelopment #Database #ScalableSystems #CloudComputing #TechEducation #ComputerScience #Programming #LearningInPublic

Top comments (0)