A load balancer is a networking component or software service that distributes incoming traffic across multiple backend servers. It helps prevent server overload and improves application performance, scalability, and availability.
- Distributes requests across healthy backend servers using routing algorithms.
- Supports horizontal scaling by allowing additional servers to handle traffic.
- Improves availability when combined with health checks, redundancy, and failover.
Example: An e-commerce application can use NGINX, HAProxy, or AWS Elastic Lo ad Balancing to distribute user requests across multiple application servers.

Why Do We Need a Load Balancer?
Without load balancing, incoming traffic may be concentrated on a single server, resulting in performance issues and potential service disruptions.

Common problems include:
- Server Overload: Excessive requests to a single server increase response time and may cause failures.
- Single Point of Failure: If an application depends on one server, its failure can make the service unavailable.
- Uneven Resource Utilization: Some servers may be overloaded while others remain underutilized.
- Limited Scalability: Increasing application capacity becomes difficult without effective traffic distribution.
A load balancer addresses these problems by distributing requests across multiple available servers.
How a Load Balancer Works
A load balancer acts as an intermediary between clients and backend servers. It receives requests and forwards them to healthy servers using a configured load-balancing algorithm.

The request-handling process follows these steps:
- Receive Request: A client sends a request to the load balancer.
- Identify Healthy Servers: The load balancer uses health information to determine which backend servers are available.
- Select Server: It selects a backend server using an algorithm such as Round Robin or Least Connections.
- Forward Request: The request or connection is forwarded to the selected server.
- Return Response: The backend processes the request, and the response reaches the client through the configured traffic path.
- Handle Failures: Unhealthy servers are excluded from normal traffic distribution until they recover.
Key Capabilities of Load Balancers
Load balancers provide several capabilities for managing traffic and maintaining application reliability.
- Traffic Distribution: Routes incoming requests across backend servers using configured algorithms.
- Health Monitoring: Detects unhealthy instances and avoids routing new traffic to them.
- Horizontal Scaling: Supports adding backend instances as application traffic increases.
- Failover Support: Redirects new requests to healthy servers when failures are detected.
- SSL/TLS Termination: Can handle HTTPS connections and forward traffic to backend services.
- Session Persistence: Routes requests from the same client to a consistent backend when required.
- Application-Level Routing: Directs HTTP requests based on URL paths, hostnames, headers, or cookies.
Types of Load Balancers
Load balancers can be classified based on deployment methods and networking layers.
1. Based on Deployment
- Hardware Load Balancer: A dedicated networking appliance offering specialized traffic management, often with higher infrastructure costs. Example: F5 hardware appliances.
- Software Load Balancer: Runs on servers, virtual machines, or containers and provides flexible traffic distribution. Examples: NGINX and HAProxy.
- Cloud Load Balancer: A cloud-managed service that integrates with cloud infrastructure and reduces operational overhead. Example: AWS Elastic Load Balancing.
2. Based on OSI Layer
- Layer 4 Load Balancer: Operates at the transport layer and routes TCP or UDP traffic using IP addresses, ports, and connection information.
- Layer 7 Load Balancer: Operates at the application layer and routes HTTP requests using URLs, hostnames, headers, cookies, or request methods.
Example: A Layer 7 load balancer can route
/apirequests to application servers and/imagesrequests to an image service.
Load-Balancing Algorithms
Load-balancing algorithms determine how incoming requests or connections are distributed among available backend servers.
- Round Robin: Distributes requests sequentially across servers, making it suitable for instances with similar capacity.
- Weighted Round Robin: Assigns traffic based on server weights so higher-capacity servers receive a larger share.
- Least Connections: Selects the server with the fewest active connections, useful when connection durations vary.
- Weighted Least Connections: Considers both active connections and server capacity when selecting a backend.
- IP Hash: Uses client IP information to select a server, supporting session affinity but potentially causing uneven distribution.
- Least Response Time: Considers observed response times and, depending on implementation, active connections to select a backend.
Server Health Monitoring
Health monitoring helps load balancers identify unavailable backend servers and avoid routing new requests to them.
- Active Health Checks: Periodically send test requests, such as GET /health, to verify backend availability.
- Passive Health Checks: Monitor real traffic for connection failures, timeouts, and unsuccessful responses.
- Heartbeats: Use periodic signals from services or nodes to indicate availability in systems that support heartbeat monitoring.
Automatic Failover and Recovery
Automatic failover helps maintain application availability by redirecting traffic when backend servers become unhealthy.
- Failure Detection: Health checks identify servers that are unavailable or not responding correctly.
- Traffic Redirection: New requests are routed to the remaining healthy servers.
- Recovery Checks: Failed servers continue to be monitored and can rejoin the pool after passing health checks.
When to Use a Load Balancer
Load balancing is useful when applications need to distribute traffic across multiple backend resources.
- High-Traffic Applications: Distributes large request volumes across multiple backend servers.
- Multiple Backend Instances: Routes traffic across several application instances serving the same workload.
- Microservices Architecture: Helps route requests to appropriate services or service instances.
- Redundant Deployments: Supports availability by redirecting traffic to healthy instances when failures occur.
- Multi-Zone or Multi-Region Systems: Can distribute traffic across deployment locations, depending on the load-balancing solution.
Benefits of Load Balancing
Load balancing helps systems manage traffic more efficiently and support increasing workloads.
- Efficient Resource Utilization: Distributes requests across available backend servers.
- Improved Scalability: Supports horizontal scaling by allowing additional backend instances to be added.
- Improved Availability: Helps keep requests flowing when some backend instances become unhealthy.
- Reduced Overload Risk: Helps prevent excessive traffic from being concentrated on individual servers.
- Flexible Routing: Directs requests to different backend services according to application requirements.