InterviewHubPRO
Section Overview
Q5

Question 5 of 50

Part 1: Foundational Distributed Systems Concepts & Trade-offs (1 - 10)

10%
System Design (50 Qs)•Part 1: Foundational Distributed Systems Concepts & Trade-offs (1 - 10)

Load Balancing Algorithms and Layer 4 vs Layer 7 Reverse Proxies.

In Simple Words: A Load Balancer (LB) distributes incoming network traffic across a cluster of backend servers to ensure high throughput, fault tolerance, and zero-downtime deployments.

  • Layer 4 Load Balancing (Transport Layer - TCP/UDP):
    • Operates on IP address and port numbers without inspecting application payload.
    • Lightning fast (millions of packets/sec with kernel bypass like DPDK/eBPF).
    • Cannot inspect HTTP headers, cookies, or URL paths.
  • Layer 7 Load Balancing (Application Layer - HTTP/HTTPS/gRPC):
    • Terminates TLS handshakes, parses HTTP request headers, cookies, and JSON payloads.
    • Can route traffic based on URL path (/api/v1/billing → Billing Cluster, /api/v1/search → Search Cluster).
    • Can inject security headers, handle gzip/Brotli compression, and enforce JWT rate limiting.
typescript
// Layer 7 Weighted Round-Robin Load Balancing Algorithm Implementation
interface BackendServer {
  id: string;
  url: string;
  weight: number;
  currentWeight: number;
  healthy: boolean;
}

export class WeightedRoundRobinLB {
  private servers: BackendServer[];

  constructor(servers: BackendServer[]) {
    this.servers = servers.map(s => ({ ...s, currentWeight: 0 }));
  }

  public getNextServer(): BackendServer | null {
    const active = this.servers.filter(s => s.healthy);
    if (active.length === 0) return null;

    let totalWeight = 0;
    let selected: BackendServer | null = null;

    for (const server of active) {
      server.currentWeight += server.weight;
      totalWeight += server.weight;

      if (!selected || server.currentWeight > selected.currentWeight) {
        selected = server;
      }
    }

    if (selected) {
      selected.currentWeight -= totalWeight;
    }

    return selected;
  }
}