DEV Community

Sreekar Reddy
Sreekar Reddy

Posted on • Originally published at sreekarreddy.com

πŸ“ CAP Theorem Explained Like You're 5

Pick two: consistency, availability, partition tolerance

Day 124 of 149

πŸ‘‰ Full deep-dive with code examples


The Three Wishes Analogy

A genie says: "Pick any TWO wishes, but not all three"

  • Wish 1: Fastest car
  • Wish 2: Cheapest car
  • Wish 3: Most reliable car

You can have fast + cheap (less reliable), or cheap + reliable (not fast).

CAP is like thisβ€”but the real trade-off shows up when the network is split (a partition).


The Three Properties

C - Consistency:

  • Reads behave as if there were a single up-to-date copy (strong consistency)
  • In practice, β€œconsistency” has levels; CAP discussions usually mean a very strong form

A - Availability:

  • Every request gets a response from a non-failing node
  • That response might be an error (the key point is: the system doesn’t stop responding)

P - Partition Tolerance:

  • The system keeps operating even if messages between servers are dropped/delayed (a network partition)

Why You Can't Have All Three

Imagine two servers that should stay in sync:

Server A ←─ network break ─→ Server B
Enter fullscreen mode Exit fullscreen mode

Network breaks. A user writes to Server A.

Option 1: Stay Consistent

  • Don't let Server B respond until synced
  • But Server B becomes unavailable!

Option 2: Stay Available

  • Both servers respond with what they have
  • But data is now inconsistent!

During a partition, you must choose what to prioritize.


Real-World Choices

CP (Consistency + Partition Tolerance):

  • Bank transactions
  • Might be briefly unavailable

AP (Availability + Partition Tolerance):

  • Social media feeds
  • Might show slightly stale data

CA (rarely used):

  • Only works when you assume no partitions (for example: a single-node system or a very reliable network)

In One Sentence

CAP Theorem says that when a network partition happens, a distributed system has to trade off between consistency and availability.


πŸ”— Enjoying these? Follow for daily ELI5 explanations!

Making complex tech concepts simple, one day at a time.

Top comments (0)