🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

CAP Theorem

TheoryDistributed Systems TheoryđŸŸĸ Free Lesson

Advertisement

System Design - Theory

CAP Theorem

The CAP theorem is one of the most fundamental results in distributed systems theory. It defines the inherent trade-offs every distributed system must make, and understanding it is essential for making informed architectural decisions.

  • Consistency - Every read returns the most recent write
  • Availability - Every request receives a response
  • Partition Tolerance - System continues operating despite network failures

Distributed systems are not about choosing the right answer - they are about choosing the right trade-off.

The CAP Theorem

CConsistencyAAvailabilityPPartition ToleranceCACPAP

What Each Guarantee Means

Consistency

Availability

Every non-failing node must return a response. The system cannot refuse to serve requests, even if some nodes are down or unreachable.

Partition Tolerance

The system must continue to function when network partitions occur. Since networks are inherently unreliable, partition tolerance is not optional in practice.

Why You Cannot Have All Three

Real-World Systems by CAP Choice

CP Systems (Consistency + Partition Tolerance)

SystemDescription
HBaseDistributed column-store, strong consistency
MongoDB (default)Document database with majority write concern
ZooKeeperDistributed coordination, consensus-based
etcdKey-value store using Raft consensus
Google SpannerGlobally distributed, externally consistent

AP Systems (Availability + Partition Tolerance)

SystemDescription
CassandraDistributed wide-column, eventual consistency
DynamoDBAWS key-value store, eventually consistent reads
CouchDBDocument database with multi-master replication
RiakDistributed key-value, eventually consistent
DNSEventually consistent, highly available

CA Systems (Consistency + Availability)

Consistency Models

Consistency Spectrum

LinearizableSequentialCausalEventualWeakStrongWeakConsistency: Strong ---------------------------------------> WeakPerformance: Slow ----------------------------------------> Fast
ModelDescriptionSystems
LinearizableStrongest; reads return latest writeSpanner, ZooKeeper
SequentialOperations appear in some sequential orderRaft, Paxos
CausalPreserves causal relationshipsMongoDB (causal sessions)
EventualAll replicas converge eventuallyCassandra, DynamoDB
WeakNo ordering guaranteesDNS, some NoSQL defaults

PACELC Theorem

The PACELC theorem extends CAP by considering what happens when there is no partition.

PACELC Classification

SystemPartitionElseClassification
CassandraAL (latency)EL
DynamoDBALEL
MongoDBCC (strong consistency)PC/EC
HBaseCCPC/EC
CockroachDBCCPC/EC
Cosmos DBConfigurableConfigurableConfigurable

Eventual Consistency

Convergence Time

Conflict Resolution

When replicas diverge, conflicts must be resolved:

StrategyDescriptionTrade-off
Last-Writer-Wins (LWW)Timestamp-based, latest winsSimple, may lose writes
Vector ClocksTrack causal orderingComplex, requires metadata
CRDTsConflict-free replicated data typesAutomatically convergent
Application logicBusiness rules resolve conflictsFlexible, but complex

Practice Exercises

  1. Analysis: For each of the following systems, classify as CP, AP, or CA and explain your reasoning: (a) a banking system, (b) a social media feed, (c) a DNS system, (d) a stock trading platform.

  2. Trade-offs: You are designing a global user profile service. Users can update their profile from any region. Which CAP choice would you make? How does PACELC influence your decision?

  3. Design: Design a distributed counter that supports concurrent increments from multiple regions. What consistency model would you use? How do you handle conflicts?

  4. Theory: Prove that during a network partition, a system cannot simultaneously guarantee both consistency and availability. Use a two-node example.


What to Learn Next

-> Introduction to System Design Core principles, the design process, and trade-offs.

-> Databases SQL vs NoSQL, indexing, replication, and sharding.

-> Caching Strategies Redis, Memcached, cache invalidation, and write strategies.

-> Microservices Service decomposition, discovery, and API gateways.

-> Scalability Fundamentals Vertical vs horizontal scaling and capacity planning.

-> Load Balancing Algorithms, health checks, and L4 vs L7.

Need Expert System Design Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement