Topic 061 / Core
NoSQL databases: key-value, document, wide-column, graph
Chapter 5.2 · NoSQL and Database Selection
What and why
Relational databases assume general joins and multi-row transactions matter more than horizontal scale or schema flexibility. NoSQL databases trade some of that generality for a specific access pattern done very well, at a scale a single relational node cannot reach. There is no single "NoSQL model" — four distinct families solve four distinct shapes of problem. Builds on: → 58 (database fundamentals).
Diagram
| Family | Shape | Example access | Gives up |
|---|---|---|---|
| Key-value | key → opaque blob | O(1) get/put by key | queries by value, joins |
| Document | key → nested JSON-like document | query by document fields | cross-document joins |
| Wide-column | partition key → sparse rows of columns | range scan within a partition | ad hoc cross-partition queries |
| Graph | nodes and typed edges | traverse relationships N hops deep | bulk tabular scans |
How it works
- Pick the family whose native access pattern matches the real query the application makes most — not the one with the most features.
- Key-value: every read/write names an exact key; no secondary query capability without building one yourself.
- Document: a document holds everything one read needs, so one fetch avoids a join — until a query needs to look across documents.
- Wide-column: data is physically organized by partition key; queries without the partition key must scan everything.
- Graph: edges are first-class, so multi-hop relationship queries that would be a chain of joins in SQL are a single traversal.
Practical example
The same catalog "product" modeled three ways: relationally it's a
row plus a join to order_item; as a document it's one self-contained blob
{"id": 1, "name": "...", "price_cents": 999, "tags": ["new","sale"]} fetched in one read
with no join; as a key-value entry it's product:1 -> <serialized blob>, fast to fetch by
id but unqueryable by tag without a separate index.
When to use it
- Use when: the access pattern is known and key-driven, must scale horizontally past what one relational node handles, or the data is genuinely schema-variable across records.
- Avoid when: the application needs ad hoc queries, joins, or multi-record transactions across the data — relational is the better fit (→ 62).
- Failure scenarios: modeling a wide-column store like a relational table and scanning across partitions on every query; a document store where documents grow without bound because every related fact gets nested in.
- Simpler alternative: a relational database with good indexing handles most workloads well past the point teams assume they need NoSQL.
Practice
(classification drill, 15–20 min). For each of five scenarios — a shopping cart keyed by session id, a product catalog with variable attributes per category, a sensor-reading log queried by device and time range, a social "who follows whom" graph, and a financial ledger — name the matching family (or "relational") and the one access pattern that decided it. Done when every answer names the pattern, not the brand.
Key takeaway
Four NoSQL families solve four distinct access patterns; none of them is a general replacement for relational joins and transactions. Done when you can: match a described access pattern to the right family without naming a product. Used next in → 62.
References
Kleppmann and Riccomini, Designing Data-Intensive Applications — see
references.md.