Section 02 · Foundation

Load Balancers & Traffic Routing

Learn how traffic enters a service, how a load balancer selects healthy backends, and how routing policy changes capacity, failover behavior, active connections, and isolation between workloads.

By the end

You will be able to choose a routing layer and algorithm, calculate traffic shares, plan capacity after failure, and design isolated backend pools for different routes.

9 chapters·8 figures·4 labs
01
Load balancer fundamentals

What a load balancer does

A load balancer is a network service placed in front of backend servers. Clients connect to the load balancer rather than choosing an application server directly. The load balancer then selects an eligible backend and forwards the work.

This indirection creates one stable entry point while allowing the backend fleet to grow, shrink, or lose unhealthy members. It does not create backend capacity by itself. It makes existing capacity reachable and controllable.

Load balancer

A service that accepts incoming connections or requests and forwards each one to an eligible backend.

Listener

The load balancer endpoint that accepts traffic on a protocol and port, such as HTTPS on port 443.

Backend

A server instance that can receive forwarded work from the load balancer.

Backend pool

A named or logical group of backends eligible for a particular class of traffic.

Routing rule

A condition and action that decide which backend pool receives traffic.

Health check

A periodic probe used to decide whether a backend should remain eligible for new traffic.

Failover

Continued service after a component fails, using healthy capacity that remains available.

Active connection

A connection currently open on a backend. Duration determines how long it contributes to current work.

Traffic weight

A relative value that controls how much traffic one backend receives compared with others.

Upstream

A common proxy term for the backend service or pool to which traffic is forwarded.

The forwarding path

A valid path has three stages: traffic arrives, the load balancer makes a routing decision, and a healthy backend handles the work. A server drawn on the canvas but not connected to the load balancer contributes no usable capacity.

Clients
Incoming connections and requests
Load balancer
Selects an eligible backend
Backend pool
Healthy application instances
Figure 1. A load balancer is a traffic decision point. It accepts work, selects an eligible backend, and forwards the connection or request.
Load balancing is not the same as autoscaling

A load balancer distributes traffic among instances that already exist. An autoscaler changes the number of instances. Many systems use both, but they solve different problems.

02
Layer 4 vs. Layer 7 load balancing

Choose what the load balancer needs to understand

Layer 4 and Layer 7 refer to layers in the network model. A Layer 4 load balancer routes transport connections using addresses and ports. A Layer 7 load balancer understands an application protocol such as HTTP and can inspect request details.

Layer 4 load balancing
IP + port + transport connection
TCP :443
Connection metadata
Backend
No HTTP inspection

Routes using network and transport information. It is fast and protocol-agnostic, but cannot choose a backend from an HTTP path such as /media.

Layer 7 load balancing
Host + path + method + headers
GET /media
Application request
Media pool
Route-specific backend

Understands application protocols such as HTTP. It can route by hostname, URL path, method, or headers and apply request-level policies.

Figure 2. Layer 4 chooses from connection metadata. Layer 7 can inspect the application request and route different paths to different backend pools.
QuestionLayer 4Layer 7
What can it inspect?IP, port, TCP or UDP connectionHTTP host, path, method, headers
Can it route /api and /media differently?NoYes
Typical useGeneral TCP services, very high throughputHTTP APIs, host and path routing, request policies
03
Load balancing algorithms

Choose the signal that represents backend work

A routing algorithm is the rule used to select a backend. The correct algorithm depends on whether servers are equal, whether request cost varies, and whether current connection count is a useful signal.

Round Robin
ABCABC

Equal request count. Best for similar servers and similar request cost.

Weighted Round Robin
224380

Configured traffic ratio. Best when backend capacities differ predictably.

Least Connections
7
2
5

Current open connection count. Best when connection durations differ.

Figure 3. There is no universally best algorithm. Choose the signal that best approximates current backend work for the workload in front of you.
Round Robin

Cycles through eligible backends. It assumes each request costs roughly the same and each backend has roughly the same capacity.

Weighted Round Robin

Cycles according to configured weights. It is deterministic and appropriate when backend capacities differ in known proportions.

Least Connections

Chooses the backend with the fewest active connections. It reacts to long-lived requests that remain open while short requests finish.

04
Weighted load balancing

Route in proportion to backend capacity

Round Robin sends equal request counts. That is unsafe for a mixed fleet because a Small server and a Large server receive the same traffic despite having different limits. Weighted Round Robin lets the routing share mirror capacity.

BackendRelative capacity and weightAssigned
Small
220 RPS
22
176 RPS
80%
Medium
430 RPS
43
344 RPS
80%
Large
800 RPS
80
640 RPS
80%
Large
800 RPS
80
640 RPS
80%
Figure 4. Weights 22:43:80:80 mirror capacities 220:430:800:800. At 1,800 RPS, every backend receives 80% of its own rated capacity.
Capacity-proportional weight
server weight ∝ server rated capacity
220:430:800:800 becomes 22:43:80:80
Assigned traffic
server RPS = total RPS × server weight ÷ total weight
1,800 × 22 ÷ 225 = 176 RPS for Small
Weights are relative, not percentages

Weights 22, 43, 80, and 80 sum to 225. The load balancer normalizes them into traffic shares. They do not need to sum to 100.

05
Health checks and failover

Stop routing before spare capacity can help

A failed backend must be removed from the eligible pool. Health checks perform this control-loop function by probing each backend and changing its routing state when failures cross a threshold.

Healthy state
Probe succeeds
Backend remains eligible
Traffic continues
Requests may be assigned
Unhealthy state
Probe fails
Failure threshold reached
Removed
Zero new requests sent
Figure 5. Health checks control eligibility. Spare capacity does not help if the load balancer continues sending requests to a failed backend.

