DEV Community

Hrishikesh Dalal
Hrishikesh Dalal

Posted on

System Design - EP 4.2 - CA vs. AP vs. CP - What to Choose and When

There is No "Best" Database

Students often ask, "Is MongoDB better than Postgres?" The real answer depends on where you stand on the CAP triangle.

1. CP (Consistency + Partition Tolerance)

Goal: The data must be correct, even if the system has to shut down to ensure it.

  • When to choose: Banking systems, ATM transactions, and stock trading. You’d rather the ATM say "Service Unavailable" than give out money you don't actually have in your account.
  • Examples: Redis, MongoDB (in certain configs), HBase.

2. AP (Availability + Partition Tolerance)

Goal: The system must stay online, even if some users see slightly "old" data for a few seconds.

  • When to choose: Social media feeds, YouTube comments, or "Likes" on a post. If I see 99 likes and you see 100, the world won't end. But if the "Like" button stops working entirely, that's a bad user experience.
  • Examples: Cassandra, DynamoDB, CouchDB.

3. CA (Consistency + Availability)

Goal: Perfect data and 100% uptime.

  • The Reality: This only works if your network never fails. Since network partitions are inevitable in distributed systems, CA is basically impossible for a truly distributed system. It usually refers to single-node databases like a standard MySQL setup.

[Image comparing CP vs AP systems with real-world application examples]

Final Verdict

  • Pick CP when the accuracy of data is the primary value.
  • Pick AP when the uptime of the system is the primary value.

Top comments (0)