Chapter 24: Why NoSQL? CAP Theorem and Trade-offs
NoSQL databases exist because relational databases have limitations at extreme scale. Understanding when (and when NOT) to use NoSQL is critical.
The CAP Theorem
In a distributed system, you can only guarantee two of three:
Consistency
/\
/ \
/ \
/ CA \ CP
/________\
Availability — Partition Tolerance
CA: Single-node RDBMS (PostgreSQL standalone)
CP: Consistent distributed (MongoDB, HBase) — may reject requests during partition
AP: Available distributed (Cassandra, DynamoDB) — may return stale data during partition
In practice: network partitions WILL happen, so you choose between C and A.
When Relational Databases Struggle
- Massive write throughput (>100K writes/sec) — single-node RDBMS bottlenecks
- Horizontal scaling — RDBMS is hard to shard (JOINs across shards are painful)
- Flexible schema — ALTER TABLE on a billion-row table takes hours
- Geographic distribution — multi-region with low latency
When to Stay Relational
- Complex queries with JOINs
- ACID transactions across multiple entities
- Data integrity is critical (financial, medical)
- Your data fits on one machine (most applications!)
- You don't know your access patterns yet
⚠️ The Most Common Mistake
Choosing NoSQL because it's "modern" or "scalable" when your data is clearly relational and fits on one PostgreSQL instance. A single PostgreSQL server handles millions of rows and thousands of queries/second. You probably don't need NoSQL.
BASE vs ACID
| ACID (Relational) | BASE (NoSQL) |
|---|---|
| Atomic | Basically Available |
| Consistent | Soft state |
| Isolated | Eventually consistent |
| Durable |
BASE means: the system is always available, data may be temporarily inconsistent, but will converge to consistency eventually.
Key Takeaways
- CAP theorem: in distributed systems, choose Consistency or Availability during partitions
- NoSQL trades consistency/query flexibility for scale/availability
- Use NoSQL when: massive scale, write-heavy, flexible schema, geo-distributed
- Stay relational when: complex queries, ACID needed, data fits one machine
- Default to PostgreSQL. Move to NoSQL only with measured evidence of need.