CAP Theorem Explained with Real-World Examples

CAP Theorem Explained with Real-World Examples

CAP Theorem states that in a distributed system (like databases or cloud services), you can only guarantee two out of three properties at once:

Consistency (C)

All nodes see the same (latest) data at the same time.

📌 Example: Online banking system

  • When you transfer money, your balance should update instantly across all devices and branches.

  • If consistency is enforced, you might experience a brief delay during network issues because the system won’t allow outdated or conflicting balances.

đź’ˇ Trade-off: If the system prioritizes Consistency, transactions may be temporarily blocked until all servers agree on the correct balance.

Availability (A)

Every request gets a response, even if some nodes fail.

📌 Example: Google Search or Social Media Feeds

  • When you Google something, it always gives you results - even if some servers are slow or unavailable.

  • Similarly, on Instagram, you can browse your feed even if new posts take a few seconds to appear.

💡 Trade-off: Prioritizing Availability means sometimes you might see slightly outdated data. For example, if your Instagram likes count isn’t instantly accurate, but the app keeps working.

Partition Tolerance (P)

The system keeps working even if network links break between nodes.

📌 Example: E-commerce website during a sale

  • Suppose Amazon’s servers are spread across multiple locations, and during Black Friday, one data center loses connectivity.

  • If the system is partition tolerant, it will keep processing purchases from available servers instead of shutting down.

💡 Trade-off: If the system prioritizes Partition Tolerance, it might temporarily allow duplicate orders or inconsistent product availability (showing “in stock” when it’s actually sold out).

Real-World Tradeoffs

Most modern distributed systems choose AP (Availability + Partition Tolerance) because network failures are inevitable. However, financial systems like banks prefer CP (Consistency + Partition Tolerance) to ensure transaction accuracy.

1. CA (Consistency + Availability)

  • Sacrifices Partition Tolerance

  • Example: A single-database e-commerce site (like a small online store).

    • Câś…: All users see the same product stock.

    • Aâś…: Always available during normal operation.

    • P❌: Site crashes if the database server fails.

2. CP (Consistency + Partition Tolerance)

  • Sacrifices Availability

  • Example: A stock trading app.

    • Câś…: All traders see real-time, accurate prices (no stale data).

    • Pâś…: Survives network issues (trades pause but don’t corrupt i.e., selling the same stock twice at different prices).

    • A❌: Delays orders during outages.

3. AP (Availability + Partition Tolerance)

  • Sacrifices Consistency

  • Example: Social media (Facebook/Instagram).

    • Aâś…: Always lets you post, even during outages.

    • Pâś…: Works globally across spotty networks.

    • C❌: You might see outdated likes/comments temporarily.

CAP Theorem Explained with Real-World Examples

https://mehamasum.github.io/blog/2025/4/cap-theorem-with-example/

Author

Mehedi Hasan Masum

Posted on

2025-04-20

Licensed under

CC BY-NC-SA 4.0

Comments

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

Ă—