Choosing the Right Database — SQL, NoSQL, and Everything Between
There is no best database#
"Should I use PostgreSQL or MongoDB?" is the wrong question. The right question is: "What are my data access patterns?"
Every database makes trade-offs. The one that's perfect for your use case might be terrible for someone else's. Here's how to choose.
The categories#
Relational (SQL)#
PostgreSQL, MySQL, SQLite
Structured data with relationships. Tables, rows, columns, JOINs, ACID transactions.
Choose when:
- Data has clear relationships (users → orders → products)
- You need complex queries with JOINs and aggregations
- ACID transactions are required (financial data)
- Schema is known and relatively stable
Skip when: Schema changes frequently, data is deeply nested, or you need horizontal scaling beyond a single node.
Document (NoSQL)#
MongoDB, CouchDB, Firestore
JSON-like documents with flexible schemas. Each document can have different fields.
Choose when:
- Schema varies between records (CMS content, user profiles with optional fields)
- Data is naturally nested (blog post with embedded comments)
- Rapid prototyping where schema isn't finalized
- Horizontal scaling is a primary concern
Skip when: You need complex JOINs, strong transactions across documents, or strict schema enforcement.
Key-Value#
Redis, Memcached, DynamoDB
Simple: put a key, get a value. Fastest possible reads.
Choose when:
- Session storage, caching, feature flags
- Real-time leaderboards (Redis sorted sets)
- Shopping carts, rate limiters
- You need sub-millisecond reads
Skip when: You need queries beyond key lookup, complex relationships, or ad-hoc reporting.
Wide-Column#
Cassandra, ScyllaDB, HBase
Optimized for massive write throughput and time-series data.
Choose when:
- IoT sensor data, event logging, time-series metrics
- Write-heavy workloads (millions of writes/second)
- Multi-region replication with tunable consistency
Skip when: Read-heavy with complex queries, small datasets, or need for strong consistency.
Graph#
Neo4j, Amazon Neptune, Dgraph
Nodes and edges. Optimized for traversing relationships.
Choose when:
- Social networks (friends of friends)
- Recommendation engines (users who bought X also bought Y)
- Fraud detection (finding suspicious transaction patterns)
- Knowledge graphs
Skip when: Data doesn't have meaningful relationships, simple CRUD operations.
Search#
Elasticsearch, Meilisearch, Typesense
Full-text search with relevance ranking, facets, and autocomplete.
Choose when:
- Product search, log analysis, content discovery
- Autocomplete and fuzzy matching
- Faceted navigation (filter by price, category, rating)
Skip when: Primary data store (use as secondary index). Not designed for transactions.
The decision framework#
| Data pattern | Best fit |
|---|---|
| Structured with relationships | PostgreSQL |
| Flexible documents | MongoDB / Firestore |
| Simple key lookups, caching | Redis |
| Massive writes, time-series | Cassandra |
| Relationship traversal | Neo4j |
| Full-text search | Elasticsearch |
| Serverless, auto-scaling | DynamoDB / Firestore |
Most apps need 2-3 databases#
A typical production system might use:
- PostgreSQL — primary data (users, orders, products)
- Redis — caching, sessions, rate limiting
- Elasticsearch — search and analytics
This isn't overengineering — each database handles what it's best at.
Start with PostgreSQL#
If you're unsure, start with PostgreSQL. It handles 90% of use cases well:
- JSON columns for semi-structured data
- Full-text search for basic search needs
- PostGIS for geospatial queries
- Great performance up to hundreds of millions of rows
Add specialized databases when PostgreSQL becomes the bottleneck for a specific access pattern — not before.
See databases in your architecture#
On Codelit, every generated architecture includes typed database components. Click any database node to audit its scaling characteristics and get recommendations for your specific workload pattern.
Explore database choices: describe your system on Codelit.io and audit each database component for the right fit.
Explore the Uber architecture interactively
Try it →Try these templates
Netflix Video Streaming Architecture
Global video streaming platform with adaptive bitrate, CDN distribution, and recommendation engine.
10 componentsSearch Engine Architecture
Web-scale search with crawling, indexing, ranking, and sub-second query serving.
8 componentsGoogle Search Engine Architecture
Web-scale search with crawling, indexing, PageRank, query processing, ads, and knowledge graph.
10 componentsContinue learning
Go deeper on data and storage
As an Amazon Associate I earn from qualifying purchases. Codelit may receive a commission at no extra cost to you.
Fundamentals of Data Engineering
Joe Reis, Matt Housley · 2022
Covers the pipeline lifecycle from ingestion to serving, with trade-off tables for each stage.
4.7 (897)#3 in Data MiningDatabase Internals
Alex Petrov · 2019
Explains B-trees, LSM trees and consensus protocols so you can predict how a datastore will behave.
4.6 (560)#2 in Management Information SystemsFundamentals of Software Architecture
Mark Richards, Neal Ford · 2025
Scores eight architecture styles against the same characteristics, so trade-offs are side by side.
Enterprise Integration Patterns
Gregor Hohpe, Bobby Woolf · 2003
A catalog of 65 messaging patterns with diagrams - the vocabulary queue designs still use today.
4.6 (581)