๐ System Design Interview Preparation โ Day 3
Topic: Databases, SQL vs NoSQL, Replication & Sharding
Day 2 was about scaling the application layer.
Today we move to one of the most important system-design topics:
What happens when the database becomes the bottleneck?
This is extremely important for .NET interviews because you'll often be asked to explain why you chose SQL Server vs NoSQL, how to handle millions of reads/writes, and how to scale a database.
1. Why is Database Design Important?
Consider our architecture from Day 2:
Load Balancer
/ | \
โผ โผ โผ
API1 API2 API3
\ | /
\ | /
โผ โผ โผ
DatabaseWe can easily add API servers:
API1
API2
API3
API4
API5
...But eventually all of them are hitting:
โโโโโโโโโโโโโโ
โ Database โ
โโโโโโโโโโโโโโThe database can become the bottleneck.
For example:
Application traffic:
100,000 requests/sec
Database:
Can handle only 20,000 operations/secAdding more API servers won't solve the problem.
This is a critical system-design principle:
Scaling one layer doesn't help if another layer has become the bottleneck.
2. SQL vs NoSQL
One of the most common interview questions:
"When would you choose SQL vs NoSQL?"
SQL Databases
Examples:
- SQL Server
- PostgreSQL
- MySQL
- Oracle
They typically provide:
- Structured schemas
- Relationships
- SQL queries
- Transactions
- Strong consistency options
- Constraints
Example:
Customer
----------------
CustomerId
Name
Email
Order
----------------
OrderId
CustomerId
OrderDate
AmountRelationship:
Customer 1 โโโโโโโโ * OrdersThis is a natural fit for systems where relationships and transactional correctness are important.
Examples:
Banking
Payments
Orders
Inventory
Healthcare records3. NoSQL Databases
NoSQL is a broad category of non-relational databases.
Examples include:
- Document databases
- Key-value databases
- Wide-column databases
- Graph databases
Examples of technologies:
MongoDB โ Document
Redis โ Key-value
Cassandra โ Wide-column
Neo4j โ GraphThey can be useful when you need:
- Very large scale
- Flexible schemas
- High write/read throughput
- Distributed storage
- Specific access patterns
4. SQL vs NoSQL โ Interview Comparison
| SQL | NoSQL |
|---|---|
| Structured schema | Often flexible schema |
| Relationships | Often denormalized |
| SQL | Database-specific APIs/query languages |
| Strong transactional support | Depends on technology |
| Joins commonly supported | Often avoided |
| Vertical scaling historically common | Horizontal scaling often emphasized |
| Excellent for relational data | Excellent for specific distributed access patterns |
Important interview point
Don't say:
"NoSQL is faster than SQL."
That's too simplistic.
Instead say:
"The choice depends on the access patterns, consistency requirements, data relationships, scale, and operational requirements."
That's a much better system-design answer.
5. When Would You Choose SQL?
Suppose we're designing a banking system.
We have:
Account
Transaction
Customer
PaymentThere are strong relationships between these entities.
We may need operations like:
BEGIN TRANSACTION;
UPDATE Accounts
SET Balance = Balance - 1000
WHERE AccountId = 1;
UPDATE Accounts
SET Balance = Balance + 1000
WHERE AccountId = 2;
COMMIT;We need transactional correctness.
A relational database is a strong candidate.
6. When Would You Choose NoSQL?
Suppose we're building a massive event-tracking system:
User clicks
Page views
Device events
Application eventsPotentially:
Billions of eventsIf the primary access pattern is something like:
userId โ eventsa distributed NoSQL store may be a better fit depending on the required queries, consistency, retention and scale.
The important thing is:
Start with access patterns and requirements, not with the database technology.
7. Database Indexing
This is a concept you already know from SQL interview preparation, but it's also critical in system design.
Suppose we have:
SELECT *
FROM Users
WHERE Email = 'ramesh@example.com';If there is no index on Email, the database may need to examine a large portion of the table.
With an appropriate index:
Email
โ
Index
โ
Matching rowThe database can find the record much more efficiently.
8. But Indexes Aren't Free
This is an important interview point.
Indexes improve reads but have costs.
Suppose:
Table
100 million rowsand we create:
Index 1
Index 2
Index 3
Index 4
Index 5Now writes can become more expensive because indexes need to be maintained.
Indexes also consume storage.
Therefore:
Don't blindly index every column.
Design indexes based on actual query patterns.
9. Database Replication
Suppose we have one database:
API Servers
โ
โผ
โโโโโโโโโโโโ
โ Primary โ
โโโโโโโโโโโโIf the database fails:
Database โthe application can become unavailable.
Replication creates additional copies.
โโโโโโโโโโโโ
โ Primary โ
โโโโโโฌโโโโโโ
โ
โโโโโโโโโดโโโโโโโโ
โผ โผ
Replica 1 Replica 2Now we have redundancy.
10. Primary and Read Replicas
A common architecture is:
API
โ
โโโโโโโโโดโโโโโโโโโ
โ โ
Writes Reads
โ โ
โผ โผ
Primary Read ReplicaFor example:
POST /orders
โ
Primary DBwhile:
GET /products
โ
Read ReplicaThis is useful when the application is read-heavy.
11. Read Replicas
Suppose:
10,000 reads/sec
1,000 writes/secWe could use:
Application
/ \
Writes Reads
โ โ
Primary โโโโโโโโโโโโ
โ Replica 1โ
โโโโโโโโโโโโ
โโโโโโโโโโโโ
โ Replica 2โ
โโโโโโโโโโโโReads can be distributed across replicas.
This reduces the read workload on the primary database.
12. Replication Lag
Here's an important problem.
Suppose:
Client
โ
Primary
โ
Write completedBut the replica hasn't received the update yet.
Then:
Read Replica
โ
Old dataThis is called replication lag.
Example:
T1:
User updates profile
Primary:
Name = "Ramesh"
T2:
Immediately reads from replica
Replica:
Name = old valueThis creates a consistency issue.
13. Strong vs Eventual Consistency
Strong consistency
After a successful write, subsequent reads return the latest committed value according to the system's consistency guarantees.
Example:
Write:
Balance = โน10,000
Immediately read:
โน10,000This is useful for things like:
- Payments
- Account balances
- Critical inventory operations
Eventual consistency
Updates may take some time to propagate, but eventually replicas converge.
Example:
Primary:
New profile picture
Replica:
Old picture
โ
Propagation
Replica:
New pictureThis may be acceptable for:
- Social media feeds
- Analytics
- View counters
- Recommendations
depending on the product requirements.
14. Database Partitioning
Partitioning means dividing data into smaller logical pieces.
For example:
Orderscould be partitioned by:
YearOrders
โโโ 2024
โโโ 2025
โโโ 2026
โโโ 2027Or by another suitable partition key.
Partitioning can improve:
- Query performance
- Data management
- Maintenance
- Storage management
15. Sharding
Sharding is a form of horizontal data distribution.
Instead of putting all data on one database:
Database
โโโโโโโโโโโโโโโโโ
โ All Customers โ
โโโโโโโโโโโโโโโโโwe distribute it:
Customer Data
โ
โโโโโโโโโโโผโโโโโโโโโโ
โผ โผ โผ
Shard 1 Shard 2 Shard 3For example, by customer ID:
CustomerId 1โ1M
โ
Shard 1
CustomerId 1Mโ2M
โ
Shard 2
CustomerId 2Mโ3M
โ
Shard 3Each shard stores only part of the data.
16. Why Do We Need Sharding?
Imagine:
10 billion usersTrying to store and serve all data from one database may become impractical.
Sharding allows:
10 billion users
โ
Shard 1 โ 2B
Shard 2 โ 2B
Shard 3 โ 2B
Shard 4 โ 2B
Shard 5 โ 2BThe system can distribute storage and workload.
17. Choosing a Shard Key
This is a very common interview follow-up.
Suppose we're sharding users.
Possible shard keys:
userId
country
email
organizationIdA good shard key should ideally:
- Distribute data reasonably evenly
- Distribute traffic
- Support common queries
- Avoid hotspots
- Be stable
Bad example
Suppose 80% of users are from one country:
India โ 80%
US โ 10%
UK โ 5%
Others โ 5%Sharding purely by country could create:
India shard โ overloaded
Other shards โ underutilizedThis is a hotspot / skew problem.
18. Sharding vs Replication
Very important.
Replication
Creates copies of the same data.
Same Data
/ | \
โผ โผ โผ
Primary Replica ReplicaPurpose:
- Availability
- Read scaling
- Disaster recovery
Sharding
Splits different data across machines.
All Data
โ
โโโโโโโผโโโโโโ
โผ โผ โผ
Shard1 Shard2 Shard3Purpose:
- Scale storage
- Scale writes
- Scale workload
Easy memory trick
Replication = Copies
Sharding = Splitting
19. Database Bottleneck Example
Imagine:
100,000 requests/secYour architecture:
Load Balancer
/ | \
โผ โผ โผ
API1 API2 API3
\ | /
\ | /
โผ โผ โผ
SQL ServerYou add:
API4
API5
API6
API7But all of them hit the same database.
Database utilization becomes:
CPU = 100%
I/O = 100%
Connections = exhaustedAdding more API servers doesn't fix it.
Possible solutions depend on the bottleneck:
Database Bottleneck
โ
โโโ Optimize queries
โโโ Add indexes
โโโ Caching
โโโ Read replicas
โโโ Partitioning
โโโ Sharding
โโโ Change storage modelYou should always identify the actual bottleneck before choosing a solution.
20. Caching as a Database Scaling Strategy
Suppose we have:
GET /products/100and Product 100 is requested 1 million times.
Without cache:
1 million requests
โ
DatabaseWith cache:
1 million requests
โ
Redis
โ
โโโ 999,000 cache hits
โ
โโโ 1,000 database requestsThe exact numbers are illustrative, but the principle is important.
Caching can dramatically reduce database load.
We'll dedicate a full day to Redis and caching strategies later.
21. SQL Server Example for Your .NET Background
Suppose your ASP.NET Core application uses:
ASP.NET Core
โ
Entity Framework Core
โ
SQL ServerAt small scale:
API
โ
SQL ServerAt larger scale, we might introduce:
Load Balancer
/ | \
โผ โผ โผ
API1 API2 API3
\ | /
Redis
โ
โผ
SQL ServerThen as read traffic grows:
SQL
โโโโโดโโโโ
โผ โผ
Primary Replica
โ โ
Writes ReadsAnd at very large scale, depending on requirements:
Data
โ
โโโโโโโโโผโโโโโโโโ
โผ โผ โผ
Shard 1 Shard 2 Shard 3Notice how we don't jump directly to sharding.
We progressively address bottlenecks.
๐ฏ Day 3 Interview Questions
Try these yourself before looking at the answers.
Q1.
Why can the database become a bottleneck even if the application layer is horizontally scaled?
Q2.
What are the main differences between SQL and NoSQL databases?
Q3.
When would you choose SQL Server over a NoSQL database?
Q4.
What is database indexing? Why does it improve query performance?
Q5.
What are the disadvantages or costs of adding too many indexes?
Q6.
What is database replication? Why is it useful?
Q7.
What is a read replica? When would you use one?
Q8.
What is replication lag and how can it affect an application?
Q9.
What is the difference between replication and sharding?
Q10.
What is a shard key? What characteristics make a good shard key?
๐ฅ Bonus Scenario
Imagine you're designing a large e-commerce application.
You have:
100 million users
10 million orders/day
100,000 requests/secCurrent architecture:
Load Balancer
/ | \
โผ โผ โผ
API1 API2 API3
\ | /
\ | /
SQL ServerThe interviewer says:
"The SQL Server is now at 95โ100% CPU. What would you do?"
Don't immediately answer:
"Shard the database."
Think in stages:
Database CPU High
โ
Check query performance
โ
Check indexes
โ
Check connection/query patterns
โ
Cache frequently accessed data
โ
Separate read traffic
โ
Read replicas
โ
Partitioning if appropriate
โ
Sharding if scale genuinely requires itThis progressive approach is exactly what interviewers want to see.
๐ง Day 3 Cheat Sheet
SQL
โ
Structured + relational + transactions
NoSQL
โ
Distributed/flexible/specialized access patterns
Index
โ
Faster reads
โ
But additional storage + write/maintenance cost
Replication
โ
Copies of data
Read Replica
โ
Scale reads
Replication Lag
โ
Replica may temporarily have older data
Sharding
โ
Split data across machines
Shard Key
โ
Determines where data goesโญ Most important distinction
Replication = "Give me another copy."
Sharding = "Split the data between machines."And one interview rule to remember:
Don't introduce sharding, microservices, Kafka, or other complex technologies just because the system is "large." First identify the actual bottleneck and choose the simplest architecture that satisfies the requirements.