Interview Question: Explain CAP Theorem

Publish date: 2025-01-29
Tags: Distributed-Systems, Interview-Questions

1. What is the CAP Theorem?

The CAP Theorem states that a distributed system can only guarantee two out of three properties at the same time:

  1. Consistency (C): All users see the same data simultaneously.
  2. Availability (A): The system always responds to requests (even with stale data).
  3. Partition Tolerance (P): The system works even if parts of it lose communication.

Key Insight: Network partitions (e.g., server crashes, dropped messages) are unavoidable. During a partition, you must choose between C and A.

Explain CAP Theorem

2. Real-World Example: Rohit’s “Reminder” Startup

Imagine Rohit runs a company called Reminder where he writes down customer reminders in a diary. When he hires Raj to help, problems arise:

Problem 1: Inconsistency

Problem 2: Availability Issues

Problem 3: Network Partition

3. CAP Trade-offs: CP vs. AP vs. CA

System Type Prioritizes Sacrifices Example
CP Consistency + Partition Tolerance Availability Banking systems (e.g., PostgreSQL)
AP Availability + Partition Tolerance Consistency Social media (e.g., Cassandra)
CA Consistency + Availability Partition Tolerance Single-server databases (e.g., non-replicated MySQL)

⚠️ CA systems are theoretical – real-world distributed systems must handle partitions (P).

4. PACELC Theorem: Beyond CAP

The PACELC Theorem extends CAP to address normal operations (no partitions):

Examples

Database CAP Type Use Case
Cassandra AP High availability (social media, IoT)
MongoDB CP (tunable) Consistency-focused apps (e-commerce)
DynamoDB AP (default) Scalable web apps (Amazon)

Key Takeaways

CAP Theorem: Choose 2/3 properties during a partition.
PACELC: Adds latency vs. consistency trade-offs in normal conditions.
AP vs. CP: Match the system to the use case (e.g., social media = AP, banking = CP).

Further Reading:

Tags: Distributed-Systems, Interview-Questions