Chapter 13: SQL vs NoSQL — Choosing the Right Store

You covered this in the DB guide. Here's the system design decision framework:

Decision Matrix

NeedChooseWhy
Complex queries, JOINs, ACIDSQL (PostgreSQL)Relational model, strong consistency
Flexible schema, document-orientedDocument (MongoDB)Schema-less, nested data
Ultra-fast cache, simple lookupsKey-Value (Redis)In-memory, sub-ms latency
Massive write throughputWide-Column (Cassandra)LSM-tree, distributed writes
Relationship traversalGraph (Neo4j)O(1) relationship hops
Full-text searchSearch (Elasticsearch)Inverted index, relevance scoring
Time-series metricsTSDB (InfluxDB, TimescaleDB)Optimized for time-ordered data

Polyglot Persistence

Real systems use MULTIPLE databases: User Service → PostgreSQL (profiles, auth) Session Service → Redis (fast lookups, TTL) Search → Elasticsearch (full-text) Analytics → ClickHouse (OLAP queries) File metadata → MongoDB (flexible schema) Notifications → Kafka (event streaming)
💡 Default Choice

When in doubt: PostgreSQL + Redis. Covers 90% of use cases. Add specialized stores only when you have a specific need PostgreSQL can't handle efficiently.