System Design / Curriculum / Day 3

DAY 3 Phase 2 ยท Storage & Data 16 min read 0 / 10 Mastered

Databases, SQL vs NoSQL, Replication & Sharding

Database scaling bottlenecks, ACID vs BASE, read replicas, replication lag, horizontal sharding strategies, and consistent hashing.

๐Ÿš€ 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:

Architecture Blueprint
                    Load Balancer
                   /      |      \
                  โ–ผ       โ–ผ       โ–ผ
                API1     API2    API3
                  \       |      /
                   \      |     /
                    โ–ผ     โ–ผ    โ–ผ
                     Database

We can easily add API servers:

Architecture Blueprint
API1
API2
API3
API4
API5
...

But eventually all of them are hitting:

Architecture Blueprint
                โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                โ”‚  Database  โ”‚
                โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The database can become the bottleneck.

For example:

Architecture Blueprint
Application traffic:
100,000 requests/sec

Database:
Can handle only 20,000 operations/sec

Adding 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:

Architecture Blueprint
Customer
----------------
CustomerId
Name
Email

Order
----------------
OrderId
CustomerId
OrderDate
Amount

Relationship:

Architecture Blueprint
Customer 1 โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ * Orders

This is a natural fit for systems where relationships and transactional correctness are important.

Examples:

Architecture Blueprint
Banking
Payments
Orders
Inventory
Healthcare records

3. 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:

Architecture Blueprint
MongoDB      โ†’ Document
Redis        โ†’ Key-value
Cassandra    โ†’ Wide-column
Neo4j        โ†’ Graph

They 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

SQLNoSQL
Structured schemaOften flexible schema
RelationshipsOften denormalized
SQLDatabase-specific APIs/query languages
Strong transactional supportDepends on technology
Joins commonly supportedOften avoided
Vertical scaling historically commonHorizontal scaling often emphasized
Excellent for relational dataExcellent 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:

Architecture Blueprint
Account
Transaction
Customer
Payment

There are strong relationships between these entities.

We may need operations like:

sql
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:

Architecture Blueprint
User clicks
Page views
Device events
Application events

Potentially:

Architecture Blueprint
Billions of events

If the primary access pattern is something like:

Architecture Blueprint
userId โ†’ events

a 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:

sql
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:

Architecture Blueprint
Email
  โ†“
Index
  โ†“
Matching row

The 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:

Architecture Blueprint
Table
100 million rows

and we create:

Architecture Blueprint
Index 1
Index 2
Index 3
Index 4
Index 5

Now 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:

Architecture Blueprint
          API Servers
               โ”‚
               โ–ผ
          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
          โ”‚ Primary  โ”‚
          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

If the database fails:

Architecture Blueprint
Database โŒ

the application can become unavailable.

Replication creates additional copies.

Architecture Blueprint
             โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
             โ”‚ Primary  โ”‚
             โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”˜
                  โ”‚
          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
          โ–ผ               โ–ผ
     Replica 1        Replica 2

Now we have redundancy.


10. Primary and Read Replicas

A common architecture is:

Architecture Blueprint
                 API
                  โ”‚
          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
          โ”‚                โ”‚
        Writes            Reads
          โ”‚                โ”‚
          โ–ผ                โ–ผ
       Primary         Read Replica

For example:

Architecture Blueprint
POST /orders
       โ†“
Primary DB

while:

Architecture Blueprint
GET /products
       โ†“
Read Replica

This is useful when the application is read-heavy.


11. Read Replicas

Suppose:

Architecture Blueprint
10,000 reads/sec
1,000 writes/sec

We could use:

Architecture Blueprint
                 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:

Architecture Blueprint
Client
  โ†“
Primary
  โ†“
Write completed

But the replica hasn't received the update yet.

Then:

Architecture Blueprint
Read Replica
     โ†“
Old data

This is called replication lag.

Example:

Architecture Blueprint
T1:
User updates profile

Primary:
Name = "Ramesh"

T2:
Immediately reads from replica

Replica:
Name = old value

This 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:

Architecture Blueprint
Write:
Balance = โ‚น10,000

Immediately read:
โ‚น10,000

This 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:

Architecture Blueprint
Primary:
New profile picture

Replica:
Old picture

     โ†“
Propagation

Replica:
New picture

This 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:

Architecture Blueprint
Orders

could be partitioned by:

Architecture Blueprint
Year
Architecture Blueprint
Orders
โ”œโ”€โ”€ 2024
โ”œโ”€โ”€ 2025
โ”œโ”€โ”€ 2026
โ””โ”€โ”€ 2027

Or 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:

Architecture Blueprint
              Database
        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ”‚ All Customers โ”‚
        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

we distribute it:

Architecture Blueprint
             Customer Data
                  โ”‚
        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ–ผ         โ–ผ         โ–ผ
      Shard 1   Shard 2   Shard 3

For example, by customer ID:

Architecture Blueprint
CustomerId 1โ€“1M
      โ†“
Shard 1

