System Design · Module 5 · Lesson 3 of 4 · 40 min
Load Balancing Algorithms and Layer 4 versus Layer 7
Choose a balancing policy from request cost and connection behavior, then state which routing decisions require HTTP visibility.
01 The idea
The problem this lesson solves
Round robin over A,B,C sends six equal-cost requests A,B,C,A,B,C.
Prerequisite: earlier lessons introduce the vocabulary and shared examples used here. Follow the module order.
02 Worked example
Follow one worked example
Trace the example in order. Follow the same identities, units and state transitions through the diagram and the events below.
Equal request counts are not necessarily equal work; choose a policy using the signals the balancer can actually observe.
03 The method
Mechanisms decisions and a guided tutorial
Work through the explanation in order, then apply it in the tutorial. Each comparison names a cost as well as a benefit.
| Choice or concept | Purpose | Boundary or cost |
|---|---|---|
| Round robin | Similar-cost requests | Equal counts need not mean equal load |
| Least connections | Varying connection lifetimes | Connections are not equal units of work |
| L4 / L7 | Connection routing / application routing | Choose based on needed protocol visibility |
| Weighted round robin | Different relative capacities | Assumes weights reflect tested budgets |
| Hash affinity | Related requests share an owner | Skew and hot keys remain possible |
Understand the balancer's visibility
Layer 4 balancing uses transport information such as addresses and ports and commonly selects a backend for a connection. Layer 7 balancing understands application information such as HTTP hostnames and paths when it terminates or can otherwise inspect that traffic. An HTTPS L7 proxy normally terminates TLS and can open a separately protected upstream connection. L7 can route /api and /images differently, but has a broader protocol responsibility.
Choose an algorithm for the workload
Round robin cycles through eligible backends and is a useful baseline when requests have similar costs. Weighted round robin gives a larger share to stronger backends. Least connections can help when connection durations vary, but connection count is an imperfect proxy for work, especially with multiplexing. Hash-based routing preserves affinity, but an unlucky distribution or hot key can overload one backend. No algorithm removes the need to measure backend latency and saturation.
Choose the unit being balanced
A layer-4 balancer commonly selects a backend for a transport connection. A layer-7 proxy can route HTTP requests based on parsed fields. Long-lived or multiplexed connections can carry very different amounts of work, so a count of connections is only a proxy for load.
For equal-cost short requests, round robin provides a simple baseline. For heterogeneous machines, weights can allocate different shares. If connection durations vary, least connections can be helpful, but a connection carrying many concurrent streams complicates the signal. Measure backend saturation and latency rather than infer fairness from one counter.
Work a weighted allocation
With weights 1,1,2, the ideal long-run shares are one quarter, one quarter and one half. Out of 800 equal-cost requests that corresponds to 200,200,400. A finite scheduling sequence can differ and request cost can dominate the result.
Hash affinity can keep related requests on one owner, which helps some caches or sessions, but it can also concentrate hot clients. Adding a server may change placement and cache warmth. Treat affinity, balanced work and fast failover as separate properties that need their own guarantees.
TLS visibility determines routing options
A transport proxy forwarding encrypted bytes cannot simply inspect an HTTP path inside that encrypted stream. An L7 HTTPS proxy normally terminates TLS to parse HTTP, then can protect its upstream connection separately. Path-based routing therefore changes both visibility and responsibility.
Document trusted forwarding headers and source identity. If all requests appear to come from the proxy address, a naive per-IP quota may combine every user. Accept forwarded identity only from trusted intermediaries under the deployment’s policy, not arbitrary client-supplied headers.
Guided tutorial
1. Trace round robin
Assign six numbered requests to three healthy backends.
2. Calculate weights
Allocate 800 requests under 1:1:2 ideal shares.
3. Introduce unequal costs
Make one request ten times more expensive and explain the limitation of request counts.
4. Draw TLS boundaries
Show where HTTP becomes visible and which upstream hop is separately protected.
# Equal-cost baseline: A B C A B C
# Weighted 1:1:2 ideal shares: 25%, 25%, 50%
# L4: addresses / ports / connection policy
# L7: parsed application fields such as host and path
# Inspect saturation and latency, not just selected-request totals.04 Cheat sheet
Review the decisions
Use this table to explain the mechanism and its limit without memorizing a product name.
| Choice or concept | Remember | Check |
|---|---|---|
| Round robin | Similar-cost requests | Equal counts need not mean equal load |
| Least connections | Varying connection lifetimes | Connections are not equal units of work |
| L4 / L7 | Connection routing / application routing | Choose based on needed protocol visibility |
| Weighted round robin | Different relative capacities | Assumes weights reflect tested budgets |
| Hash affinity | Related requests share an owner | Skew and hot keys remain possible |
05 Where & why
Check the primary references
Use official references for protocol and implementation details. The questions here are teaching prompts.
06 Interview questions
Explain the reasoning out loud
Answer before opening the explanation. The questions progress from vocabulary to decisions and failure behavior.
What does Round robin mean?
What does Least connections mean?
What does L7 routing mean?
Why can round robin overload one backend?
Can a plain L4 proxy choose by URL path?
Does least connections measure CPU demand?
Why are weights assumptions rather than proof?
Why must forwarded identity be trusted selectively?
07 Practice problems
Practice a new case
Solve the new cases before revealing hints. Complete worked solutions follow the set.
Name the proxy
EasyShow the hint
Round robin
EasyShow the hint
Weighted traffic
MediumShow the hint
Unequal work
MediumShow the hint
Encrypted routing
MediumShow the hint
Policy choice
HardShow the hint
Worked solutions
1. Name the proxy
The corporate browsing proxy is a forward proxy for clients. The ingress proxy is a reverse proxy for the API servers.
2. Round robin
A,B,C,A,B,C,A,B gives A=3, B=3, C=2.
3. Weighted traffic
The total weight is 4. Expected shares are 200,200,400 requests. Short finite scheduling sequences can differ, and equal cost is an assumption.
4. Unequal work
A receives fifty modeled work units and B five. Equal request counts hide a ten-times work difference.
5. Encrypted routing
Use an application-aware proxy that can inspect HTTP at the chosen TLS termination boundary, with a separately defined upstream protection policy.
6. Policy choice
Round robin is a reasonable baseline for similar reads. Connection-aware routing may help streams, but measure active work, backend latency and saturation, including multiplexing and failure behavior before claiming balance.