What is load balancing?
Load balancing is the process of distributing incoming network traffic across multiple backend servers to ensure no single server becomes overwhelmed. This improves application availability, reliability, and performance.Load balancing helps prevent server overload by spreading requests evenly across your infrastructure.
Why load balancing matters
Without load balancing, all traffic would hit a single server, leading to:- Performance bottlenecks - One server handling all requests
- Single point of failure - If the server goes down, your entire application is unavailable
- Poor resource utilization - Other servers sit idle while one is overworked
- Limited scalability - Can’t handle traffic spikes effectively
How this implementation works
The load balancer uses a strategy pattern to distribute traffic. TheLoadBalancer class coordinates between the backend pool and the routing strategy:
src/balancer/loadBalancer.ts
Request routing flow
When a request comes in, the load balancer follows this flow:Get healthy backends
The load balancer queries the backend pool for all healthy servers using
getHealthyBackends(). This ensures traffic only goes to available servers.Apply routing strategy
The list of healthy backends is passed to the routing strategy (e.g., Round Robin), which selects the next server to handle the request.
The load balancer only considers healthy backends when routing traffic. Unhealthy servers are automatically excluded from the rotation.