Published on

Real-World Examples of the CAP Theorem in Action

We've discussed the CAP Theoremβ€”how distributed systems must choose two of three: πŸ“ Consistency, πŸ”„ Availability, and 🌐 Partition Tolerance.

Now, let's explore real systems and how they each make their CAP trade-offs.

πŸƒ MongoDB β€” CP (Consistency + Partition Tolerance)

MongoDB is a document-based NoSQL database that prioritizes strong consistency by default.

πŸ› οΈ How it works:

  • Uses a primary-secondary replication model.
  • Only the primary node accepts writes.
  • If the primary is partitioned or crashes, the cluster elects a new one.
  • During this election, writes are blocked (availability is sacrificed).

🎯 Why? To prevent multiple primaries and ensure data consistency.

βœ… Great for:

  • Applications where stale or conflicting data is unacceptable (e.g., transactions, profiles).
  • Systems needing event ordering or integrity.

❌ Trade-Off:

  • Brief unavailability during failover.
  • Sacrifices uptime for correctness.

🌍 Cassandra β€” AP (Availability + Partition Tolerance)

Cassandra is a highly available distributed database known for horizontal scalability.

πŸ› οΈ How it works:

  • No single leader – any node can handle reads/writes.
  • During a partition, all nodes continue operating.
  • Writes might conflict, but are reconciled later using timestamps and background repair processes.

βš™οΈ Offers tunable consistency, but defaults to β€œalways available” mode.

🎯 Why? Designed for always-on systems where availability > strict correctness.

βœ… Great for:

  • User activity feeds
  • IoT data
  • Web apps where delays are worse than slight staleness

❌ Trade-Off:

  • You might read outdated data temporarily
  • Eventual consistency by default

🧭 ZooKeeper β€” CP (Consistency + Partition Tolerance)

ZooKeeper is a coordination service used in distributed systems for leader election, configuration, and more.

πŸ› οΈ How it works:

  • Uses the Zab protocol requiring quorum-based consensus.
  • If a quorum is lost (e.g., due to a network partition), the minority stops responding.
  • ZooKeeper refuses operations it cannot guarantee are consistent.

🎯 Why? To preserve a single agreed state (vital for consensus-critical services like leader election).

βœ… Great for:

  • Distributed locks
  • Service discovery
  • Cluster coordination (like Kafka, HDFS)

❌ Trade-Off:

  • Availability sacrificed during partition
  • Strong consistency is non-negotiable

πŸ›’ DynamoDB β€” AP (Availability + Partition Tolerance)

Amazon's DynamoDB is a cloud-native key-value store, inspired by the Amazon Dynamo paper.

πŸ› οΈ How it works:

  • Automatically replicates across zones.
  • Continues to serve reads and writes even during failures.
  • Uses eventual consistency by default, but offers strong consistency within a region if configured.

🎯 Why? Built for always-on services like shopping carts and preferences, where availability is critical.

βœ… Great for:

  • E-commerce systems
  • Session storage
  • Gaming state and preferences

❌ Trade-Off:

  • Data may briefly diverge across replicas
  • Strong consistency costs performance and must be explicitly enabled

🧩 Summary Table

SystemTypeGuaranteesSacrificesBest For
MongoDBCPStrong Consistency, P-toleranceAvailabilityFinancial or profile data
CassandraAPHigh Availability, P-toleranceImmediate ConsistencyReal-time feeds, logs
ZooKeeperCPStrict Consensus, P-toleranceAvailabilityLeader election, config mgmt
DynamoDBAPAlways-on availability, P-toleranceStrong consistency (by default)Shopping carts, user preferences

🎯 Final Thought

There's no perfect systemβ€”only trade-offs.

Each of these tools excels in a specific niche by choosing two CAP properties over a third, based on what matters most: consistency, uptime, or resilience. Understanding these trade-offs helps you pick the right system for your needs.