Skip to content
SeeItFlow
Interactive guide · 8 chapters · ~24 min read

Load Balancer Fundamentals

A load balancer exists because a single server has a ceiling, and because that server failing shouldn't mean the service failing. This guide covers how traffic actually gets distributed and how failure gets detected — algorithms, health checks, session handling, and the mechanisms behind each.

Mangesh MandlikLast updated:
01 · Foundations

What Is a Load Balancer?

A load balancer is a system that sits between clients and a pool of backend servers, distributing incoming requests so no single server is overwhelmed.

From the client's perspective, a load balancer is invisible — it looks like a single server at a single address. Behind that address, the load balancer decides, request by request, which of several backend servers should actually handle the work. This decision is what makes horizontal scaling possible: instead of buying one increasingly expensive machine, you run many ordinary machines and let the load balancer spread traffic across them.

Swipe to pan
ClientbrowserLoad BalancerL4/L7Server AServer BServer C
The canonical shape: client → load balancer → one of several backend servers.

Load Balancer Architecture: Clients, Balancer, and Backend Pool

The diagram above shows the basic architecture. Clients send every request to one address; the load balancer picks a healthy backend server using a distribution algorithm; and the chosen server does the work. Health monitoring keeps failed servers out of the pool, so the pool can grow, shrink, or lose a member without clients noticing.

The core responsibilities

Request distribution

Spread incoming requests across backend servers using an algorithm — round robin, least connections, weighted, IP hash, or least response time. This is the headline feature, but far from the only one.

Health monitoring

Continuously probe backend servers and stop routing to any that are unhealthy, then automatically resume once they recover.

Single entry point

Clients only ever see the load balancer's address. Backend servers can be added, removed, or replaced without clients noticing.

TLS / SSL termination

Terminate TLS once at the edge, or terminate and re-encrypt to backends when required, instead of managing certificates independently on every server.

Session affinity

Optionally pin a client's requests to the same backend server. Useful for stateful backends, but it is a workaround that can skew load and makes each client partially dependent on that chosen server.

Interactive

Visual Learning

Watch one load balancer route requests across a growing server pool, detect and reroute around failures, terminate TLS, drain connections, and fail over across regions — as a single continuous system.

02 · History & motivation

Why Load Balancers Exist

Every load balancer story starts the same way: one server, then too much traffic for one server.

The bottleneck

A single application server has a hard ceiling — CPU cores, memory, open file descriptors, network bandwidth. You can buy a bigger machine (vertical scaling), but that has diminishing returns and a very real dollar and physical ceiling. Eventually the only way to serve more traffic is to run more machines (horizontal scaling) — and the moment you have more than one machine, something has to decide which machine handles which request. That something is the load balancer.

Availability, not just throughput

The second motivation is just as important: a single server is a single point of failure. If it crashes, restarts, or gets overwhelmed, every user is affected. A pool of servers behind a load balancer means one server failing degrades capacity slightly instead of taking the whole application down — assuming the load balancer notices the failure and reacts (see health checks, next chapter).

Key takeaways
  1. 01Vertical scaling (bigger machine) has a ceiling; horizontal scaling (more machines) has a much higher one.
  2. 02The moment you have more than one server, you need something to route requests between them.
  3. 03Load balancers exist to solve throughput AND availability — not just one.
03 · The mechanism

Layer 4 vs Layer 7 Load Balancing

Load balancers operate at different layers of the network stack — and the layer determines what they can see and decide on.

Swipe to pan
Layer 4Layer 7ClientTCP LBIP:portServerServerroutes on URL / headers / cookiesroutes on IP + port only
L4 routes on IP + port. L7 can read the actual HTTP request.

Layer 4 (transport layer)

A Layer 4 load balancer works with IP, TCP, and UDP metadata. It can choose a backend using source/destination IPs, ports, and connection state, but it does not parse HTTP or other application payloads. That means it cannot make path-based, header-based, or cookie-based routing decisions. It often preserves end-to-end TLS via passthrough because it is not decrypting the traffic to inspect it.

Layer 7 (application layer)

A Layer 7 load balancer understands the application protocol — commonly HTTP/HTTPS or gRPC over HTTP/2. It typically terminates TLS, reads request metadata such as host, path, headers, or cookies, and then routes accordingly. That visibility enables content-based routing, redirects or rewrites, caching, and canary rules, at the cost of more CPU and memory per request than L4.

When to use L4

Very high throughput, protocol-agnostic traffic (databases, custom TCP protocols, gaming), or when you don't need content-aware routing.

When to use L7

Web applications and APIs where routing decisions depend on the request itself — path-based microservice routing, canary releases by header, WebSocket upgrades, and TLS termination.

04 · The related concept

Reverse Proxy

A reverse proxy sits in front of one or more backend servers and forwards client requests to them. A load balancer's primary job is specifically to distribute traffic across multiple backends, so many real systems use one product for both roles.

The term "reverse proxy" describes architectural position and forwarding responsibility; "load balancer" describes the traffic-distribution responsibility. Sometimes a reverse proxy fronts a single origin and does no balancing at all; sometimes the same NGINX, HAProxy, or Envoy deployment also performs load balancing, TLS termination, caching, and header policy. For the full reverse-proxy treatment, see Reverse Proxy Fundamentals.

Hiding backend topology

