π System Design Interview Preparation β Day 2
Topic: Scalability, Load Balancing & Stateless Services
Day 1 taught us how to approach a system-design problem.
Today we'll understand one of the most important building blocks of scalable systems:
How do we handle increasing traffic without relying on one server?
This topic comes up constantly in .NET/Azure interviews.
1. Why Do We Need Scalability?
Imagine you have an ASP.NET Core Web API running on one server:
Users
β
βΌ
βββββββββββββββ
β API Server β
ββββββββ¬βββββββ
β
βΌ
DatabaseInitially:
1,000 requests/secMaybe the server handles it comfortably.
But your application grows:
10,000 requests/secNow the server becomes overloaded.
Eventually:
CPU β 100%
Memory β High
Response time β Increasing
Requests β Timing outWe need to scale.
2. Vertical Scaling
We covered this briefly on Day 1.
You make the existing machine more powerful.
Before:
4 CPU
8 GB RAM
β
After:
16 CPU
64 GB RAMAdvantages
- Simple
- Minimal architectural changes
- Easy for smaller systems
Disadvantages
- Hardware limits
- Increasing cost
- Potential single point of failure
- Eventually you cannot keep scaling vertically
3. Horizontal Scaling
Instead of making one server bigger, add more servers.
Users
β
βΌ
Load Balancer
/ | \
βΌ βΌ βΌ
Server1 Server2 Server3If one server handles:
5,000 requests/secthree servers could potentially handle roughly:
15,000 requests/secassuming the workload distributes evenly and the rest of the architecture can support it.
Why is horizontal scaling powerful?
Because you can continue adding instances:
3 servers
β
10 servers
β
50 servers
β
100 serversThis is fundamental to distributed systems.
4. What is a Load Balancer?
A load balancer distributes incoming requests across multiple backend servers.
Without load balancing:
Users
β
βΌ
ServerWith load balancing:
Users
β
βΌ
Load Balancer
/ | \
βΌ βΌ βΌ
Server1 Server2 Server3The load balancer acts as the entry point for backend servers.
5. Why Do We Need a Load Balancer?
There are several reasons.
1. Distribute traffic
Instead of sending every request to one server:
100% β Server 1we can distribute:
33% β Server 1
33% β Server 2
34% β Server 32. Scalability
If traffic increases:
Before:
LB
βββ Server 1
βββ Server 2We can add:
LB
βββ Server 1
βββ Server 2
βββ Server 3
βββ Server 4
βββ Server 53. Fault tolerance
Suppose:
Server 2 βThe load balancer can stop sending traffic to that unhealthy server.
Load Balancer
/ | \
βΌ X βΌ
Server 1 Server 2 Server 3
β
β β
Users can continue using the system.
6. Types of Load Balancers
At a high level:
Layer 4 Load Balancer
Works primarily with network-level information such as:
- IP
- TCP
- Port
Example:
Client
β
TCP
β
Load Balancer
β
ServerLayer 7 Load Balancer
Works at the application/HTTP level.
It can inspect things such as:
HTTP method
URL
Headers
Cookies
HostFor example:
/api/products
β
Product Service
/api/orders
β
Order ServiceLayer 7 load balancing is especially useful when routing decisions depend on HTTP-level information.
7. Load Balancing Algorithms
The interviewer may ask:
"How does a load balancer decide which server receives a request?"
There are several approaches.
Round Robin
Requests are distributed sequentially.
Request 1 β Server 1
Request 2 β Server 2
Request 3 β Server 3
Request 4 β Server 1
Request 5 β Server 2Pattern:
S1 β S2 β S3 β S1 β S2 β S3Simple and commonly used.
8. Weighted Round Robin
Not every server has the same capacity.
Suppose:
Server 1 β powerful
Server 2 β medium
Server 3 β smallWe could assign:
Server 1 β weight 5
Server 2 β weight 3
Server 3 β weight 2The stronger server receives more traffic.
9. Least Connections
The load balancer sends a new request to the server with the fewest active connections.
Example:
Server 1 β 100 connections
Server 2 β 50 connections
Server 3 β 20 connectionsNew request:
β
Server 3This can be useful when requests have different processing times.
10. IP Hash
The load balancer calculates a hash based on the client's IP.
Conceptually:
Client IP
β
Hash
β
ServerThis can help maintain affinity for a client, although relying on IP-based affinity has limitations and is not automatically the best solution.
11. Health Checks
This is an important interview concept.
Suppose we have:
Server 1 β
Server 2 β
Server 3 β
How does the load balancer know Server 2 is unhealthy?
Through health checks.
For example:
GET /healthServer responds:
200 OKThe load balancer considers it healthy.
If it repeatedly fails:
500
Timeout
Connection refusedthe load balancer can temporarily remove the server from rotation.
12. ASP.NET Core Health Checks
Since you're preparing for .NET interviews, this is particularly relevant.
ASP.NET Core supports health checks.
Conceptually:
builder.Services.AddHealthChecks();Then:
app.MapHealthChecks("/health");Now an infrastructure component can check:
GET /healthYou can also add checks for dependencies such as databases.
For example:
Application
β
βββ API
βββ SQL Server
βββ Redis
βββ External APIA production health strategy may distinguish between:
Liveness
Is the application process alive?
Readiness
Is the application ready to receive traffic?
This distinction becomes very important in containerized environments.
13. What is a Stateless Service?
This is one of today's most important concepts.
A stateless server doesn't rely on local server memory to maintain a user's session between requests.
Imagine:
Request 1 β Server 1
Request 2 β Server 2
Request 3 β Server 3The application should still know who the user is.
The server shouldn't depend on:
Server 1's RAMfor essential session state.
14. Stateful vs Stateless
Stateful
User
β
Server 1
Session stored in Server 1 memoryNext request:
User
β
Server 2
β Server 2 doesn't know the sessionThis creates problems when horizontally scaling.
Stateless
Instead:
User
β
Load Balancer
β
Any ServerState can be stored externally:
βββββββββββββββ
β API Servers β
ββββββββ¬βββββββ
β
ββββββββ΄βββββββ
β β
Redis DatabaseNow any server can handle the request.
15. JWT and Stateless Authentication
This connects directly with what you've been learning in ASP.NET Core.
With JWT authentication:
Login
β
Authentication Service
β
JWT
β
ClientThe client sends:
Authorization: Bearer <token>with subsequent requests.
Any API instance can validate the token.
Load Balancer
/ | \
βΌ βΌ βΌ
API1 API2 API3
\ | /
\ | /
DatabaseThis is one reason token-based authentication can work well with horizontally scaled APIs.
Important: "JWT = always stateless" is an oversimplification. If your system maintains server-side session/revocation state, some state still exists. The key interview point is that the API instances don't need to keep the user's primary session in local memory.
16. Session Stickiness
You may hear:
Sticky sessions
or:
Session affinity
This means the load balancer tries to send the same user's requests to the same server.
User A
β
Load Balancer
β
Server 1
User A's future requests
β
Server 1This can make stateful applications easier to operate.
But it has disadvantages:
- Uneven traffic
- Reduced flexibility
- Server failure can affect sessions
- Scaling becomes more complicated
Therefore, for many modern APIs, we prefer:
Stateless application servers + shared external state when state is required.
17. Scaling with External State
Suppose you have:
API Server 1
API Server 2
API Server 3and all need access to:
Session
Cache
Shared dataDon't put it only in:
API Server 1 memoryInstead:
Load Balancer
/ | \
βΌ βΌ βΌ
API1 API2 API3
\ | /
\ | /
βΌ βΌ βΌ
Redis
β
βΌ
DatabaseRedis can provide shared in-memory data, while the database provides durable storage.
We'll study Redis properly later.
18. Single Point of Failure
Imagine:
Load Balancer
β
βΌ
ServerIf the server fails:
Server βEntire application is unavailable.
That's a single point of failure (SPOF).
A common approach is redundancy:
Load Balancer
/ | \
βΌ βΌ βΌ
API1 API2 API3Now one API server can fail without necessarily bringing down the whole application.
But notice something subtle:
If you have only one load balancer, the load balancer itself could become a single point of failure.
In production systems, infrastructure components are typically designed with redundancy/failover as appropriate.
19. Complete Example
Suppose we have an ASP.NET Core e-commerce API.
A simplified architecture:
Internet
β
βΌ
βββββββββββββββ
βLoad Balancerβ
ββββββββ¬βββββββ
β
ββββββββββββΌβββββββββββ
βΌ βΌ βΌ
API 1 API 2 API 3
β β β
ββββββββββββΌβββββββββββ
β
ββββββββββ΄βββββββββ
βΌ βΌ
Redis SQL ServerRequest:
GET /api/products/100Flow:
Client
β
Load Balancer
β
API Server
β
Redis
β
βββ Cache Hit β Return product
β
βββ Cache Miss
β
SQL Server
β
Store in Redis
β
Return responseThis architecture gives us:
- Horizontal scalability
- Load distribution
- Fault tolerance at the API tier
- Shared caching
- Durable database storage
20. Azure Mapping β Important for Your Interviews
Because you're targeting .NET + Azure roles, you should learn to map these concepts to Azure services.
Conceptually:
Internet
β
βΌ
Azure Load Balancer /
Application Gateway
β
βΌ
ASP.NET Core APIs
/ | \
βΌ βΌ βΌ
Instance Instance Instance
β
ββββββββ΄βββββββ
βΌ βΌ
Azure Cache Azure SQL
for RedisThe exact Azure service depends on the architecture and whether you need L4 or L7 routing, WAF, global distribution, etc.
We'll cover Azure-specific system design later rather than memorizing service names now.
π― Day 2 Interview Questions
As with DSA and SQL, try these yourself first.
Q1.
What is horizontal scaling and why is it preferred for many large-scale systems?
Q2.
What is the difference between vertical and horizontal scaling?
Q3.
What is a load balancer and why do we need one?
Q4.
What is the difference between Layer 4 and Layer 7 load balancing?
Q5.
Explain Round Robin, Weighted Round Robin, Least Connections and IP Hash.
Q6.
What is a health check? Why is it important?
Q7.
What is the difference between a stateful and stateless service?
Q8.
Why are stateless services easier to scale horizontally?
Q9.
What are sticky sessions? What are their advantages and disadvantages?
Q10.
What is a Single Point of Failure? How would you design a system to avoid it?
π₯ Bonus Interview Scenario
You have an ASP.NET Core API running on one Windows/Azure VM.
Traffic increases from:
1,000 requests/secto:
20,000 requests/secThe interviewer asks:
"How would you scale this application?"
Your thought process should now be:
Single API server
β
Multiple API instances
β
Load Balancer
β
Stateless APIs
β
Shared Cache / Database
β
Health Checks
β
Auto ScalingDon't immediately jump to microservices.
A horizontally scaled monolith can be perfectly valid.
That's an important system-design lesson.
π§ Day 2 Key Takeaways
Remember these 7 points:
1. Vertical scaling β Bigger machine
2. Horizontal scaling β More machines
3. Load balancer β Distributes traffic
4. Health check β Detects unhealthy instances
5. Stateless API β Any instance can handle request
6. Sticky session β Routes user to same instance
7. SPOF β Component whose failure can
bring down the systemand I'll give you the interview-ready answers + common follow-up questions + .NET/Azure examples.