Part 4 of 5 · 2 min read
When each one actually fits
Matching a real access pattern to a data model, instead of picking by trend.
When each one actually fits
Think of choosing a container at a hardware store. A bucket fits loose sand. A rack fits long poles. Using a bucket for the poles, or a rack for the sand, technically works but fights the material the whole time.
In plain words: a relational table fits data with a stable, well-understood structure, where queries need to join across several related facts. A document store fits data whose shape genuinely varies from record to record, and is usually read or written as one whole unit. A key-value store fits simple lookups by a single key, with no need to query by anything else. A graph database fits data where the relationships themselves are the main thing being asked about. A column-family store fits workloads writing or scanning enormous numbers of rows, where reading only a few columns at a time matters more than reading a whole row.
A worked example. The bookstore's customers and orders tables fit relational well, since orders need to join against customers and books reliably, and every order has the exact same shape. The product-review example fits a document store, since reviews vary in shape and are read one at a time. Neither needs a graph database, since nothing about the bookstore so far is primarily about following chains of relationships.
The key lines. No new code; this part is a decision, not a mechanism.
Remember this
NoSQL is not a single alternative to relational. It is four or more genuinely different tools, each suited to a different access pattern. Picking one means matching the access pattern, not following a trend.
For the curious Most real systems use more than one
A real production system very often uses a relational database for its core transactional data, alongside a key-value store for caching and a document store for a specific flexible-shape feature. Level 11's module on picking a data model for a real workload covers this combination, called polyglot persistence, properly.
Quick notes
- NoSQL groups several unrelated data models under one label, defined mainly by what they are not.
- Document stores hold independently shaped records; key-value stores map a key straight to a value.
- Graph databases make relationships first-class, queryable data; column-family stores group data by column for huge write and scan workloads.
- A relational table fits stable, joinable, well-structured data; each NoSQL family fits a specific different access pattern.
- Real systems often combine a relational database with one or more NoSQL stores, rather than replacing it outright.