AlgoMaster Logo

Load Balancers

High Priority9 min readUpdated July 4, 2026
AI Mock Interview

Practice this topic in a realistic system design interview

Listen to this chapter
Unlock Audio

As soon as you run more than one instance of a service, you face a simple question: which instance should handle the next request?

A load balancer answers that question. It sits between clients and backend services. Clients connect to one stable address, and the load balancer forwards each request to a backend that should be able to handle it.

This one piece of infrastructure often handles a lot of production work: scaling, failover, TLS termination, connection draining, traffic rules, and observability.

The goal is not to split requests perfectly evenly. The goal is to keep traffic moving to backends that are healthy and have capacity. If a backend is overloaded or failing health checks, it should stop receiving new work, even if that makes the traffic split uneven.

The diagram below shows the basic shape: clients use one stable address, and the load balancer spreads requests across a pool of backends.

This chapter explains what load balancers do, where they sit in a system, and the production problems they help solve.

1. The Problem Load Balancers Solve

Load balancers solve several practical problems:

  1. Horizontal scaling: Add or remove backend instances without changing the client-facing address.
  2. Availability: Stop sending new traffic to unhealthy instances.
  3. Deployments: Drain traffic from an instance or version before replacing it.
  4. Traffic control: Route by host, path, protocol, region, tenant, or service policy.
  5. Connection management: Reuse backend connections, enforce timeouts, and protect backends from overload.
  6. Security boundary: Terminate TLS, apply WAF rules, rate limit, and keep private backends away from direct internet access.
  7. Observability: Collect request logs, latency metrics, error rates, and backend health signals in one place.

The main design benefit is separation. Clients talk to the load balancer. Backends can be added, removed, replaced, or marked unhealthy behind it.

2. Where Load Balancers Sit

Load balancers can appear at several layers of a system.

Common placements:

  • Global front door: Sends users toward a region or edge location.
  • Regional load balancer: Spreads traffic across zones and instances.
  • Internal service load balancer: Routes service-to-service traffic inside a VPC, Kubernetes cluster, or service mesh.
  • Client-side load balancer: Library or sidecar chooses endpoints directly from service discovery.
  • Database or cache proxy: Spreads connections across replicas, shards, or primary/replica endpoints.

Do not assume a system has only one load balancer. Large systems often use several, each with a different job.

3. Layer 4 vs Layer 7

The most important distinction is whether the load balancer understands the application protocol.

Loading simulation...

Layer 4 Load Balancers

A Layer 4 load balancer works at the transport layer. It sees IP addresses, ports, TCP, UDP, and connection state. It does not read HTTP paths, headers, cookies, or request bodies.

Layer 4 load balancers are common for:

  • High-throughput TCP and UDP services
  • TLS pass-through
  • Databases and caches
  • Game servers
  • Real-time protocols
  • Services where the application protocol is custom or encrypted end to end

They are usually faster and simpler than Layer 7 load balancers, but they know less about each request.

Layer 7 Load Balancers

A Layer 7 load balancer understands the application protocol, most commonly HTTP.

It can route based on:

  • Hostname
  • URL path
  • HTTP method
  • Headers
  • Cookies
  • gRPC method
  • JWT claims or tenant information, if integrated carefully

Layer 7 load balancers are common for web apps, APIs, microservices, and edge gateways.

That control has a cost. Reading application traffic gives you smarter routing, but it also adds more configuration, more ways to misconfigure the system, and more CPU work.

FeatureLayer 4Layer 7
Decision inputIP, port, protocol, connectionHTTP/gRPC request details
Typical protocolsTCP, UDP, TLS pass-throughHTTP, HTTPS, HTTP/2, gRPC
StrengthThroughput, simplicity, works with many protocolsRich routing and policy
WeaknessLimited request awarenessMore overhead and configuration risk
ExamplesNetwork load balancer, LVS/IPVS, AWS NLBNGINX, HAProxy HTTP mode, Envoy, AWS ALB

Many production stacks use both: Layer 4 near the edge for raw throughput, and Layer 7 behind it when routing depends on request details.