Health detection and failover capacity are separate

Health checks prevent requests from reaching a failed server. They do not create replacement capacity. After removal, the remaining servers must still handle the specified failure traffic without crossing their utilization limit.

Normal operation
430
430
430
430
4 × 430 = 1,720 RPS
1,400 RPS demand · 81.4% utilization
One backend removed
430
430
430
430
3 × 430 = 1,290 RPS
1,000 RPS demand · 77.5% utilization
Figure 6. Failover capacity is calculated after removing the failed server. Normal capacity alone does not prove the service survives a failure.
Remaining capacity
normal fleet capacity − failed server capacity
1,720 − 430 = 1,290 RPS
Failover utilization
failure traffic ÷ remaining capacity × 100
1,000 ÷ 1,290 × 100 = 77.5%

What makes a useful health check

  • Representative

    Probe a path that proves the service can perform meaningful work, not only that a process exists.

  • Cheap

    The probe runs frequently, so it must not create significant application or database load.

  • Fast enough

    Detection must be quick enough for the recovery objective without reacting to one harmless transient error.

  • Independent

    Avoid a probe that declares every backend unhealthy because one optional shared dependency is degraded.

06
Connection-aware routing

Request count is not current work

Round Robin equalizes arrivals, not active work. When all requests finish in similar time, that distinction may not matter. When some requests remain open far longer, one backend can accumulate more active work than another even after receiving the same number of requests.

Fast API calls50 ms

Many requests complete quickly, so each connection disappears almost immediately.

Data exports8 seconds

Only a few exports arrive each second, but they remain open long enough to dominate active connection count.

Figure 7. Request rate measures arrivals. Active connections also depend on duration, so equal request counts can still create unequal current work.
Fast-request concurrency
1,800 RPS × 0.05 s = 90 active requests
Export concurrency
15 RPS × 8 s = 120 active exports
Total average concurrency
90 + 120 = 210 open requests
Only 15 of 1,815 arrivals per second are exports, yet exports account for 120 of 210 active requests.

Least Connections uses open connection count as a live approximation of backend work. It does not replace capacity planning. The fleet still needs enough RPS capacity, and connection count is only useful when it correlates with resource usage.

07
Path-based routing and backend pools

Route each workload to dedicated capacity

A Layer 7 routing rule can inspect the URL path and choose a backend pool. This allows one domain to serve multiple workloads while keeping their compute capacity independent.

Layer 7 path rules
/api
2 × Large
1,200 RPS
/media
1 × Large
600 RPS
/admin
1 × Medium
200 RPS
Figure 8. Path-based routing maps each URL path to a dedicated backend pool. Pool isolation prevents one route from consuming another route’s compute budget.

Pool isolation

A backend is dedicated only when it belongs to one pool. Connecting the same application server to two route pools creates shared capacity and allows one route to affect the other. Isolation requires separate membership, separate sizing, and separate utilization checks.

/api capacity
1,200 ÷ 0.75 = 1,600 RPS
2 × Large
/media capacity
600 ÷ 0.75 = 800 RPS
1 × Large
/admin capacity
200 ÷ 0.75 = 266.7 RPS
1 × Medium
Canvas representation

The section exercise represents each path rule as a separate Layer 7 load-balancer node because canvas nodes do not carry editable route labels. In production, one Layer 7 proxy commonly owns several path rules that select several backend pools.

08
Routing design workflow

A repeatable interview method

Separate topology, algorithm, health, and capacity decisions. A correct answer should explain what traffic is being routed, which signal drives selection, which backends are eligible, and what happens when one fails.

  1. 1

    Identify the traffic unit

    State whether the input is requests, connections, jobs, bytes, or sessions. Keep units attached to every calculation.

  2. 2

    Choose Layer 4 or Layer 7

    Use Layer 7 when routing depends on HTTP host, path, method, or headers. Use Layer 4 when transport-level forwarding is sufficient.

  3. 3

    Define backend pools

    List which servers may handle each traffic class. Decide whether capacity is shared or isolated.

  4. 4

    Choose the routing algorithm

    Use Round Robin for a uniform fleet, Weighted Round Robin for known capacity differences, and Least Connections for varying connection duration.

  5. 5

    Calculate normal capacity

    Verify aggregate throughput and every backend's utilization under the selected traffic split.

  6. 6

    Define health and eligibility

    Specify how failed backends are detected and removed from rotation.

  7. 7

    Calculate failure capacity

    Remove the failed backend from the math and verify that the remaining fleet serves the required failover load.

  8. 8

    Check isolation and cost

    Verify that no backend is unintentionally shared and that every load balancer and server fits the monthly budget.

Do not trust aggregate capacity

A bad traffic split can overload one backend while total fleet capacity looks sufficient.

Do not ignore duration

Equal arrival counts do not imply equal active work when requests have different lifetimes.

Do not fake isolation

A backend shared by two pools is still a shared failure and capacity domain.

09
Practice labs

Apply each traffic-routing concept

Each lab isolates one routing decision. Build mode asks you to construct the system from a specification. Fix mode gives you a concrete routing failure and asks for the smallest effective repair.

01

Uneven Fleet

Use Weighted Round Robin to distribute traffic across backends with different capacities.

02

The Dead Server Still Gets Traffic

Enable health checks and prove the remaining fleet can carry failover traffic.

03

Long Requests Break Round Robin

Use Least Connections when active connection duration matters more than arrival count.

04

Route by Workload

Use Layer 7 path routing and dedicated backend pools for independent workloads.

Ready to route traffic

You are ready when you can justify the routing layer, algorithm, health behavior, backend membership, failure capacity, and cost without relying on the diagram alone.

Start section