SQL vs NoSQL and the Data-Store Zoo (System Design #10)
A team argues for a week about Postgres versus MongoDB for a new feature. In the end, the data is 200 GB, has clear relationships, needs transactions, and is read 100× more than written. Postgres wins instantly, and the week was wasted. The database debate is usually decided by four questions — data shape, access pattern, consistency, and scale — not by fashion.
This chapter builds a decision procedure. It covers relational databases and the four NoSQL families, what each is genuinely good at, and why most mature systems end up with more than one store (polyglot persistence) — carefully, because every extra store is operational cost.
Relational databases (RDBMS)
Tables with a fixed schema, rows, and typed columns; related rows joined on keys. The defining feature is ACID transactions:
- Atomicity — all-or-nothing.
- Consistency — the database invariant holds before and after.
- Isolation — concurrent transactions do not corrupt each other.
- Durability — once committed, it survives a crash.
They also give you strong integrity (foreign keys, unique constraints), a mature query language (SQL), and battle-tested scaling paths (replicas, partitioning). PostgreSQL, MySQL, and SQL Server are the workhorses of the industry.
Use an RDBMS when the data is relational, you need transactions, the schema is stable, and the working set fits (or can be sharded). That is the majority of applications, including many that reach user scale — a well-tuned Postgres serves a lot of traffic.
The four NoSQL families
“NoSQL” is not one thing. The four families solve different problems, and conflating them is how teams pick the wrong store.
| Family | Model | Strength | Examples |
|---|---|---|---|
| Key-value | key → value |
extreme scale, simple lookups | Redis, DynamoDB, Riak |
| Document | JSON documents | flexible schema, nested reads | MongoDB, CouchDB, Firestore |
| Wide-column | rows with dynamic column families | huge write volume, time-series | Cassandra, HBase, Bigtable |
| Graph | nodes + edges | relationship traversal | Neo4j, Neptune |
- Key-value. The simplest model:
get/putby key, often in memory. Lightning fast, trivially shardable, no queries beyond the key. Use for sessions, caches, feature flags, rate counters, and primary storage where access is always by key. - Document. Stores self-contained JSON documents; no joins, but you can embed related data. Flexible schema eases early product iteration. Use for content, catalogs, user profiles, and event payloads.
- Wide-column. Optimized for massive write throughput across many nodes; rows can have thousands of sparse columns. Queries are by partition key and clustering key. Use for time-series, activity feeds, and IoT at Cassandra/HBase scale.
- Graph. Stores relationships as first-class citizens and traverses them efficiently. Use for social graphs, recommendation, fraud detection, and knowledge graphs — anywhere “friends of friends” is the query.
\begin{tikzpicture}[font=\rmfamily, >=stealth,
b/.style={draw, thick, rounded corners=3pt, minimum width=2.2cm, minimum height=0.72cm, align=center, font=\rmfamily\small},
a/.style={->, thick}]
\node[b, fill=blue!10] (r) at (0,1.6) {RDBMS\\tables + ACID joins};
\node[b, fill=green!12] (kv) at (-3.4,-0.4) {key-value\\lookup by key};
\node[b, fill=orange!16] (doc) at (-1.2,-0.4) {document\\JSON, nested};
\node[b, fill=yellow!22] (wc) at (1.2,-0.4) {wide-column\\huge writes};
\node[b, fill=cyan!15] (g) at (3.4,-0.4) {graph\\traversal};
\draw[a] (r) -- (kv); \draw[a] (r) -- (doc); \draw[a] (r) -- (wc); \draw[a] (r) -- (g);
\end{tikzpicture}
Consistency and CAP by store
Where a store sits on the CAP spectrum (Chapter 3) is a first-class selection criterion:
| Store type | Typical posture | Note |
|---|---|---|
| Single-node RDBMS | CP | strong, single point |
| Distributed SQL (Spanner, CockroachDB) | CP (EC in PACELC) | strong across regions, latency cost |
| MongoDB (default) | CP-ish, tunable | can trade for availability per read |
| DynamoDB / Cassandra | AP (EL) | eventual by default, tunable quorums |
| Redis (single) | CP | in-memory, fast, durable options vary |
The practical filter: does this data tolerate eventual consistency? If yes, wide open. If no, distributed SQL or a single primary. This single question eliminates most options before performance even enters the conversation.
The decision procedure
Ask four questions in order:
- Data shape. Relational with joins? → RDBMS. Self-contained documents? → document. Simple key lookups? → key-value. Relationships as the query? → graph. Massive time-ordered writes? → wide-column.
- Consistency. Need ACID across entities? → RDBMS/distributed SQL. Eventual is fine? → NoSQL families.
- Access pattern. Query by key only? By many secondary indexes? By range/time? Full-text/geospatial? This often eliminates more options than the data model does.
- Scale. Fit on one primary plus replicas and a cache? → almost certainly Postgres/MySQL. Need >100K writes/s or >100 TB? → sharded NoSQL.
def choose_store(shape, needs_acid, access, writes_per_s):
if needs_acid or shape == "relational":
return "RDBMS (Postgres/MySQL); shard later if writes > 100k/s"
if shape == "documents":
return "Document store (MongoDB/Firestore)"
if shape == "key_only":
return "Key-value (Redis/DynamoDB)"
if shape == "relationships":
return "Graph (Neo4j)"
if shape == "time_series" and writes_per_s > 100_000:
return "Wide-column (Cassandra/HBase)"
return "RDBMS by default — it handles more than people think"
print(choose_store("relational", True, "joins+reports", 5_000))
print(choose_store("time_series", False, "range scan", 500_000))
Notice the default: relational unless proven otherwise. The burden of proof is on the exotic choice, because the relational store is the one your team already knows how to back up, monitor, and query.
Polyglot persistence (and its tax)
Real systems often use several stores, each for what it is best at: Postgres for transactions, Redis for sessions, Elasticsearch for search, S3 for blobs, Cassandra for the activity feed. This is polyglot persistence. It lets each workload use the right tool.
The tax is real: every store is another thing to deploy, secure, back up, upgrade, monitor, and staff. Each one is a new failure mode and a new consistency boundary between copies of the truth. A team that adopts five databases for a young product usually spends more time operating them than building features.
The discipline: one source of truth per piece of data, and add a store only when a measured bottleneck justifies it. If you cache Redis in front of Postgres, Postgres is still the truth; Redis is derived. Never let two stores both be authoritative for the same field.
\begin{tikzpicture}[font=\rmfamily, >=stealth,
b/.style={draw, thick, rounded corners=3pt, minimum width=1.9cm, minimum height=0.68cm, align=center, font=\rmfamily\small},
a/.style={->, thick}]
\node[b, fill=blue!10] (pg) at (0,1.3) {Postgres\\source of truth};
\node[b, fill=green!12] (redis) at (-3.2,-0.4) {Redis\\cache/session};
\node[b, fill=orange!16] (es) at (-1.0,-0.4) {Elasticsearch\\search index};
\node[b, fill=yellow!22] (s3) at (1.2,-0.4) {S3\\blobs};
\node[b, fill=cyan!15] (ch) at (3.4,-0.4) {Kafka\\event log};
\draw[a, dashed] (pg) -- (redis); \draw[a, dashed] (pg) -- (es);
\draw[a, dashed] (pg) -- (s3); \draw[a, dashed] (pg) -- (ch);
\node[font=\rmfamily\small, text=black!60] at (0,-1.1) {one authority; the rest are derived};
\end{tikzpicture}
Pitfalls
- Choosing by hype. MongoDB because it is “modern”, or Postgres because it is “safe”, without checking the four questions. Pick by access pattern.
- Believing “NoSQL is faster”. Speed depends on the workload; a bad access pattern is slow in any store, and key-value stores are fast only because they do less.
- No schema in a document store. “Schema-less” means schema lives in application code, where it drifts. Validate writes and version documents.
- Many stores, no owner. Polyglot persistence without operational ownership becomes a fleet of unmaintained databases.
- Using a cache as the source of truth. If Redis holds the only copy of a value, a flush is data loss. Keep one authority.
What four questions decide the store? Why is a shared relational database still the right default for most applications?
ASolution
Data shape, consistency needs, access pattern, and scale. Relational databases handle the majority of shapes, give ACID transactions and integrity, and scale further than people assume via replicas, caching, and partitioning — so they are the safe default unless a proven bottleneck or data shape argues otherwise.
Next: Databases at Scale (System Design #11) — replication, sharding, and keeping queries fast when one box is not enough.