21. How do you pick between relational vs document database?
Not a religious choice — match the data model to the access pattern.
Choose relational (PostgreSQL, MySQL) when:
Data has complex relationships requiring joins across entities
ACID transactions across multiple entities are required
Schema is well-defined and stable
Ad-hoc querying flexibility is needed
You need strong consistency guarantees
Choose document (MongoDB, DynamoDB, Firestore) when:
Data is naturally hierarchical and consumed as a whole unit (e.g., a user profile with embedded addresses)
Schema is variable or evolves frequently (sparse attributes)
Access pattern is well-defined and key-based (no complex joins)
Horizontal sharding is a primary requirement from day one
Write throughput at scale exceeds what a single relational node can handle
Practical guidance: Start with PostgreSQL. It handles documents (JSONB), time series, full-text search, and graph queries better than many think. Only move to a specialised document store when you have a concrete scaling or data model requirement that PostgreSQL genuinely can't serve.