Q21 · How do you pick between relational vs document database?
Back to 24 Architecture Questions.
The question is not a feature-list comparison. The axis it turns on: which access patterns and invariants you already know, and which queries are still unknown. A database is a bet on what you know at design time: relational bets you know the invariants and want them enforced; document bets you know the access unit and that the shape will drift.
The signal table
| Signal | Points to |
|---|---|
| Invariants are known and load-bearing (balances, references, “every order has items”) | Relational: the database enforces them, in one place |
| Future queries are unknown (reporting, exploration, ad-hoc) | Relational: SQL explores; documents force you to predict access patterns at design time |
| The access unit is one aggregate (fetch the whole order, never half of it) | Document: one fetch, no joins |
| The shape varies per record and drifts (content, catalogs, per-tenant payloads) | Document: shape lives with the record, not in a migration |
| Many-to-many relations dominate the model | Relational: document references turn every join into application code |
| Shape evolution must be explicit and reviewed | Relational: migrations hurt on purpose, and the history is written down |
What the answer should contain
Invariants are the first question. Money, references, and aggregates that must hold: a relational database enforces them at the only place every writer passes through. In a document store, “schema-on-read” really means the schema moves into every reader, with no constraint anywhere: three services, three subtly different assumptions, drift by record.
Unknown queries are the second question. Relational plus SQL is an exploration tool: ask a question you did not anticipate at design time. Document stores require predicting the access pattern up front and denormalizing for it; the first unanticipated query becomes a rebuild. If the domain is young and the questions are still forming, that weighs relational regardless of shape flexibility.
The aggregate test. If the application’s unit of work is “load this whole thing, save this whole thing”, documents fit naturally. If the unit of work is “relate these things across boundaries”, relations fit naturally. Matching the storage unit to the work unit removes a whole class of mapping code.
The hybrid is the honest default. A relational core with JSON columns for the loose shapes, a search engine beside it for the query shapes neither store answers well. Most real systems are polyglot per query class, not per fashion; the modern relational engines make the core-plus-blobs option cover a surprising share of both worlds.
Migrations are part of the price. Relational: explicit, painful, planned in steps (expand, migrate, contract). Document: deferred, and the debt lands in every reader that must now tolerate five shapes of the same record. Deferred schema debt compounds faster than migration pain.
Ideas worth mentioning out loud
- “Choosing a document store ‘to avoid schema design’ does not remove the schema; it removes the place where the schema is written down.”
- Write down the access patterns first. If the list is empty, the choice is not ready to be made, and the safe default is the one that tolerates not knowing.
- The strongest interview move is refusing the binary: name the question class each store answers, then answer per class.
Follow-ups to expect
- Aren’t document transactions enough now? Single-document ones, often; cross-document ones with real isolation, rarely at scale. If your invariants span documents, the answer is sagas of local steps with compensations, which is a much heavier price than a foreign key.
- What about performance? Document fetches of one aggregate are genuinely faster than reassembling joins; it is the cross-aggregate queries where the bill arrives.
- Can I get both? Mostly yes: relational core, JSON columns, and a projection pipeline into whatever index each query class needs.
Red flags in an answer
- “Document, so we do not have to design a schema.”
- One giant JSON column in a relational table: the worst of both, constraints lost and queries blind.
- The choice made before the access patterns were listed.
- “We might need web scale someday”, as a reason for anything today.
Sources
- Data Modeling, MongoDB Manual: the document side’s own guidance, and it supports the axis rather than the fashion: the manual’s first decision is embedded versus referenced, which is the aggregate question this answer poses.
- Designing Data-Intensive Applications, Martin Kleppmann, O’Reilly: chapter 2 compares the models without a vendor’s thumb on the scale, including the observation that schema-on-read means the schema moves into the readers.