A load balancer is a device or service that distributes incoming network traffic across multiple servers or other targets so no single one carries the whole load. It monitors the health of those targets and routes traffic only to healthy ones. Because it stands between clients and servers, backend logs record the load balancer's IP unless it passes the client IP in a header such as X-Forwarded-For.

Spreading requests this way increases the availability and fault tolerance of an application. NGINX describes load balancing as a technique for optimizing resource utilization, maximizing throughput, reducing latency and ensuring fault-tolerant configurations.

Common Load Balancing Methods

  • Round robin: Requests go to each server in turn.
  • Least connections: The next request goes to the server with the fewest active connections.
  • IP hash: A hash of the client's IP address picks the server, so the same client keeps reaching the same server unless that server is unavailable. This is one way to get session persistence.
  • Health checks: When a server fails, the load balancer marks it and avoids sending it new requests for a while.

Layer 4 vs. Layer 7 Load Balancers

A Layer 4 load balancer works at the transport layer of the OSI model and routes connections without reading the HTTP request. A Layer 7 load balancer works at the application layer, so it can route on request details such as the URL path, host header or HTTP method. A Layer 7 load balancer can also add the client IP to the X-Forwarded-For header before passing the request on.

SSL/TLS Termination

A load balancer can terminate SSL/TLS for the backend. It decrypts client requests and encrypts responses, work the backend servers would otherwise do. Because the TLS handshake is CPU-intensive, this takes computational load off those servers.