What is the CAP theorem and why it matters
· Category: System Design
Short answer
The CAP theorem states that a distributed data store can simultaneously guarantee at most two of consistency, availability, and partition tolerance.
How it works
Consistency means every read receives the most recent write. Availability means every request receives a non-error response. Partition tolerance means the system continues operating despite network failures that prevent communication between nodes. Since network partitions are inevitable in distributed systems, architects must choose between strong consistency and high availability during a partition. Systems like traditional SQL databases prioritize consistency, while NoSQL stores often favor availability.
Example
During a network split between two data centers, a bank might choose consistency by rejecting transactions rather than risking double spending. A social media feed might choose availability by showing slightly stale data so users can still post.
Why it matters
Understanding CAP trade-offs guides database selection, replication strategies, and failover design. It helps teams set realistic expectations and choose technologies aligned with business priorities rather than chasing impossible guarantees.
Example
# Consistent hashing for service discovery
import hashlib
def get_node(key, nodes):
hash_val = int(hashlib.md5(key.encode()).hexdigest(), 16)
return nodes[hash_val % len(nodes)]
node = get_node('user-123', ['node-a', 'node-b', 'node-c'])
This snippet implements consistent hashing to distribute keys across nodes, a foundational technique in scalable distributed systems.