Lesson 14 of 15

Load Balancing

Load Balancing

A load balancer distributes incoming network traffic across multiple servers to improve reliability, performance, and availability.

Algorithms

Round Robin

Requests are distributed sequentially across servers in a circular order.

Request 1 -> Server A
Request 2 -> Server B
Request 3 -> Server C
Request 4 -> Server A  (cycles back)

Least Connections

Each request goes to the server with the fewest active connections.

Weighted Round Robin

Servers with higher weight receive more requests proportionally.

Health Checks

Load balancers periodically check server health. Unhealthy servers are removed from the pool.

Your Task

Implement a LoadBalancer class with multiple strategies:

  • addServer(id, weight) — adds a server with optional weight (default 1)
  • removeServer(id) — removes a server
  • roundRobin() — returns next server using round-robin
  • leastConnections() — returns server with fewest connections
  • connect(serverId) — increments connection count
  • disconnect(serverId) — decrements connection count
  • getStats() — returns connection counts for all servers
Node.js loading...
Loading...
Click "Run" to execute your code.