When to Choose SQL
SQL databases excel at structured data with complex relationships.
SQL Strengths
1. ACID Transactions
- Financial data
- Inventory management
- Order processing
2. Complex Queries
- Multi-table JOINs
- Aggregations
- Window functions
- Subqueries
3. Data Integrity
- Constraints (UNIQUE, FOREIGN KEY)
- Schema enforcement
- Data validation
4. Mature Ecosystem
- Tooling
- Backup/restore
- Monitoring
- Community support
SQL Use Cases
| Use Case | Why SQL |
|---|---|
| Banking | ACID for transactions |
| E-commerce | Complex product relationships |
| User management | Structured user data |
| Content management | Structured content |
| Analytics | Complex queries |
| Inventory | Data integrity |
SQL Decision Criteria
Choose SQL when:
✓ Data is structured and tabular
✓ Relationships are important
✓ ACID transactions required
✓ Complex queries needed
✓ Schema is stable
✓ Data integrity critical
✓ Team knows SQL
Examples:
- Banking system
- E-commerce platform
- HR system
- Patient records
SQL Anti-Patterns
Avoid SQL when:
✗ Schema changes frequently
✗ Data is unstructured/semi-structured
✗ Write throughput very high
✗ Need extreme horizontal scaling
✗ Simple key-value access pattern
SQL Scaling Considerations
SQL Scaling Path:
1. Vertical scaling (first)
- Upgrade instance
- Add RAM, CPU
2. Read replicas
- Distribute reads
- Async replication
3. Connection pooling
- Reuse connections
- Reduce overhead
4. Sharding (last resort)
- Complex but scalable
- Distributed transactions
When to Choose NoSQL
NoSQL databases optimize for specific workloads and data models.
NoSQL Strengths
1. Horizontal Scaling
- Add nodes linearly
- No vertical limits
2. Flexible Schema
- Dynamic fields
- Schema-less or schema-on-read
3. High Throughput
- Optimized for specific access patterns
- Distributed architecture
4. High Availability
- No single point of failure
- Multi-datacenter replication
NoSQL Use Cases
| Use Case | NoSQL Type |
|---|---|
| Session storage | Key-value (Redis) |
| User profiles | Document (MongoDB) |
| Time-series data | Column-family (Cassandra) |
| Social network | Graph (Neo4j) |
| Caching | Key-value (Redis) |
| Product catalog | Document (MongoDB) |
| IoT data | Column-family (Cassandra) |
NoSQL Decision Criteria
Choose NoSQL when:
✓ Data is semi-structured or unstructured
✓ Schema changes frequently
✓ High write throughput needed
✓ Horizontal scaling required
✓ Simple access patterns
✓ Eventual consistency acceptable
✓ Need specific data model
Examples:
- Social media feed
- IoT sensor data
- Real-time analytics
- Gaming leaderboards
NoSQL Tradeoffs
Tradeoffs:
- No complex JOINs
- Weaker consistency (eventual)
- Limited query flexibility
- Data duplication (denormalization)
- Newer technology (less mature tooling)
NoSQL Scaling Path
NoSQL Scaling:
1. Horizontal scaling (built-in)
- Add nodes
- Auto-rebalance
2. Replication
- Multiple copies
- High availability
3. Partitioning
- Data distribution
- Linear scaling
NoSQL Anti-Patterns
Avoid NoSQL when:
✗ Data is highly relational
✗ Complex queries required
✗ Strong ACID needed
✗ Data consistency critical
✗ Team lacks NoSQL experience
Decision Framework
Step 1: Data Model
- Structured → SQL
- Semi-structured → Document
- Key-value → Key-Value
- Relationships → Graph
Step 2: Consistency
- Strong → SQL
- Eventual → NoSQL
Step 3: Scale
- Vertical → SQL
- Horizontal → NoSQL
Step 4: Query Complexity
- Complex → SQL
- Simple → NoSQL
Hybrid Approaches
Many systems use both SQL and NoSQL together.
Polyglot Persistence
Using multiple database types for different needs:
System: E-commerce Platform
- User data → PostgreSQL (ACID, relationships)
- Product catalog → MongoDB (flexible schema)
- Sessions → Redis (fast access)
- Search → Elasticsearch (full-text search)
- Recommendations → Neo4j (graph relationships)
Hybrid Architecture
┌─────────────────────────────────────────┐
│ Application │
└──────────────┬──────────────────────────┘
│
┌───────┼───────┬──────────┐
│ │ │ │
┌───▼──┐ ┌──▼───┐ ┌─▼──┐ ┌───▼──┐
│PostgreSQL│MongoDB│Redis│ │Neo4j │
│(users) │(products)│(sessions)│(social)│
└────────┘└────────┘└────┘ └──────┘
Data Synchronization
Challenge: Keeping data consistent across databases
Solutions:
1. CDC (Change Data Capture)
- PostgreSQL → Kafka → MongoDB
2. Dual Writes
- Application writes to both databases
3. Event Sourcing
- Events stored, projections to different DBs
NewSQL
NewSQL: SQL + NoSQL benefits
Examples:
- CockroachDB: Distributed SQL
- Google Spanner: Global SQL
- TiDB: MySQL-compatible distributed
Features:
- ACID transactions
- Horizontal scaling
- SQL interface
- Distributed architecture
Hybrid Best Practices
- Choose the right tool for each job: Don't force one database
- Manage data synchronization: Use CDC or event sourcing
- Monitor all databases: Unified observability
- Team expertise: Consider what your team knows
- Start simple: Add NoSQL only when needed
Decision Checklist
For each component:
□ What is the data model?
□ What are the consistency requirements?
□ What is the scale (reads/writes)?
□ What are the query patterns?
□ What is the team's expertise?
□ What is the operational overhead?
Practice Problems
Design a scalable SQL vs NoSQL system. Cover high-level architecture, data model, and API design.
Solution
// Complete system design:
// - Functional + Non-functional requirements
// - Capacity estimation
// - Data model (SQL/NoSQL choice)
// - API endpoints
// - Component architecture
// - Scaling strategy
// - Monitoring & reliabilityHow would you scale SQL vs NoSQL to handle 10x the current load? Identify bottlenecks and solutions.
Solution
// Scaling approach:
// 1. Load balancing
// 2. Database sharding/replication
// 3. Cache layer (Redis)
// 4. CDN for static assets
// 5. Async processing (queues)
// 6. Microservices decompositionAnalyze potential failure modes for SQL vs NoSQL and design mitigation strategies.
Solution
// Failure mitigation:
// 1. Redundancy (multi-AZ)
// 2. Circuit breakers
// 3. Retry with backoff
// 4. Dead letter queues
// 5. Health checks
// 6. Graceful degradationQuiz
1. When should you choose SQL over NoSQL?
2. What is polyglot persistence?
3. What is a NewSQL database?
4. What is the main tradeoff of NoSQL?
Flashcards
Question
When should you choose SQL?
Click to reveal answer
Answer
Structured data, ACID transactions, complex queries (JOINs), data integrity critical, stable schema. Examples: banking, e-commerce, inventory.
Question
When should you choose NoSQL?
Click to reveal answer
Answer
Semi-structured data, high write throughput, horizontal scaling, flexible schema, eventual consistency OK. Examples: social media, IoT, caching.
Question
What is polyglot persistence?
Click to reveal answer
Answer
Using multiple database types optimized for different needs: PostgreSQL for users, MongoDB for products, Redis for sessions, Neo4j for social graph.
Question
What are NewSQL databases?
Click to reveal answer
Answer
Databases combining SQL features (ACID, JOINs) with NoSQL benefits (horizontal scaling). Examples: CockroachDB, Google Spanner, TiDB.
Question
What is SQL vs NoSQL?
Click to reveal answer
Answer
SQL vs NoSQL is a key concept in system design.
Revision Notes
Key Takeaways
- 1.SQL for structured data with complex queries and ACID requirements
- 2.NoSQL for flexible schemas, high throughput, and horizontal scaling
- 3.Polyglot persistence uses multiple databases for different needs
- 4.NewSQL combines SQL features with NoSQL scalability
- 5.Choose based on data model, consistency, scale, and query patterns
Interview Tips
- •Justify SQL vs NoSQL choice based on requirements
- •Discuss hybrid approaches when appropriate
- •Consider team expertise and operational overhead
- •Mention specific databases for your use case
Cheat Sheet
SQL vs NoSQL - Cheat Sheet
Choose SQL When:
- Structured, tabular data
- ACID transactions needed
- Complex queries (JOINs)
- Data integrity critical
- Schema stable
Choose NoSQL When:
- Semi-structured data
- High write throughput
- Horizontal scaling required
- Flexible schema
- Eventual consistency OK
NoSQL Types:
| Type | Use Case |
|---|---|
| Document | CMS, profiles |
| Key-Value | Sessions, cache |
| Column-Family | Time-series, IoT |
| Graph | Social, recommendations |
Hybrid:
- Polyglot persistence
- Right tool for each job
- NewSQL: SQL + NoSQL benefits