Load Balancing and Proxies

How traffic is spread across servers: where load balancers sit, layer 4 versus layer 7, algorithms (round robin, weighted, least connections, least response time, IP hash, consistent hashing), health checks, smart clients versus hardware and software balancers like HAProxy, redundant balancers, and forward versus reverse proxies.

On this page

Simple definition#

A load balancer (LB) spreads incoming requests across a group of servers so that no single server is overloaded, and stops sending traffic to servers that are unhealthy. It improves scalability (add servers behind it), availability (route around failures) and responsiveness.

It is also a proxy: clients talk to the balancer, not to the servers.

Where load balancers sit#

To get full scalability and redundancy, balance load at each tier:

Load balancers between tiers
flowchart LR
    U((Users)) --> LB1[LB]
    LB1 --> W1[Web server]
    LB1 --> W2[Web server]
    W1 --> LB2[LB]
    W2 --> LB2
    LB2 --> A1[App / internal service]
    LB2 --> A2[App / internal service]
    A1 --> LB3[LB]
    A2 --> LB3
    LB3 --> D1[(DB / cache)]
    LB3 --> D2[(DB / cache)]
  1. Between users and web servers.
  2. Between web servers and internal application or platform services (and caches).
  3. Between the application tier and databases (for read replicas).

Layer 4 versus layer 7#

Layer 4 (transport)Layer 7 (application)
Decides usingIP addresses and ports (TCP/UDP)HTTP method, path, headers, cookies, host
Sees contentNo (can pass TLS through)Yes (terminates TLS)
Can doFast connection-level forwardingPath-based routing, header rewriting, caching, compression, auth, rate limiting
CostCheaper, fasterMore CPU per request
ExamplesAWS NLB, IPVS, HAProxy TCP modeAWS ALB, NGINX, Envoy, HAProxy HTTP mode, Spring Cloud Gateway

Algorithms#

AlgorithmHowGood forWeakness
Round robinEach server in turnIdentical servers, similar requestsIgnores current load
Weighted round robinProportional to configured weights (CPU, memory)Mixed server sizesStatic weights
RandomRandom server (optionally weighted)Simple, stateless balancersUneven in small samples
Least connectionsServer with fewest active connectionsLong-lived or variable-length requestsConnections ≠ load
Least response timeFastest recent responses and fewest connectionsHeterogeneous latencyNeeds measurement
Power of two choicesPick two at random, choose the less loadedLarge fleets; avoids herding—
IP hash / consistent hashingHash of client IP or a key picks the serverSession affinity, cache localityUneven if keys are skewed

Sticky sessions (affinity by cookie or IP hash) send a user to the same server. They are a workaround for stateful servers and reduce the balancer’s freedom; prefer a stateless web tier.

Health checks#

A balancer periodically probes each server (TCP connect, or GET /health). Servers failing several checks are removed from the pool, and added back when they recover. Distinguish liveness (process up) from readiness (able to serve: warmed up, dependencies reachable); route only to ready instances. Spring Boot exposes both through Actuator.

Kinds of load balancer#

KindHowProsCons
Smart clientThe client library holds the server list, detects unresponsive hosts, picks oneNo extra hop; attractive for small systems and internal callsLogic in every client and language
HardwareDedicated appliances (e.g. Citrix NetScaler, F5)Very high performanceExpensive, not trivial to configure
SoftwareHAProxy, NGINX, Envoy on commodity machines or as a managed cloud serviceCheap, flexible, programmableYou operate it (unless managed)

A hybrid is common: large companies use hardware or cloud balancers at the edge as the first point of contact, and smart clients or software balancers internally. HAProxy can run on each client machine (the app talks to localhost:9000, HAProxy forwards to the pool) or on intermediate servers shared by many clients; either way it handles health checks and adds or removes machines.

In Java microservices, Spring Cloud LoadBalancer with a service registry is a smart-client balancer, and a service mesh (Envoy sidecars) moves the logic out of the application.

Avoiding a single point of failure#

A single load balancer is itself a single point of failure. Run a pair:

  • Active–passive: the passive node monitors the active one (heartbeat) and takes over its virtual IP if it fails (VRRP, keepalived).
  • Active–active: both serve traffic, with DNS or anycast spreading load between them.

Managed cloud balancers are already redundant across zones.

Forward and reverse proxies#

Forward proxyReverse proxy
Acts on behalf ofClientsServers
HidesClient identity from serversServer topology from clients
UsesCorporate egress filtering, anonymity, client-side cachingLoad balancing, TLS termination, caching, compression, WAF, rate limiting
ExamplesSquid, corporate proxiesNGINX, HAProxy, Envoy, CDNs, API gateways

The handwritten notes add a useful use of proxies: collapsed forwarding. A proxy that receives many identical requests (the same object) at once can forward one request and share the response, or combine requests for nearby data into one, reducing load on the origin — especially useful when caching is limited.

Common mistakes#

  • One balancer, no redundancy.
  • Round robin with very different request costs, overloading some servers.
  • Relying on sticky sessions instead of externalising state.
  • No connection draining during deployments, dropping in-flight requests.

Quick check#

What is the difference between layer 4 and layer 7 load balancing? Layer 4 routes by IP and port without reading content; layer 7 reads HTTP and can route by path, header or cookie.

When is least connections better than round robin? When requests vary in duration or connections are long-lived, so the number of active connections reflects load better than a fixed rotation.

What is a smart client? A client library that load-balances itself: it holds the server list, detects unhealthy hosts and picks a server.

How do you make the load balancer highly available? Run a redundant pair (active-passive with a floating IP, or active-active), or use a managed multi-zone balancer.

Forward versus reverse proxy? A forward proxy represents clients to servers; a reverse proxy represents servers to clients.

References#

  • System Design Notes (handwritten, LinkedIn): load balancing, smart clients, hardware and software load balancers, proxies.
  • Alex Xu. ByteByteGo System Design Handbook: how to scale a website.
  • HAProxy documentation.