Load/Bearing
Fundamentals · 05 of 22

SQL vs NoSQL

Choose a database by its data model and access patterns. The label on the box matters much less.

3 min readUsed in: 12 · Hypergrowth

"SQL or NoSQL?" is a question about trade-offs: how your data relates, which queries must be fast, how much consistency you need, and how far writes must scale. A good answer starts from the access patterns and picks the store that serves them, and it's fine to use more than one.

Relational databases

PostgreSQL, MySQL and SQL Server store data in tables with a fixed schema, connect them with joins, and wrap changes in ACID transactions: atomic, consistent, isolated and durable. You can ask questions nobody planned for, because SQL can join and filter any columns.

They scale up well (one large server handles a lot) and scale reads with replicas. Scaling writes past one primary means sharding, which the database doesn't do for you, unless you add a layer such as Vitess or Citus or use a distributed SQL database (Spanner, CockroachDB, YugabyteDB).

The NoSQL families

FamilyData modelExamplesGood at
Key-valuekey → opaque valueRedis, DynamoDB, MemcachedVery fast lookups by key, caching, sessions, counters
Documentkey → JSON-like documentMongoDB, Couchbase, FirestoreNested objects read and written as a whole
Wide-columnpartition key → sorted rows of columnsCassandra, ScyllaDB, Bigtable, HBaseHuge write volume, time-ordered data, multi-region
Graphnodes and edgesNeo4j, Amazon NeptuneMany-hop relationships (friends of friends, fraud rings)
Searchinverted index over documentsElasticsearch, OpenSearchFull-text search, relevance, faceted filters
Time-seriesmetric + timestamp → valuePrometheus, InfluxDB, TimescaleDBMetrics, downsampling, retention windows

They have little in common except not being relational. What most of them share is that they are designed to partition automatically across many machines, in exchange for giving up general joins and, often, multi-row transactions.

The real trade-offs

RelationalTypical NoSQL (Cassandra, DynamoDB)
SchemaEnforced by the databaseEnforced by your code (schema-on-read). It still exists.
QueriesAny query, with joinsOnly the queries the keys were designed for
TransactionsMulti-row ACIDUsually single-item or single-partition; limited multi-item
ConsistencyStrong on the primaryOften tunable: eventual by default, strong at extra cost
Write scalingOne primary, unless you shard by handBuilt in: add nodes and data rebalances
ModellingNormalise, then joinDenormalise: one table per query pattern

Design from access patterns

With a store like DynamoDB or Cassandra, list the queries first, then design keys that answer each one by reading a single partition. A partition key decides which node holds the data, and a sort key orders items inside the partition.

query: "latest 20 messages in conversation C" table: messages partition key = conversation_id sort key = sent_at (descending) → one partition read, already sorted, no join

The same data might be written twice, for example messages by conversation and by user, because each query needs its own layout. Denormalising like this moves cost from reads to writes and puts the job of keeping copies in sync on you.

CAP, stated correctly

The CAP theorem says that during a network partition, a distributed store must choose between consistency (every read sees the latest write, or fails) and availability (every request gets an answer, possibly stale). It doesn't mean "pick two of three" in normal operation. PACELC adds the everyday case: else, when there's no partition, you trade latency against consistency. Cassandra and DynamoDB lean towards availability and low latency. Spanner leans towards consistency.

A sensible default

In the game: the database is a relational primary that you scale with replicas and shards by hand. The six shards you need in Hypergrowth are roughly what Cassandra or DynamoDB would give you automatically, with the same rule underneath: writes scale only when each one lands on a single partition.

Interview checklist

Practise it

Level 12: Hypergrowth

Put this into practice against live traffic. Your progress in the game is saved in your browser.

Play level 12

Further reading