4. How Request Flow Works

A typical request through a Layer 7 load balancer looks like this:

Connect and send requestApply listener, TLS, routing, policySelect healthy backendForward requestResponseResponseClientLoad BalancerBackend Instance
6 / 6
algomaster.io

Important details:

  • The load balancer may terminate TLS or pass encrypted traffic through.
  • It may reuse backend connections instead of opening a new backend connection for every client connection.
  • It may retry failed requests, but only when retrying is safe.
  • It should enforce timeouts so slow backends do not consume resources forever.
  • It should stop routing to backends that fail health checks.

5. Health Checks

Health checks decide whether a backend should receive new traffic.

Simple checks only prove that a process accepts connections. Better checks prove that the backend is ready to serve real work.

CheckWhat It ProvesWhat It Can Miss
TCP connectPort is openApplication may be broken
HTTP /healthApp responds to a known endpointDownstream dependency may fail later
Readiness checkInstance should receive trafficMay miss real problems if poorly designed
Synthetic requestA representative path worksMore expensive and harder to keep reliable

Health checks should be fast, stable, and meaningful. They should also use thresholds:

Without thresholds, a brief network blip can bounce an instance in and out of rotation. With thresholds that are too slow, failed instances keep receiving traffic for too long.

6. Connection Draining and Deployments

Removing a backend from rotation is not the same as killing it.

During a deployment, the safer flow is:

  1. Mark the instance as draining.
  2. Stop sending it new requests.
  3. Let in-flight requests finish.
  4. Close or migrate long-lived connections according to policy.
  5. Shut the instance down.

This matters for HTTP requests, WebSockets, gRPC streams, file uploads, and token-streaming AI responses. If you kill a backend while it is streaming a response, the user sees the failure.

Good systems combine load balancer draining with application shutdown hooks. The application should stop accepting new work before the process exits.

7. TLS Termination

Many load balancers terminate TLS. The client establishes HTTPS with the load balancer, and the load balancer forwards traffic to the backend.

There are three common patterns:

PatternDescriptionUse Case
TLS terminationLB decrypts; backend receives plain HTTPCommon web/API deployments
TLS pass-throughLB forwards encrypted TCP without decryptingEnd-to-end encryption or custom protocols
TLS re-encryption (bridging)LB decrypts and inspects, then opens a new TLS connection to the backendZero-trust or regulated internal networks

TLS termination keeps certificate management in one place and enables Layer 7 routing. Pass-through keeps traffic encrypted all the way to the backend, but limits application-aware features.

8. Session Affinity

Session affinity, often called sticky sessions, routes the same client to the same backend.

Common methods:

  • Source IP affinity
  • Cookie-based affinity
  • Header-based hashing
  • Consistent hashing on a tenant, user, or session key

Affinity is useful when backends hold local state, keep warm caches, or handle long-lived sessions.

It can also be a warning sign. If sticky sessions are hiding state that could live in a shared database, cache, or token, the system will usually be harder to scale and recover.

Use affinity deliberately. Document what breaks if a user moves to another backend.

9. Algorithms

The load balancer needs a rule for picking a backend. Common algorithms include:

AlgorithmBest ForWatch Out For
Round robinSimilar short requestsIgnores request cost and active load
Weighted round robinBackends with known capacity differencesWeights can drift from reality
Least connections / least requestsLong or uneven requestsNeeds accurate active request tracking
Power of two choicesLarge backend poolsNot perfect, but cheap and effective
Consistent hashCache locality and sticky routingUneven keys can create hot spots
RandomSimple balancing with little coordinationNo health or load awareness unless combined with checks
Locality-awareMulti-zone or multi-region systemsCan overload local capacity without spillover rules

No algorithm can fix unhealthy backends, missing timeouts, bad retries, or not enough capacity.

10. Failure Modes

Load balancers improve availability, but many services may depend on them. Design for the ways they can fail.

The Load Balancer as a Bottleneck

A load balancer has limits: connections, packets per second, requests per second, TLS handshakes, memory, and routing-rule cost.

Track those limits. Do not discover them for the first time during an incident.

