CAP Theorem

The principle that a distributed system can only provide two out of three: Consistency, Availability, and Partition Tolerance

Overview

The CAP Theorem states that a distributed system can only guarantee two out of three properties: Consistency, Availability, and Partition Tolerance.

Since network partitions (P) are inevitable in distributed systems, you must choose between Consistency (C) and Availability (A). This fundamental trade-off shapes how distributed databases are designed.

Key Concepts

Consistency

All nodes see the same data at the same time. A read returns the most recent write.

Availability

Every request receives a response, even if some nodes are down. System remains operational.

Partition Tolerance

System continues to operate despite network partitions (nodes can't communicate). Required in distributed systems.

How It Works

Network Partition Scenario:

  • Node A and Node B can't communicate
  • Client writes to Node A: user.name = "Alice"
  • Client reads from Node B

CP System (Consistency over Availability):

  • Node B refuses to respond (not available)
  • Ensures client doesn't see stale data
  • Example: HBase, MongoDB (strong consistency mode)

AP System (Availability over Consistency):

  • Node B responds with old data
  • System stays available but inconsistent
  • Eventually syncs when partition heals
  • Example: Cassandra, DynamoDB

CA doesn't exist in distributed systems (partitions happen!)

Use Cases

CP: Banking (need exact balances), inventory systems, coordination services (ZooKeeper)

AP: Social media feeds (OK if likes count is temporarily wrong), analytics, caching

CP: Systems where correctness is critical

AP: Systems where uptime is critical

Best Practices

Understand your requirements: correctness vs availability

Choose database that matches your needs

Design for eventual consistency in AP systems

Implement conflict resolution for AP systems

Use CP for financial transactions, AP for user-generated content

Consider hybrid approach: CP for critical data, AP for non-critical

Interview Tips

What Interviewers Look For

  • Explain the CAP theorem triangle: can only pick 2 of 3

  • Clarify that P is required, so choice is really CP vs AP

  • Give examples: MongoDB (CP), Cassandra (AP)

  • Discuss eventual consistency in AP systems

  • Mention PACELC extension: in absence of partitions, trade-off between Latency and Consistency

  • Explain that CAP is spectrum, not binary (tunable consistency)

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In
CAP Theorem - Module 6: Advanced Distributed Systems | System Design | Revise Algo