Notes
All notes

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:

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
\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:

  1. 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.
  2. Consistency. Need ACID across entities? → RDBMS/distributed SQL. Eventual is fine? → NoSQL families.
  3. 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.
  4. 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

QQuestionSelf-check

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.