Course contents

03. Scaling and Load Balancing Lesson 5 of 8

Weighted Round Robin

Derive weights from capacity for a mixed fleet, see vCPU-based weights misfire, plan N+1 around the largest server and ramp returning servers with slow start.

14 minIntermediate10 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • Capacity is workers ÷ time per request; weights should be proportional to it.
  • With capacity weights every server runs at the same utilisation, so the whole fleet’s capacity is usable.
  • Spec-sheet weights (vCPUs, memory) miss per-core speed and code changes; measure throughput instead.
  • Losing the largest server removes the most capacity; plan headroom for that case.
  • Returning servers are cold: ramp their weight up (slow start) instead of restoring it at once.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

Weighted round robin (WRR) is round robin where each backend has a weight. Over a cycle, each backend receives requests in proportion to its weight: with weights 1 : 4 : 2, one, four and two of every seven requests. The weights are configuration; the balancer itself still looks at nothing at run time.

This lesson assumes the Round Robin lesson, which introduces weights and the smooth variant. Here the focus is on where weights come from, how they go wrong, and how they interact with failures and restarts.

Why it exists

Fleets are rarely uniform. Instance generations change, a migration leaves old and new machines side by side, one pool runs on larger hosts, spot capacity comes in odd sizes. Plain round robin gives every backend the same count, so the smallest one saturates first: the usable capacity of the fleet becomes number of servers × smallest server. Weights let each server take a share proportional to what it can do.

How it works

Capacity. For a request/response service, a server’s throughput is roughly workers ÷ time per request (Little’s law): 16 workers at 50 ms is about 320 req/s; 4 workers at 25 ms is about 160 req/s. Weights proportional to these numbers make every server run at the same utilisation.

Smooth selection. Naive WRR serves each backend its weight in a block (four in a row to the big one). Smooth WRR (nginx’s algorithm, also used in the simulation) keeps a running score per backend: each pick adds every backend’s weight to its score, picks the highest, and subtracts the total weight from the winner. With 1 : 4 : 2 the cycle is L, F, L, S, L, F, L: exact proportions per cycle and no back-to-back picks of the heavy server.

Failures. When a backend is ejected by health checks, the others keep their relative weights. Its share is spread over the survivors in their ratio, so the biggest survivors absorb the most.

Slow start. Many balancers can ramp a newly healthy backend’s weight from a small value to its configured weight over a window (for example HAProxy’s slowstart, nginx Plus’s slow_start, Envoy’s slow-start mode, AWS ALB’s slow-start duration). Pre-warming the instance before registering it is the alternative.

When to use it

  • Mixed instance types or generations behind one balancer, with stable and known capacities.
  • Gradual traffic shifting: canary releases (weight 1 of 100 for the new version) and blue/green cut-overs use the same mechanism.
  • Weighted DNS records or weighted target groups when splitting traffic across regions or clusters.
  • Requests are short and similar in cost, so counts are a good proxy for work.

When not to

  • Capacities change at run time (autoscaled mixed instances, noisy neighbours, degraded hosts): a load-aware algorithm adapts on its own.
  • Request costs vary widely: equal counts in proportion to weight still pile heavy requests onto one server; use least outstanding requests (weighted, if capacities differ).
  • Stateful affinity is needed (caches, sessions): use hashing, possibly with weights on the ring.

Trade-offs

Choice You gain You pay
Weights from measured throughput Equal utilisation, full fleet capacity Load tests or per-backend metrics; must be redone after changes
Weights from specs (vCPU, memory) Easy to set Wrong whenever per-core speed, code or request mix differ
Slow start No error spike from cold servers Survivors carry more for longer; must have headroom
Weighted least connections instead Adapts to slow servers and heavy requests Needs in-flight tracking; weaker with many balancers that each see only part of the load

Alternatives

  • Weighted least connections / least outstanding requests: divides in-flight requests by weight and picks the lowest; reacts to slowness.
  • Power of two choices (weighted): sample two backends with probability proportional to weight, pick the less loaded one; scales to many balancers.
  • Adaptive weights: some proxies adjust weights from observed latency or error rate (for example Envoy’s outlier detection, or client-side load reports in gRPC’s weighted round robin policy).
  • Homogeneous pools: keep one instance type per pool and balance pools separately; simplest when you control the fleet.

