Load balancing: layer 4, layer 7 and gateways

What a load balancer can see and do at each layer, the algorithms behind it, and how API gateways add routing, aggregation and policy.

On the roadmap:Software Architect · step 2: Edge & traffic

Lesson 5 of 10 · 12 min read · quiz · exercise

What a load balancer sees

A load balancer spreads incoming work across servers, removes failed servers and makes scaling invisible to clients. What it can do depends on which layer it works at.

Layer 4 (transport)Layer 7 (application)
SeesIP addresses, ports, TCP/UDP connectionsHTTP method, URL, headers, cookies, body
Decides byConnection detailsRequest content
TLSPasses through, or terminates in some productsUsually terminates TLS
Speed and costVery fast, low overheadMore work per request
Typical featuresSpread connections, health checksPath routing, header rules, sticky sessions, rewrites, caching, WAF rules
ExamplesNetwork load balancers, LVS, HAProxy in TCP modeNGINX, Envoy, HAProxy HTTP mode, cloud application balancers

Figure SDA-ADS-05-1 · Layers of balancing in front of an app · © forcsguy.com

Many designs use both: an L4 tier for raw scale and an L7 tier for smart routing.

Before: layer 4: ports and addresses

After: layer 7: reads the request

What changed

  • Layer 7 can route by path, header or cookie
  • Layer 7 can terminate TLS and retry safely
  • Layer 4 is simpler and faster, with less to configure

Choosing a target: algorithms

  • Round robin / weighted round robin: simple rotation; weights favour bigger machines.
  • Least connections (and weighted): send to the server with fewest active requests; good when requests vary in length.
  • Least response time: combine active load with measured latency.
  • IP hash / consistent hashing: the same client or key goes to the same server, useful for caches and sticky state (see consistent hashing).
  • Resource-based: servers report load and the balancer follows.

Health checks (active probes and passive error tracking) remove unhealthy servers; connection draining lets in-flight requests finish before a server is removed during a deploy.

Global and DNS balancing

Across regions, DNS-based or global server load balancing directs users to the nearest or healthiest region (see the DNS and CDN lessons). DNS answers are cached, so failover takes as long as the TTL; anycast and global balancers react faster.

API gateway: policy at the edge

An API gateway is an L7 component specialised for APIs. Beyond routing it commonly provides:

  • Authentication and authorisation (validate tokens, apply scopes),
  • Rate limiting and quotas (see the rate-limiter lesson),
  • Request and response transformation, versioning, and protocol translation,
  • Aggregation: one client call fans out to several services and merges results, saving round trips for mobile clients,
  • Caching, logging, metrics and tracing in one place.

Figure SDA-ADS-05-2 · Gateway routing and aggregation · © forcsguy.com

Risks: the gateway is a single entry point (run several instances, keep it stateless) and can become a bottleneck or a place where business logic piles up. Keep it to cross-cutting concerns. A variation is a backend for frontend: one gateway per client type (web, mobile) tailored to its needs.

Inside the cluster

Between services, balancing is done by client-side libraries or sidecar proxies that know the healthy instances from service discovery, so no central balancer is needed for east-west traffic. See the microservices lessons for service mesh ideas.

Failure habits

  • Run at least two of every balancer and gateway instance across zones.
  • Keep timeouts and retry budgets (see the resilience lesson) so the balancer does not amplify an outage.
  • Use slow start for new instances so they warm up before taking a full share.

Real-world reading

Check your understanding

Question 1 of 3

0 correct so far

What is the main difference between layer 4 and layer 7 load balancing?

Topics covered in this lesson

All topics

© 2026 forcsguy.com. Original lesson text and diagrams. Books and sites linked above belong to their authors.