Interview Question: Explain Load Balancer

Publish date: 2025-02-02
Tags: Distributed-Systems, Interview-Questions

What is a Load Balancer?

A load balancer acts like a traffic cop for your application, directing incoming requests across multiple servers to prevent overload and ensure smooth performance. It’s the entry point of your system, hiding server details and optimizing traffic flow.

Visual Analogy: Imagine a supermarket with 10 cashiers. The load balancer ensures customers (requests) are evenly distributed to avoid long queues.

Explain Load Balancer


Why Use a Load Balancer?

1. Scalability

2. Resilience

3. Higher Availability

4. Security

5. Performance


Deployment Process with a Load Balancer

Here’s how companies deploy updates without downtime:

  1. Detach a Server: Temporarily remove Server 1 from the pool.
  2. Deploy Code: Update the application on Server 1.
  3. Sanity Check: Test if Server 1 works post-update.
  4. Reattach: Add Server 1 back to the pool.
  5. Repeat: Do this for all servers sequentially.

Visual Guide:

[Client] → [Load Balancer]  
           ↓         ↓  
        [Server 1 (Updated)]  [Server 2 (Old)] → ...  

How Load Balancers Work

Reverse Proxy

Health Checks

Self-Scaling


Load Balancing Algorithms

  1. Round Robin:

    • Sends requests sequentially: Server 1 → Server 2 → Server 3 → repeat.
    • Use Case: Evenly distributed, stateless traffic (e.g., static websites).
  2. Weighted Round Robin:

    • Assigns more requests to powerful servers (e.g., Server 1 handles 60% traffic).
  3. Least Connections:

    • Directs traffic to the server with the fewest active requests.
  4. IP Hash:

    • Uses client IP to assign a fixed server (useful for session persistence).
Clients → [Load Balancer]  
           ↙   ↓   ↘  
        [S1] [S2] [S3]  

Key Configuration Components

  1. Listeners:

    • Rules for traffic entry (e.g., HTTP on port 80 → Server port 8080).
  2. Routing:

    • Conditional rules (e.g., send /admin requests to admin servers).
  3. Target Groups:

    • Group servers by role (e.g., static-content-servers, payment-servers).

Types of Load Balancers

  1. Application Load Balancer (ALB):

    • Layer 7 (HTTP/HTTPS): Routes based on URL paths (e.g., /api vs /images).
    • Example: AWS ALB.
  2. Network Load Balancer (NLB):

    • Layer 4 (TCP/UDP): Handles high-speed, low-latency traffic (e.g., gaming).
  3. DNS Load Balancer:

    • Distributes traffic via DNS (e.g., returns different IPs for example.com).

Interview Cheat Sheet

Common Questions:

  1. How does a load balancer improve security?

    • Answer: Hides server IPs, blocks DDoS attacks, and manages SSL.
  2. What happens if a server fails during deployment?

    • Answer: The load balancer reroutes traffic to active servers.
Tags: Distributed-Systems, Interview-Questions