Failure modes

  • Stale weights: a code change makes one instance type relatively slower; its weight now over-feeds it and P95 rises with no failure anywhere.
  • Largest server fails: its share lands on smaller machines that cannot carry it; a fleet sized for “any one server” must use the largest one in the N+1 calculation.
  • Cold return at full weight: a restarted server passes liveness checks and is immediately given its full share while caches and JIT are cold, producing a second error spike.
  • Degraded but alive: health checks pass, the weight stays, the slow server keeps receiving its share (see the Round Robin lesson).
  • Weights across balancers: each balancer instance runs its own smooth cycle; proportions hold per balancer, not globally per instant.

In production

Derive weights from per-backend throughput at your latency target, measured by load test or from production metrics, and store them with the instance type, not by hand per host. Alert on per-backend utilisation and latency skew: with correct weights, utilisation should be roughly equal across backends. Use slow start or pre-warming for services with caches or JIT warm-up. When capacity plans assume a mixed fleet, compute headroom with the largest backend removed. For canaries, use weights to send a small, fixed fraction to the new version and widen it gradually.

Common misconceptions

  • “Weight = number of cores.” Cores differ in speed across generations, and the bottleneck may be memory, I/O or a downstream call. Measure.
  • “Weighted round robin balances latency.” It balances utilisation if the weights are right. A server with a faster CPU still answers faster; a server with more but slower workers answers slower at the same utilisation.
  • “Healthy means ready for full load.” A health check says the process answers, not that it is warm.
  • “Weights fix slow servers.” Only if someone updates them. Static weights cannot see run-time changes.

Interview angle

When an interviewer mentions mixed hardware, a migration or a canary, bring up weights and say where the numbers come from (measured throughput, not specs). Then cover the failure cases: what happens when the largest node fails (N+1 around the biggest), how restarted nodes are ramped (slow start), and when you would switch to a load-aware algorithm. Doing the arithmetic out loud (“80 + 320 + 160 = 560 req/s; without the 320 node we have 240, so peak must stay below that or we add capacity”) shows real capacity thinking.

Model assumptions

The simulation is a simplified model, not a benchmark. Small has 4 workers at 50 ms mean service time (about 80 req/s), Large 16 workers at 50 ms (about 320 req/s), Fast 4 workers at 25 ms (about 160 req/s); service times are lognormal with cv 0.4 and each server queues up to 16 requests before rejecting. Arrivals are Poisson. The balancer uses smooth weighted round robin; health checks probe every 250 ms, with 2 failures to eject and 2 successes to restore. “Cold” is modelled as half speed for the first seconds after a restart, and slow start as a manual weight change from 2 to 4; real balancers ramp continuously over a configured window. Utilisation is the fraction of busy workers, not CPU. All numbers are illustrative.

