At the end of part 3, steer could notice a dead backend and route around it. But a backend doesn’t have to be dead to cause trouble. A slow one is worse in a way, because it passes every health check while making a third of your users wait.

So this time I made one backend slow on purpose. Round robin kept giving it exactly a third of the traffic, as expected. Least connections cut its share to about 2%. Then I ran the same test with requests sent one at a time, and least connections did exactly what round robin did. Least connections only helps when requests overlap.

the experiment Link to heading

Three backends on my laptop. The backend on port 8083 sleeps 300 ms before every response:

./bin/backend -port 8083 -delay 300ms

Then 600 requests through steer, 10 at a time, timing each one with curl:

seq 1 600 | xargs -P 10 -I{} curl -s -o /dev/null -w '%{http_code} %{time_total}\n' localhost:8080/r{}

Three runs with each strategy, plus one run of least connections sending a single request at a time:

avgp50p90requests over 250 ms
round robin, 10 at a time (3 runs)101, 101, 102 ms~1 ms302 ms200, 200, 200
least connections, 10 at a time (3 runs)9.3, 5.7, 5.4 ms1–2 ms7.3, 2.6, 2.2 ms12, 8, 8
least connections, 1 at a time101 ms0.8 ms302 ms200

Every request that took over 250 ms was one that went to 8083. Here’s where the requests went:

Stacked bars of where 600 requests went in each run. Round robin sends 200 to each backend in every run, including the slow one. Least connections with 10 requests at a time sends only 8 to 12 to the slow backend and about 295 to each fast one. Least connections with one request at a time goes back to 200 each.

The median barely moves between the strategies, because most requests were always fast. The change is all in the slow third, which is the same lesson as my histograms post: the median would tell you nothing happened here.

how least connections works Link to heading

Each backend keeps a count of the requests it’s handling right now. To pick a backend, steer looks for the one with the lowest count.

A slow backend holds on to every request for 300 ms, so its count goes up and stays up. The fast backends finish in about a millisecond, so their counts keep dropping back to zero. Steer doesn’t know anything about response times. A backend that’s slow ends up looking busy, and busy backends get skipped.

The count is one more atomic number on each backend, like the alive flag from part 3, because every request changes it from its own goroutine:

func (b *Backend) serve(w http.ResponseWriter, r *http.Request) {
	b.active.Add(1)
	defer b.active.Add(-1)
	b.Proxy.ServeHTTP(w, r)
}

The defer is important. It runs however the function exits, including the panic net/http uses to abort a broken response. If the decrement were a plain line after ServeHTTP, one panic would leave a backend’s count stuck one higher forever, and least connections would quietly send it less traffic for the rest of its life.

why one at a time looks like round robin Link to heading

This one surprised me, but the log shows why. Here’s a stretch of the one-at-a-time run:

GET /r1 -> http://localhost:8082 (in flight: 0)
GET /r2 -> http://localhost:8083 (in flight: 0)
GET /r3 -> http://localhost:8081 (in flight: 0)

Every count is zero, every time. When there’s only one request in the system, the previous one has already finished, slow or not. So every pick is a tie, and with nothing to choose between, least connections falls back to taking turns. The slow backend gets its full third.

The count only carries information when requests overlap. At one request at a time there’s nothing to measure. At 10 at a time, the slow backend is almost always in the middle of a 300 ms request, while the fast ones keep emptying out. It only ties for the lowest count in the brief moments when it has just finished one, which is how it still picked up 8 to 12 requests.

This matters outside my laptop too. A service with low traffic, or a load balancer spread across lots of instances that each see a trickle, can be in the “one at a time” world even when the total traffic is high.

the bug: ties aren’t fair either Link to heading

My first version ran the experiment with an uneven split: 372 requests to 8081 and 221 to 8082, two identical fast backends.

To break ties, it scanned the list from a rotating starting point and kept the first backend with the lowest count. That works when everything is idle. But when 8083 was busy and the scan started at 8083, it skipped to the next one in the list, which was always 8081. So 8081 won all of 8083’s ties.

That’s the exact bug from part 3, where skipping a dead backend handed its share to whichever backend came after it, in a new disguise. The fix is the same shape too: collect every backend tied for the lowest count, and take turns among those.

tied := make([]*Backend, 0, len(available))
for i, b := range available {
	if counts[i] == fewest {
		tied = append(tied, b)
	}
}
return tied[n%uint64(len(tied))]

After the fix the two fast backends got 292 and 296, 297 and 295, and 299 and 293. A test now checks the split around a busy backend.

what it still can’t do Link to heading

  • Two requests can pick the same backend. Reading the counts and incrementing one aren’t a single step, so two requests arriving at the same instant can both see the same idle backend. Real load balancers accept this too.
  • A broken backend can win. A backend that answers every request with a 500 in 1 ms always looks idle, so least connections sends it more traffic than the healthy ones. Part 3’s retries only catch connection failures, not error responses.
  • The counts are per steer. Run two copies of steer and each one only knows about its own requests.

things I’m still not sure about Link to heading

  • How much concurrency is enough? I tried 1 and 10. Somewhere in between, least connections starts to win, and I’d like to find where that is.
  • Would picking two at random do just as well? The “power of two choices” picks two backends at random and takes the less busy one, without reading every count. I’d like to compare it on this same setup.
  • Should a fast error count as busy? The fix for the 500 problem is probably to count errors as well as connections, but I don’t know yet what real load balancers do here.

Next is phase 5, metrics, so I can see these counts in a graph instead of reading them out of a log.

The code is at github.com/balakrishnan-vasudevan/steer. The data and the script for the chart are in static/charts/steer-lc/.