πŸŽ‰ 75% of content is free forever β€” Unlock Premium from $10/mo β†’
CW
Search courses…
πŸ’Ό 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

DfCAP Theorem

The CAP Theorem (Brewer, 2000; Gilbert and Lynch, 2002) states that a distributed data store can provide at most two of the following three guarantees simultaneously:

  • Consistency (C): Every read receives the most recent write or an error
  • Availability (A): Every request receives a non-error response (without guaranteeing it is the most recent write)
  • Partition Tolerance (P): The system continues to operate despite network partitions (message loss or delay between nodes)

Since network partitions are inevitable in distributed systems, the practical choice is between CP (consistency + partition tolerance) and AP (availability + partition tolerance).

CConsistencyAAvailabilityPPartition ToleranceCACPAP

What Each Guarantee Means

Consistency

DfLinearizability (Strong Consistency)

A system is linearizable if every operation appears to take effect atomically at some point between its invocation and response. Once a write completes, all subsequent reads (from any node) must return that value or a later value. This is the strongest single-object consistency model.

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

Consider two nodes (N1, N2) with a network partition between them. A client writes to N1. When a client reads from N2:

  • If N2 responds (A), it must return stale data (not C)
  • If N2 waits for N1 to sync (C), it cannot respond (not A)

This is the fundamental tension. During normal operation (no partition), you can have both C and A. The theorem applies during partitions.

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)

True CA systems only exist in single-node deployments or when partitions cannot occur. In distributed systems, partitions are inevitable, so CA is not a practical choice. Traditional RDBMS (PostgreSQL, MySQL) are CA when running on a single node, but become CP or AP when distributed.

Consistency Models

DfConsistency Model

A consistency model defines the rules for when and how reads return values written by other operations. It exists on a spectrum from strong to weak consistency.

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.

DfPACELC Theorem

PACELC (Abadi, 2012) states: if there is a Partition, choose between Availability and Consistency; Else (when running normally), choose between Latency and Consistency. This captures the full spectrum of distributed system trade-offs.

PACELC Classification

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

PACELC is more practical than CAP for real-world decisions. Most systems are not in a partition state, so the "else" trade-off (latency vs consistency) matters more day-to-day. A system that always chooses consistency pays the latency cost even when the network is healthy.

Eventual Consistency

DfEventual Consistency

Eventual consistency is a consistency model where, if no new updates are made to a given data item, all replicas will eventually return the last written value. It provides high availability and low latency at the cost of temporary inconsistency.

Convergence Time

Eventual Convergence

lim⁑tto∞P(readi(t)=write(t0))=1\lim_{t \\to \infty} P(\text{read}_i(t) = \text{write}(t_0)) = 1

Here,

  • t0t_0=Time of the last write
  • tt=Current time (t > t_0)
  • readi(t)read_i(t)=Read from replica i at time t

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

DfCRDTs

Conflict-free Replicated Data Types (CRDTs) are data structures that can be replicated across multiple nodes and merged without coordination. They guarantee eventual consistency regardless of the order in which operations are applied. Examples include G-Counters, PN-Counters, G-Sets, OR-Sets, and LWW-Registers.

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.

Key Takeaways:

  • The CAP theorem proves that during network partitions, you must choose between consistency and availability
  • Network partitions are inevitable, so true CA systems cannot exist in distributed environments
  • PACELC extends CAP: even without partitions, you must trade latency for consistency
  • Consistency models range from linearizable (strong) to eventual (weak) with corresponding performance trade-offs
  • CRDTs provide mathematically guaranteed eventual consistency without coordination
  • The right CAP choice depends on the specific requirements of each component in your system

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.

⭐

Premium Content

CAP Theorem

Unlock this lesson and 900+ advanced tutorials with a Premium plan.

🎯End-to-end Projects
πŸ’ΌInterview Prep
πŸ“œCertificates
🀝Community Access

Already a member? Log in

Need Expert System Design Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement