Choosing The Right Database
Match access patterns and business constraints to the right datastore.
Curriculum
System Design Foundations
Scalability Basics
Database Design
Choosing The Right Database — Beginner Friendly System Design Guide
One Database Cannot Solve Everything
A very common beginner mistake is thinking:
"There is one best database."
Wrong.
Different applications have:
- different traffic patterns
- different data structures
- different scaling needs
- different consistency requirements
Choosing the wrong database can:
- slow down product
- increase costs
- create scaling problems
- limit future growth
Real Example — Swiggy
Swiggy handles:
- restaurant data
- live delivery tracking
- payments
- search
- recommendations
Using ONE database for everything would be inefficient.
Different problems need different databases.
What Does A Database Choice Depend On?
Database selection depends on:
| Factor | Meaning |
|---|---|
| Traffic | How many users/requests |
| Data structure | Structured or flexible |
| Consistency needs | Banking vs social media |
| Query patterns | Search-heavy or write-heavy |
| Scale | Millions vs billions of rows |
| Latency | Real-time or not |
| Availability | Downtime tolerance |
Real-Life Analogy
Imagine storage in real life.
Example
| Data Type | Better Storage |
|---|---|
| Clothes | Wardrobe |
| Rice/Wheat | Containers |
| Books | Bookshelf |
| Money | Bank locker |
You don't store everything in same place.
Databases work similarly.
Main Types Of Databases
Two major categories:
1. SQL Databases
2. NoSQL Databases
What Is SQL Database?
SQL databases store:
- structured data
- rows and columns
like Excel sheets.
Example Table
| user_id | name | city |
|---|---|---|
| 1 | Rahul | Jaipur |
| 2 | Priya | Delhi |
Popular SQL Databases
| Database | Common Usage |
|---|---|
| PostgreSQL | SaaS apps |
| MySQL | Websites |
| Oracle | Banking |
| SQL Server | Enterprises |
SQL Database Flow
Application
│
▼
SQL Query
│
▼
SQL Database
│
▼
Structured Result Returned
Best Use Cases For SQL
SQL works best when:
- data structure is fixed
- consistency is critical
- relationships are important
Examples
| Product Type | Why SQL Works |
|---|---|
| Banking | Strong consistency |
| Ecommerce orders | Transactions |
| Accounting | Accuracy required |
| ERP systems | Structured data |
Real Example — PhonePe
PhonePe payment system requires:
- strong consistency
- reliable transactions
- accurate balances
SQL databases are ideal because:
- money cannot disappear
- duplicate deductions must be prevented
What Is NoSQL Database?
NoSQL databases are more flexible.
Data structure can vary.
Example Documents
{
"name": "iPhone",
"price": 80000
}
Another product may contain:
{
"name": "T-Shirt",
"size": "XL",
"color": "Black"
}
Flexible schema.
Popular NoSQL Databases
| Database | Usage |
|---|---|
| MongoDB | Flexible applications |
| Cassandra | Massive scale |
| DynamoDB | AWS cloud apps |
| Redis | Caching |
| Elasticsearch | Search systems |
NoSQL Database Flow
Application
│
▼
Document / Key Query
│
▼
NoSQL Database
│
▼
Flexible Data Returned
Best Use Cases For NoSQL
NoSQL works best when:
- schema changes frequently
- huge scalability needed
- write traffic is massive
- distributed systems involved
Examples
| Product Type | Why NoSQL Works |
|---|---|
| Social media | Flexible content |
| Chat systems | Huge write volume |
| Analytics | Massive scale |
| Recommendation systems | Fast distributed reads |
Real Example — Instagram
Instagram stores:
- posts
- comments
- likes
- reels
- stories
Data structure changes often.
NoSQL helps with:
- flexibility
- massive scale
SQL vs NoSQL Comparison
| SQL | NoSQL |
|---|---|
| Structured schema | Flexible schema |
| Tables | Documents / Key-value |
| Strong consistency | High scalability |
| Complex joins supported | Faster distributed scaling |
| Best for transactions | Best for huge traffic |
Choosing Database Based On Access Patterns
One of the biggest system design concepts:
Choose database based on access patterns
Not hype.
Not trends.
What Are Access Patterns?
Access pattern means:
How application reads/writes data
Example
Suppose app mostly:
- reads data
Then:
- caching important
Suppose app mostly:
- writes huge event streams
Then:
- distributed NoSQL may work better
Real Example — Blinkit
Blinkit inventory system needs:
- extremely fast updates
- low latency
- real-time stock management
Redis often helps because:
- memory-based
- extremely fast
Example Database Decisions
| Use Case | Better Choice |
|---|---|
| Banking | PostgreSQL |
| Chat App | Cassandra |
| Ecommerce | MySQL |
| Caching | Redis |
| Search Engine | Elasticsearch |
| Analytics | ClickHouse |
One Application Often Uses Multiple Databases
Modern systems rarely use:
- one database only
Different workloads use different databases.
Example — Swiggy Architecture
Swiggy Application
│
┌──────┼────────┐
▼ ▼
PostgreSQL Redis
Orders Cache
│
▼
Elasticsearch
Restaurant Search
Why Multiple Databases?
Because:
- one database cannot optimize everything
Example
| Need | Best Tool |
|---|---|
| Transactions | SQL |
| Caching | Redis |
| Search | Elasticsearch |
| Analytics | Column DB |
What Is Database Consistency?
Consistency means:
- all users see correct data
Critical in:
- banking
- payments
- trading
Example — UPI Payment
Suppose:
- ₹500 deducted from sender
Receiver must:
- receive ₹500
System cannot partially fail.
SQL databases help ensure this.
ACID Properties
SQL databases usually support:
ACID
ACID Means
| Property | Meaning |
|---|---|
| Atomicity | All or nothing |
| Consistency | Valid state maintained |
| Isolation | Concurrent transactions safe |
| Durability | Data survives crashes |
Real Example — Zerodha
Stock trading systems require:
- strong consistency
- reliable transactions
- low latency
SQL databases commonly used for:
- orders
- balances
- positions
What Is Scalability Trade-Off?
As systems grow:
- strong consistency becomes harder
NoSQL databases often trade:
- consistency
for:
- scalability
- availability
Example — WhatsApp
Messages may:
- sync slightly later across devices
This is acceptable.
System prioritizes:
- scale
- availability
Database Selection Mistakes
Mistake 1 — Choosing Trendy Database
Bad approach:
"Everyone uses MongoDB, so I should too."
Wrong.
Choose based on:
- requirements
Mistake 2 — Premature Optimization
Small startup with:
- 100 users
does NOT need:
- Cassandra cluster
Simple PostgreSQL may be enough.
Mistake 3 — Ignoring Future Scale
Choosing database without considering:
- growth
- traffic
- scaling strategy
can create migration pain later.
Real Startup Journey
Most startups begin with:
PostgreSQL or MySQL
Then gradually add:
- Redis
- Elasticsearch
- analytics databases
- queues
as scale grows.
Typical Scalable Architecture
Users
│
▼
Application Server
│
┌────┼────────────┐
▼ ▼
Redis Cache PostgreSQL
│
▼
Elasticsearch
Final Mental Model
Choose database based on:
- data shape
- traffic pattern
- consistency needs
- scaling requirements
Not popularity.
Common Beginner Misconceptions
"NoSQL Is Better Than SQL"
Wrong.
Both solve different problems.
"One Database Is Enough Forever"
Modern systems usually use:
- multiple specialized databases
"Scaling Means Changing Database"
Not always.
Often:
- indexing
- caching
- query optimization
solve problems first.
One-Line Interview Definition
Choosing the right database means selecting storage technology based on access patterns, consistency requirements, scalability needs, latency expectations, and business constraints.
Module context
Choosing the right database, indexing strategies, and the SQL vs NoSQL debate with real production examples from Indian startups.