Relational vs. Document Data Modeling
Relational databases model data through tables, keys, constraints, and relationships. They are strong when transactions, joins, integrity constraints, and expressive queries matter. Document databases store nested objects and can fit aggregate-shaped records with evolving fields and access patterns centered on the document.
Do not choose a document database merely because the application uses JSON. Model the lifecycle and relationships. Embedding a user's settings with the user may be convenient when read and updated together; embedding millions of independently growing events in one document is usually a poor fit.
Normalize to reduce duplication and preserve integrity, or denormalize deliberately to optimize reads. Denormalization creates update obligations: if a product name is copied into orders, decide whether historical orders should preserve the purchased name or reflect current catalog data.
- Relational strengths:
Constraints, joins, multi-row transactions - Document strengths:
Aggregate-shaped data, flexible schema, nested reads - Decision input:
Access patterns and correctness requirements
- Choose based on access patterns and integrity needs.
- Embedding and normalization are trade-offs, not moral rules.
- Document the source of truth when data is duplicated.
Why is a database unique constraint important when creating usernames?