Clients never learn how many servers exist, what their IPs are, or what technology they run. This is a security boundary as much as an architectural one.

Caching

A reverse proxy can cache responses for identical requests, serving repeat requests without touching a backend server at all.

Compression & security

Gzip/Brotli compression, request/response header sanitization, and basic WAF-style filtering commonly live at this layer.

05 · Routing decisions

Load Balancing Algorithms

Once you have a pool of servers, you need a rule for picking one per request. Every algorithm is a trade-off between simplicity, fairness, and awareness of real server load.

Round Robin

Requests are sent to each server in turn, in a fixed rotation. Simple, predictable, and fair when requests are roughly equal cost and servers have equal capacity.

Weighted Round Robin

Same rotation, but servers with a higher weight receive proportionally more requests. Used when the pool has mixed instance sizes.

Least Connections

Send the next request to whichever server currently has the fewest active connections. Adapts better than round robin when request durations vary widely.

IP Hash

Hash the client's IP address to consistently map it to the same server. Gives you a lightweight form of session affinity without cookies — at the cost of uneven distribution if traffic is dominated by a few IPs (e.g., behind a corporate NAT).

Least Response Time

Combine connection count with recently observed latency to route to the server currently responding fastest. Closest to what a truly "smart" load balancer does in production.

Worked trace: round robin vs least connections (illustrative)

Assumptions: three equal servers A, B, C; one request arrives per second; R1 is slow (10 s) and R2–R6 each take 1.5 s; ties go to the first server.

Request (arrival)Round robin → server (active on A/B/C before)Least connections → server (active on A/B/C before)
R1 (t=0, slow)A (0/0/0)A (0/0/0)
R2 (t=1)B (1/0/0)B (1/0/0)
R3 (t=2)C (1/1/0)C (1/1/0)
R4 (t=3)A (1/0/1) — A is still busy with R1B (1/0/1)
R5 (t=4)B (2/0/0)C (1/1/0)
R6 (t=5)C (1/1/0)B (1/0/1)
Illustrative sequence, not a benchmark. Round robin gives A two of six requests, one of them while A is still stuck on the slow R1; least connections routes around A until it drains.

Round robin ignores load, so its fairness depends on requests costing about the same. Least connections uses the live active count, which is why it copes better with a mix of slow and fast requests.

06 · Reliability

Health Checks

A load balancer is only as good as its knowledge of which servers are actually healthy.

Swipe to pan
Load BalancerServer AhealthyServer BunhealthyServer Chealthy✗ timeout
One unhealthy server is silently excluded from routing until it passes checks again.

A health check is a lightweight periodic probe — often a GET /healthz request every few seconds — that the load balancer sends to each backend server. If a server fails enough consecutive checks (the unhealthy threshold), it's removed from rotation immediately, without waiting for an actual user request to fail.

Active vs passive health checks

Active checks are proactive probes sent independent of real traffic. Passive checks observe real request outcomes (timeouts, 5xx responses) and react to failures as they happen. Production systems usually run both.

Thresholds prevent flapping

A server isn't marked unhealthy after one failed check (that would be too twitchy) nor healthy after one success (too eager). Configuring an unhealthy threshold (e.g., 3 consecutive failures) and healthy threshold (e.g., 2 consecutive successes) avoids servers flapping in and out of the pool.

07 · State

Sticky Sessions & Session Affinity

Load balancing works best when servers are stateless — but many real applications keep some state in memory, and that changes the routing rules.

Swipe to pan
Clientcookie: srv=ALoad BalancerServer Ahas sessionServer BServer C
An affinity cookie routes every request from this client to the same server.

If a server keeps session data (a shopping cart, an authenticated session, a WebSocket connection) purely in its own memory, then subsequent requests from that same user often need to reach that same server or the state appears to vanish. Sticky sessions (session affinity) are the workaround: they pin a client to a server, commonly via a cookie the load balancer sets or via IP hash. The trade-off is uneven load distribution and a soft dependency on that specific backend.

08 · The bigger picture

SSL Termination & Horizontal Scaling

Two more responsibilities complete the fundamentals picture: encryption and elasticity.

Swipe to pan
Client🔒 HTTPSLoad BalancerTLS certServerplain HTTPencryptedplain
With TLS termination, the load balancer decrypts at the edge; it can forward plain HTTP internally or open a new TLS connection to the backend.

SSL / TLS termination

In TLS termination, the load balancer holds the certificate, decrypts incoming HTTPS, and then either forwards plain HTTP internally or opens a new TLS connection to the backend (re-encryption). In TLS passthrough, the load balancer does not decrypt at all; it forwards encrypted traffic end-to-end, which fits L4 designs but gives up most L7 routing visibility.

Horizontal scaling & high availability

Because the load balancer is the single entry point, it's also the natural place to add and remove capacity. Auto-scaling groups register new servers with the load balancer as demand rises, and deregister them (after draining connections) as demand falls — all invisible to the client.

Key takeaways
  1. 01A load balancer is a single, invisible entry point that distributes requests, checks health, and often participates in TLS handling.
  2. 02L4 balances using IP/port-level metadata; L7 balances with awareness of the application request.
  3. 03Reverse proxy and load balancer are overlapping roles that often coexist in the same software.
  4. 04Health checks with thresholds keep the pool accurate without flapping.
  5. 05Sticky sessions are a workaround for stateful servers — a shared session store is the cleaner long-term answer.