CustomerId 1Mโ€“2M
      โ†“
Shard 2

CustomerId 2Mโ€“3M
      โ†“
Shard 3

Each shard stores only part of the data.


16. Why Do We Need Sharding?

Imagine:

Architecture Blueprint
10 billion users

Trying to store and serve all data from one database may become impractical.

Sharding allows:

Architecture Blueprint
10 billion users

       โ†“

Shard 1 โ†’ 2B
Shard 2 โ†’ 2B
Shard 3 โ†’ 2B
Shard 4 โ†’ 2B
Shard 5 โ†’ 2B

The 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:

Architecture Blueprint
userId
country
email
organizationId

A 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:

Architecture Blueprint
India โ†’ 80%
US    โ†’ 10%
UK    โ†’ 5%
Others โ†’ 5%

Sharding purely by country could create:

Architecture Blueprint
India shard โ†’ overloaded
Other shards โ†’ underutilized

This is a hotspot / skew problem.


18. Sharding vs Replication

Very important.

Replication

Creates copies of the same data.

Architecture Blueprint
             Same Data
          /      |      \
         โ–ผ       โ–ผ       โ–ผ
      Primary Replica Replica

Purpose:

  • Availability
  • Read scaling
  • Disaster recovery

Sharding

Splits different data across machines.

Architecture Blueprint
          All Data
             โ”‚
       โ”Œโ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”
       โ–ผ     โ–ผ     โ–ผ
     Shard1 Shard2 Shard3

Purpose:

  • Scale storage
  • Scale writes
  • Scale workload

Easy memory trick

Replication = Copies

Sharding = Splitting


19. Database Bottleneck Example

Imagine:

Architecture Blueprint
100,000 requests/sec

Your architecture:

Architecture Blueprint
                 Load Balancer
               /      |      \
              โ–ผ       โ–ผ       โ–ผ
            API1     API2     API3
               \      |      /
                \     |     /
                 โ–ผ    โ–ผ    โ–ผ
                  SQL Server

You add:

Architecture Blueprint
API4
API5
API6
API7

But all of them hit the same database.

Database utilization becomes:

Architecture Blueprint
CPU = 100%
I/O = 100%
Connections = exhausted

Adding more API servers doesn't fix it.

Possible solutions depend on the bottleneck:

Architecture Blueprint
Database Bottleneck
        โ”‚
        โ”œโ”€โ”€ Optimize queries
        โ”œโ”€โ”€ Add indexes
        โ”œโ”€โ”€ Caching
        โ”œโ”€โ”€ Read replicas
        โ”œโ”€โ”€ Partitioning
        โ”œโ”€โ”€ Sharding
        โ””โ”€โ”€ Change storage model

You should always identify the actual bottleneck before choosing a solution.


20. Caching as a Database Scaling Strategy

Suppose we have:

Architecture Blueprint
GET /products/100

and Product 100 is requested 1 million times.

Without cache:

Architecture Blueprint
1 million requests
        โ†“
Database

With cache:

Architecture Blueprint
1 million requests
        โ†“
      Redis
        โ”‚
        โ”œโ”€โ”€ 999,000 cache hits
        โ”‚
        โ””โ”€โ”€ 1,000 database requests

The 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:

Architecture Blueprint
ASP.NET Core
      โ†“
Entity Framework Core
      โ†“
SQL Server

At small scale:

Architecture Blueprint
API
 โ†“
SQL Server

At larger scale, we might introduce:

Architecture Blueprint
                Load Balancer
                /     |     \
               โ–ผ      โ–ผ      โ–ผ
             API1    API2    API3
                \     |     /
                  Redis
                    โ”‚
                    โ–ผ
                 SQL Server

Then as read traffic grows:

Architecture Blueprint
                    SQL
                 โ”Œโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”
                 โ–ผ       โ–ผ
              Primary  Replica
                โ†‘        โ†‘
              Writes    Reads

And at very large scale, depending on requirements:

Architecture Blueprint
                    Data
                     โ”‚
             โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
             โ–ผ       โ–ผ       โ–ผ
           Shard 1 Shard 2 Shard 3

Notice 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:

Architecture Blueprint
100 million users
10 million orders/day
100,000 requests/sec

Current architecture:

Architecture Blueprint
                 Load Balancer
                /      |      \
               โ–ผ       โ–ผ       โ–ผ
             API1     API2    API3
                \      |      /
                 \     |     /
                  SQL Server

The 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:

Architecture Blueprint
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 it

This progressive approach is exactly what interviewers want to see.


๐Ÿง  Day 3 Cheat Sheet

Architecture Blueprint
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

Architecture Blueprint
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.

Day 3 Interview Questions & Answers

Real-world verbal answers designed to articulate clearly in high-stakes technical interviews.

Day 3 Flashcard Drill

Practice these 10 questions using 3D interactive flashcards. Click or press Space to flip.

Day 3 Q1 UNMASTERED

Loading question...

Day 3 VERBAL ANSWER

...

Card 1 of 10
Copied to clipboard!