Ankit KanojiyaIntermediate

Evergreen guide by Ankit Kanojiya · Intermediate · System Design

Load Balancing and Reverse Proxies Explained

Understand what load balancers and reverse proxies do, the difference between L4 and L7, common routing algorithms, health checks, and how sticky sessions work.

Once an application runs on more than one server, something must decide where each request goes. That something is usually a load balancer, and it is often also a reverse proxy. The two roles overlap but are not identical, and knowing the difference clarifies a lot of infrastructure design.

Reverse proxy vs load balancer

A reverse proxy sits in front of one or more servers and forwards client requests to them. Clients talk only to the proxy. Even with a single backend, a reverse proxy is useful for:

  • Terminating TLS in one place.
  • Serving cached or static content.
  • Compressing responses and rewriting headers.
  • Hiding backend topology from clients.

A load balancer is a reverse proxy whose main job is distributing traffic across multiple backends for scale and availability. In practice tools like Nginx, HAProxy, and Envoy do both.

Layer 4 vs Layer 7

Load balancers operate at different levels of the network stack:

  • Layer 4 (transport): routes by IP address and TCP/UDP port without inspecting the payload. It is fast and protocol-agnostic, but cannot make decisions based on URLs or headers.
  • Layer 7 (application): understands HTTP. It can route by path, host, header, or cookie, terminate TLS, and retry failed requests intelligently.

L4 is right for raw throughput and non-HTTP protocols. L7 is right when routing decisions depend on the content of the request.

# L7 routing by path
/api/*    -> api-servers
/static/* -> cdn-origin
/*        -> web-servers

Routing algorithms

How does the balancer pick a backend? Common choices:

  • Round robin: rotate through servers in order. Simple, assumes servers are equal.
  • Weighted round robin: give more traffic to more powerful servers.
  • Least connections: send to the server with the fewest active connections. Good when request durations vary.
  • IP hash: hash the client IP to a server, giving the same client the same backend.

Least connections is often the best default for web traffic, because it adapts to uneven request costs that round robin ignores.

Health checks

A load balancer must stop sending traffic to a dead backend. It does this with health checks:

  • Passive: watch real traffic for errors and timeouts.
  • Active: periodically probe an endpoint such as /healthz.

A good health endpoint checks that the server can actually serve requests, for example that it can reach its database, rather than just returning 200 unconditionally. But avoid making it so strict that one slow dependency pulls every server out of rotation at once.

GET /healthz -> 200 OK   (ready for traffic)
GET /healthz -> 503      (drain me)

Sticky sessions

Stateless servers let any backend handle any request. When state lives on a specific server, you need sticky sessions (session affinity): the balancer routes a given client to the same backend repeatedly, usually via a cookie or IP hash.

Stickiness is a workaround, not a goal. It undermines even load distribution and complicates deploys, because draining a server disrupts its pinned users. The better fix is to externalize session state into a shared store so stickiness is unnecessary.

TLS termination

Terminating TLS at the load balancer means it decrypts incoming HTTPS and forwards plain HTTP to backends on a trusted network. This centralizes certificate management and offloads crypto from app servers. When the internal network is not trusted, re-encrypt to the backend (TLS passthrough or re-encryption) instead.

High availability of the balancer itself

A single load balancer is a single point of failure. Production setups avoid this with:

  • A pair of balancers with a floating virtual IP that fails over.
  • DNS-based distribution across multiple balancers.
  • A managed cloud load balancer that is redundant internally.

Putting it together

A typical request path looks like this:

Client
  |
  v
DNS -> Load Balancer (L7, TLS termination, health checks)
          |  least-connections
   ---------------------
   |        |          |
 app-1    app-2      app-3   (stateless)
          |
       Redis (shared sessions)

The balancer spreads load, removes unhealthy nodes, and gives you a seam for deploys, TLS, and caching. Keep the backends stateless, keep the health check honest, and prefer shared state over sticky sessions, and the load balancing tier stays simple as the system grows.