Understand the building blocks / CHAPTER 10
Load balancing algorithms
Choose a routing policy using request cost, connection lifetime, and placement needs.
Builds on Load balancing.
- Compare round robin, least connections, weighted routing, and hashing.
- Explain why request count can differ from actual load.
- Select a policy and define its failure fallback.
01First decide what “less busy” means
A new download request reaches a balancer. Servers A, B, and C are ready, but which should receive it? Count connections, measure transferred bytes, or observe processor use and you may get different answers. Start by asking what work the request needs.
Use this measured snapshot. MB/s means decimal megabytes transferred per second. Capacity weights are operator-supplied estimates based on tests; they are not current load measurements.
| Server | Capacity weight | Active connections | Current MB/s | Response time | CPU busy | Queued jobs |
|---|---|---|---|---|---|---|
| A | 4 | 8 | 80 | 40 ms | 70% | 8 |
| B | 2 | 6 | 20 | 20 ms | 50% | 2 |
| C | 1 | 3 | 60 | 60 ms | 20% | 10 |
Read the numbers before choosing a rule:
- C has the fewest connections, but those connections may carry large downloads.
- B transfers the fewest bytes and has the lowest measured response time.
- A has more connections, but testing assigns it more capacity.
- C uses little CPU while ten jobs wait. It might be blocked on another service, so low CPU alone does not prove it can finish new jobs quickly.
A balancing algorithm is the rule that turns such information into a destination. First exclude unready servers; then apply the rule to those still eligible.
02Nine policies, with actual choices
The table applies nine common rules to the same snapshot. Product implementations may use different measurements or tie-breaking, so state the calculation you mean.
| Algorithm | How it chooses | Example and limitation |
|---|---|---|
| Round robin | Select A, then B, then C, and repeat. | Twelve comparable arrivals give four each. Equal counts can overload a smaller server. |
| Weighted round robin | Assign request shares in proportion to tested capacity weights. | Weights 4:2:1 give eight, four, and two of fourteen arrivals. Static weights do not follow a sudden slowdown. |
| Least connections | Select the server with the fewest active connections. | C has three. One connection can carry much more work than another. |
| Weighted least connections | In this simplified rule, divide active connections by capacity weight and select the smallest result. | A: 8/4=2; B: 6/2=3; C: 3/1=3. Choose A. Connections must still be comparable measures of work. |
| Least response time | Favor the server with the lowest recent measured response time. | B has 20 ms. An old or unusually fast sample may mislead. |
| Least bandwidth | Select the lowest measured rate of transferred bytes. | B has 20 MB/s. This says little about its CPU capacity. |
| IP hash | Apply a repeatable calculation to the client address and map the result to a server. | The same address usually gets the same choice while the mapping remains unchanged. Many people may share that address. |
| Random | Choose independently, with equal probability among eligible servers. | Long-run shares tend toward equality. Twelve arrivals need not split four each. |
| Custom load | Select using an explicitly defined application measurement. | Fewest queued jobs chooses B. Lowest CPU chooses C. Choose the measurement that reflects the incoming work. |
Separate the two weighted policies:
- Weighted round robin divides arrivals according to capacity shares. For fourteen arrivals, the total weight is seven; each weight unit receives two requests.
- Weighted least connections checks the current connection count as well as capacity. It can choose a different next server even with unchanged weights.
Measurements need interpretation:
- A 20 MB/s flow fills a 20 MB/s link but uses only one fifth of a 100 MB/s link. Compare traffic with link capacity when links differ.
- Combine recent response-time samples over a stated window rather than reacting entirely to one sample. This is smoothing. Some products also consider active work.
- Define the maximum age of a report, behavior when it is missing, and how a recovered server gets enough trial traffic to be measured again.
03Affinity, feedback, and a practical selection
Consider two office users who share one public network address. A network device translates their private addresses to that shared public address; this is network address translation, or NAT. An IP-hash rule sees the shared address rather than their separate account identities.
A toy mapping makes the effect visible:
- Number the servers
A=0,B=1, andC=2. - Suppose a repeatable calculation on the address produces 8.
- Divide by the server count and take the remainder:
8 mod 3 = 2. Select C. - Both office users therefore reach C. Their traffic can concentrate there.
- If adding D changes this simple rule to
8 mod 4 = 0, the same address now reaches A. Other implementations can use different mapping rules.
Address affinity has further limits:
- A mobile user’s address can change. A proxy may hide the original address.
- Accept a forwarded address only from a configured proxy that removes caller-supplied fakes.
- An address does not identify a person or guarantee the nearest region.
- If C fails, another server needs recoverable session data. Repeated routing to C did not copy C’s memory elsewhere.
Choose and operate a policy in steps:
- Identify the constrained resource. Separate long downloads from CPU-heavy reports when they interfere.
- Pick a signal that reflects that resource. For a job service, count waiting jobs only if those jobs have sufficiently comparable costs.
- Record when each load report was measured. For example, with one report per second, discard reports older than two seconds and fall back to weighted round robin across ready servers while keeping concurrency limits. Shift traffic gradually and check whether response times rise after adding that work.
- Limit work admitted when all servers are full. Choosing the least busy overloaded server does not create spare capacity.
Two extensions are worth recognizing. Least outstanding requests counts unfinished requests rather than connections, which matters when one connection carries many requests. Power of two choices samples two eligible servers and chooses the less loaded of those two. Object-key routing can improve cache reuse, but one extremely popular object can still overload its assigned server; Chapter 15 explains stable placement as membership changes.
Raw and normalized connections select different servers
Follow the numbered arrows
- Next request → Least connectionsuse raw count
- Next request → Weighted least connectionsuse capacity-normalized count
- Least connections → Choose C3 is smallest
- Weighted least connections → Choose A2 is smallest
Trace the diagram in words
- Compare the current connection counts: A has eight, B six, and C three.
- The raw-count rule selects C.
- Divide those counts by weights four, two, and one.
- The resulting counts per capacity unit are two, three, and three, so the weighted rule selects A.
DRAW. CALCULATE. EXPLAIN.
Compare policies on one snapshot
20 minutes · guided practice
For ready servers A, B, C, use these measurements:
| Server | Weight | Connections | MB/s | Response time |
|---|---|---|---|---|
| A | 3 | 12 | 90 | 30 ms |
| B | 1 | 5 | 10 | 50 ms |
| C | 2 | 6 | 40 | 20 ms |
- Select the next server for least connections, weighted least connections, least bandwidth, and least response time.
- Divide twelve comparable arrivals by weighted round robin.
- Explain one reason the selected server might still be unsuitable.
Compare the worked solution
- Least connections chooses B: five is smaller than twelve or six.
- Weighted least connections compares
12/3=4,5/1=5, and6/2=3. It chooses C. - Least bandwidth chooses B at 10 MB/s. Least response time chooses C at 20 ms.
- Weighted round robin uses total weight
3+1+2=6. Twelve arrivals give two per weight unit: A receives six, B two, and C four. - These are rule outputs, not guaranteed fastest results. One connection may carry a large download, measurements may be old, or the tested capacity may not match today’s work. Check those assumptions and limit new work if all servers are full.
The checkboxes record your own assessment. The worked solution gives you a way to check the reasoning.
RETRIEVE BEFORE YOU REVEAL
Keep the important ideas.
Say the answer aloud, then turn the card over.
CARD 01How do the nine policies differ?
- Round robin and weighted round robin divide arrivals equally or by capacity shares.
- Connections, response time, bandwidth, and custom load use different observations.
- IP hash repeats an address-based choice.
- Random makes independent choices.
First ask which resource limits the incoming work.
CARD 02What does weighted least connections compare?
- Divide each active-connection count by its capacity weight.
- For A, B, C: 8/4=2, 6/2=3, and 3/1=3.
- Choose A, the smallest result.
This works only as well as connections and tested weights represent the work.
CARD 03What does IP affinity fail to guarantee?
- IP hash selects from an address, not an account identity.
- Many office users can share one address and one destination.
- Addresses and server membership can change the mapping.
- Failure recovery still requires session data saved beyond the chosen server.
CHECK THE REASONING
Can you explain the choice?
Answer every question, then finish for an immediate score and an explanation for each answer. Aim for 80%, then revisit any gaps.
CLOSE THE LOOP
Explain it without the page.
Summarise the mechanism, one trade-off, and one failure mode. Then mark this chapter complete.