Bad Health Checks

If health checks are too weak, broken backends stay in rotation. If they are too strict, healthy backends get removed during harmless dependency blips.

Health check design is production logic, not boilerplate.

Retry Storms

Retries at the load balancer can make outages worse. If every failed request is retried three times against an already overloaded pool, the load balancer adds more pressure exactly when the system is weakest.

Retries need budgets, timeouts, backoff, and awareness of whether the request is safe to repeat.

Long-Lived Connections

WebSockets, HTTP/2 streams, gRPC streams, and AI token streams do not behave like short HTTP requests. They hold connections for longer and make deployments harder.

Use draining, max connection age, keepalive settings, and clear reconnect behavior.

Uneven Request Cost

One request may take 2 ms. Another may run a 30-second report or a long model inference. Balancing request counts is not the same as balancing work.

For expensive workloads, route using application-level signals such as queue depth, model type, tenant quota, or estimated cost.

11. Load Balancers in Modern Systems

Today, "load balancer" means more than one appliance in front of a web tier. Kubernetes, service meshes, and AI inference systems all add their own balancing layers with different jobs.

The sections below show how the idea appears in modern systems.

Kubernetes

In Kubernetes, "load balancer" can mean several things:

  • A cloud load balancer created for a Service of type LoadBalancer
  • An ingress controller such as NGINX, HAProxy, or Envoy-based gateways
  • A service mesh sidecar or node proxy
  • kube-proxy, eBPF-based networking such as Cilium, or cloud-provider routing integrations

Be precise about which layer you mean. A Kubernetes Service, an ingress controller, and a cloud load balancer solve different problems.

Service Mesh and Sidecars

Service meshes move some load balancing into sidecars or node proxies. The client-side proxy can choose from discovered endpoints, apply retries, enforce mTLS, and collect logs, metrics, and traces.

This is useful, but it increases operational complexity. A bad mesh configuration can break service-to-service traffic just as badly as a code bug.

AI Systems

AI inference traffic stresses load balancers in ways classic HTTP traffic does not. Responses may stream tokens for tens of seconds, sometimes minutes. That makes simple request-count metrics misleading and makes connection draining very important.

A normal Layer 7 load balancer is a good front door for TLS, authentication, and ingress. But model-aware scheduling, such as GPU placement, queue depth, and model availability, belongs in a dedicated inference gateway behind it.

12. Practical Design Rules

The rules below turn the chapter into practical decisions you can apply when designing a load-balanced service.

  1. Put a load balancer in front of any horizontally scaled service.
  2. Use Layer 4 when you need maximum throughput or do not need HTTP-level routing.
  3. Use Layer 7 when routing depends on HTTP/gRPC request details.
  4. Make health checks meaningful, not just "process is alive."
  5. Configure timeouts deliberately: client, load balancer, backend, idle, and request timeouts.
  6. Use connection draining for deployments.
  7. Keep backend services stateless when possible.
  8. Treat retries as load multipliers.
  9. Monitor the load balancer itself, not only the backends.
  10. Do not rely on one layer for everything. Use DNS/global routing, regional load balancers, and service routing for different decisions.

Summary

A load balancer gives clients one stable endpoint while backend instances scale, fail, deploy, and recover behind it. That separation is the core idea: clients keep using the same address while backends change.

Layer 4 and Layer 7 load balancers solve different problems. Layer 4 works with network and connection details. Layer 7 understands application details such as HTTP paths, hosts, headers, and methods.

Health checks are part of system correctness. A bad health check can send traffic to broken instances or remove healthy ones. Connection draining matters because stopping new traffic before shutdown avoids many user-visible failures.

The algorithm is only one piece. TLS termination, retries, session affinity, and routing rules are all design choices with tradeoffs. Timeouts, capacity, health, and observability matter just as much.

Modern systems usually run several load-balancing layers together: DNS, edge, regional, service mesh, and application routing.

The goal is simple: give clients a stable way in, send work only to backends that can handle it, and make failures predictable instead of chaotic.

Quiz

What are Load Balancers? Quiz

10 quizzes