SQL vs NoSQL Decision Framework
SQL excels at structured relational data with ACID guarantees; NoSQL trades some of that for flexibility and horizontal scale.
How do you choose between SQL and NoSQL databases?
Choosing between SQL (relational) and NoSQL databases is one of the most fundamental architectural decisions. There is no universally correct answer - the right choice depends on your data model, access patterns, scale requirements, and consistency needs.
Relational (SQL) Databases
Core strengths:
- Structured schema with tables, rows, foreign keys, constraints
- ACID transactions (Atomicity, Consistency, Isolation, Durability)
- Powerful JOIN queries across related tables
- Decades of tooling, expertise, and query optimisation
Examples: PostgreSQL, MySQL, Amazon Aurora, Google Cloud SQL, SQL Server
Use when:
- Complex relationships between entities (users -> orders -> products -> inventory)
- Financial data requiring strict consistency and auditability
- Reporting and analytics with ad-hoc queries
- When schema is well-defined and changes slowly
- When you need transactions across multiple entities
NoSQL Database Types
Document Stores
Store schema-flexible JSON/BSON documents. Nested structures map naturally to application objects.
- Examples: MongoDB, DynamoDB (document mode), Firestore, CouchDB
- Use when: content management, product catalogues, user profiles with varying attributes, rapid schema iteration
Key-Value Stores
Extremely fast O(1) reads/writes by key. Limited query capability.
- Examples: Redis, DynamoDB (key-value mode), Memcached, Riak
- Use when: caching, session storage, leaderboards, feature flags, rate limiting counters
Wide-Column Stores
Rows with dynamic columns; optimised for write-heavy time-series and sparse data.
- Examples: Apache Cassandra, HBase, Google Bigtable
- Use when: time-series data, IoT sensor data, audit logs, write-heavy workloads at massive scale
Graph Databases
Nodes and edges; efficient traversal of relationships.
- Examples: Neo4j, Amazon Neptune, Dgraph
- Use when: social networks, recommendation engines, fraud detection, knowledge graphs
The Tradeoffs
| Dimension | SQL | NoSQL |
|---|---|---|
| Schema | Fixed, enforced | Flexible, per-document |
| Transactions | Multi-row ACID | Usually single-doc or eventual |
| Join queries | Native, efficient | Application-side or denormalise |
| Horizontal scale | Harder (sharding is complex) | Built for scale-out |
| Consistency | Strong (ACID) | Usually eventual |
| Query flexibility | High (SQL) | Limited (by access pattern) |
Polyglot Persistence
Modern systems often use multiple database types for different parts of the system:
- PostgreSQL for transactional user/order data
- Redis for session cache and rate limiting
- Cassandra for time-series event log
- Elasticsearch for full-text search
- S3 for object storage
This is called polyglot persistence - use the right tool for each data type.
Common Mistakes
- Choosing NoSQL for its "web scale" without needing scale (complexity without benefit)
- Using a document store for highly relational data and struggling with application-side joins
- Assuming SQL can't scale horizontally (PostgreSQL scales very far with read replicas + sharding)
Interview Angle
Force yourself to articulate why, not just what. "I'd use PostgreSQL for user accounts and orders because they have complex relationships and require ACID transactions. I'd use Redis for session storage because it's key-value with TTL and doesn't need persistence. I'd use Cassandra for the activity log because it's append-only, time-series, and needs to handle millions of writes per second."