The lesson as textEvery step of the simulation, written out.
  1. 1. Three servers, three different capacities

    Real fleets are rarely uniform. Here: a Small box (4 workers, 50 ms per request, about 80 req/s), a Large box (16 workers, 50 ms, about 320 req/s) and a Fast box with only 4 workers but a newer CPU (25 ms, about 160 req/s).

    The balancer uses plain round robin. At 150 requests per second each server gets 50, which is fine for all of them, but look at the load: Small is about 60% busy, Fast about 30%, Large under 20%. Equal counts, very unequal utilisation.

    Scene: Round robin gives Small, Large and Fast 50 requests per second each. Small is about 60% busy, Fast about 30%, Large under 20%. Nothing fails.

    Key idea: Capacity = workers ÷ time per request. Round robin ignores it.

  2. 2. The smallest server sets the limit

    Traffic grows to 300 requests per second, about half of the fleet’s 560. Round robin still sends each server 100. Small can do about 80, so it saturates, fills its queue and rejects requests, while Large sits about a third busy.

    With round robin, the fleet’s usable capacity is three times the smallest server: about 240 req/s, not 560.

    Scene: At 300 requests per second round robin gives each server 100. Small is saturated and rejects requests; Large is about a third busy and Fast about 60%.

    Key idea: Under round robin the weakest server overloads first, long before the fleet is full.

  3. 3. Weights proportional to capacity

    Set each weight to the server’s capacity: 80 : 320 : 160, which reduces to 1 : 4 : 2. Out of every seven requests, Small gets one, Large four and Fast two.

    At 300 req/s that is about 43, 171 and 86 per second, and every server ends up roughly 54% busy: the same utilisation everywhere. The rejections stop. The fleet can now be pushed towards its real 560 req/s.

    Scene: With weights 1, 4 and 2 the balancer sends Small, Large and Fast about 43, 171 and 86 requests per second. All three are about 54% busy and nothing fails.

    Key idea: Weight = capacity. Then every server runs at the same utilisation and the fleet saturates all at once, not one box at a time.

  4. 4. Large never gets two in a row

    The balancer uses smooth weighted round robin (see the Round Robin lesson): each pick, every server adds its weight to a running score, the highest score wins and pays back the total (7).

    With 1 : 4 : 2 the cycle is Large, Fast, Large, Small, Large, Fast, Large. Large gets four of every seven requests but never two in a row, so no server sees bursts. Any seven consecutive picks contain exactly 4, 2 and 1.

    Scene: The balancer repeats the order Large, Fast, Large, Small, Large, Fast, Large. Large receives four of every seven requests, never two in a row.

    Key idea: Smooth WRR keeps exact proportions over every cycle and interleaves the turns.

  5. 5. Weights from the spec sheet

    A teammate sets weights from vCPU counts: Small and Fast both have 4, Large 16, so 1 : 4 : 1. That ignores that Fast’s newer CPU does each request in half the time.

    Traffic grows to 450 req/s (80% of capacity). Fast gets only a sixth, 75 req/s, and is only about half busy, while Small and Large get about 94% of their capacity each. Queues build and P95 climbs, although the fleet as a whole has 20% headroom.

    Scene: With weights 1, 4 and 1 at 450 requests per second, Small and Large are nearly saturated and queueing, while Fast is only about half busy. P95 latency is high.

    Key idea: Hardware specs are a guess. Weights must match measured throughput, or one group of servers runs hot while another idles.

  6. 6. Tune weights from measurements

    Measure each server’s throughput at a fixed latency target (a load test, or production metrics per backend) and set weights from that. Fast measures at twice Small, so the weights go back to 1 : 4 : 2.

    At the same 450 req/s every server is now about 80% busy, P95 drops, and the fleet has its full headroom again. Re-check weights whenever the instance mix, the code or the request mix changes.

    Scene: With weights 1, 4 and 2 at 450 requests per second all three servers are about 80% busy and P95 latency is lower than with vCPU-based weights.

    Key idea: Derive weights from measured per-server throughput, and re-measure when anything changes.

  7. 7. Losing the largest server

    Traffic eases to 360 req/s. One second in, Large crashes. After two failed health checks the balancer ejects it and spreads its share over the survivors in their weight ratio, 1 : 2.

    But Large was 57% of the fleet. Small and Fast together do about 240 req/s, far less than 360: both saturate and about a third of requests are rejected. With unequal servers, N+1 planning must assume the biggest one fails.

    Scene: Large crashes and is ejected after two failed health checks. Small and Fast receive all 360 requests per second, more than their combined 240, so both saturate and many requests are rejected.

    Key idea: In a mixed fleet, plan headroom for losing the largest server, not an average one.

  8. 8. Back at full weight, but cold

    Large restarts half a second in. A fresh process is slow at first: empty caches, cold connection pools, code not yet optimised by the JIT. Here it runs at half speed for the first few seconds.

    As soon as two health checks pass, the balancer gives it its full weight again: 4/7 of 360, about 206 req/s, but cold it can only do about 160. Large saturates and rejects requests right after "recovering", until it warms up.

    Scene: Large comes back at half speed and immediately receives its full share of about 206 requests per second. It saturates and rejects requests until it is warm.

    Key idea: Passing a health check means alive, not warm. Full weight on a cold server causes a second wave of errors.

  9. 9. Slow start: ramp the weight

    Replay the restart with slow start: the balancer brings Large back at weight 2 instead of 4 and raises it once the server is warm (many balancers ramp weight linearly over a configured window, e.g. 30–60 s).

    At weight 2 Large gets 2/5 of 360, about 144 req/s, which fits its cold capacity; Small and Fast run hot but cope. Far fewer requests are rejected than with full weight. At four seconds it is warm and goes back to weight 4.

    Scene: Large comes back cold at weight 2 and receives about 144 requests per second, which it can handle. Few requests fail. Once warm, its weight returns to 4.

    Key idea: Ramp a returning server’s weight up over a warm-up window, and only if the others have room for the difference.

  10. 10. Weights are configuration you must maintain

    Weighted round robin is cheap, predictable and stateless per request: a good fit for a mixed fleet with stable, known capacities and similar requests. Derive weights from measured throughput, plan N+1 around the largest server and ramp returning servers.

    What it cannot do is notice change: a degraded server, a noisy neighbour, a request mix that shifts. For that, combine weights with a load-aware algorithm (weighted least connections, or least outstanding requests) and per-backend latency alerts.

    Scene: Small, Large and Fast are healthy behind weighted round robin with weights 1, 4 and 2 at 360 requests per second; all are about 64% busy.

    Key idea: Static weights encode what you believe about capacity. Keep the belief true, or let the balancer measure load.