knaxuslearn · build · ship

Projects / Load balancer / Wrap up

Wrap up

Keep going

Pick one when you want more:

  • Weights. Give bigger servers a larger share, and make round robin and least connections respect it.
  • Sticky sessions. Send each client to the same backend, by cookie or by a hash of the client address, and see what happens when that backend dies.
  • A circuit breaker. After enough failures in a row, stop trying a backend for a while instead of paying for every failed attempt.
  • TLS. Terminate HTTPS at the proxy and talk plain HTTP to the backends, then try encrypting the second leg as well.
  • Slow start. When a backend comes back, give it a growing share of traffic instead of a full one at once.
  • Metrics. Turn the status page into counters and latency histograms that a monitoring tool can scrape.

What this version leaves out, and what a real load balancer does

We kept the ideas and dropped the weight.

  • Real balancers such as nginx, HAProxy and Envoy are built to handle tens of thousands of connections each, with careful memory use. Ours leans on the standard library, which is fine for learning.
  • Real health checks can use many protocols, set per-backend rules, and look at error rates and response times, not only a yes or no from /health.
  • Our retry is once, per request. Real systems add a retry budget, a cap on retries as a share of traffic, so a bad minute cannot become a retry storm.
  • Least connections counts only the requests this proxy sent. With several proxies in front of the same backends, each sees only its own share.
  • We reload by signal and a file. Real systems often push configuration through an API, and validate it before switching over.
  • The status page has no login. Real ones are private, and part of a wider monitoring setup.

Check yourself

Answer each question in your head first, then open it to check.

  1. Why does the round-robin counter need to be an atomic, and what could go wrong with a plain number?
    Many requests run in separate goroutines at the same moment. With a plain number, two could read the same value and choose the same backend, or an update could be lost. An atomic does the read and the increment as one indivisible step.
  2. Why is marking a backend down after one failed health check a worse default than two in a row?
    One failed check can be a blip: a slow response, a garbage collection pause, a dropped packet. Reacting to it takes a healthy server out of rotation and moves its load onto the others, and a flaky one then flaps in and out. Two in a row filters the blips, at the price of noticing a real failure a second later.
  3. Why do we retry a GET but not a POST, even when the backend could not be reached?
    A GET is idempotent, so doing it twice changes nothing. A POST may have been applied before the connection dropped, so a retry could do the work twice, and its body has usually been consumed already.
  4. In Step 3.4, why did least connections cut throughput problems but leave p99 almost unchanged?
    Least connections sends almost nothing to the slow backend, so most requests are fast and total throughput rises. A few requests still land on it, and p99 measures the slowest 1 in 100, so those few set the number.
  5. Teach it back: explain to a teammate why ListenAndServe returning does not mean the server has finished shutting down.
    ListenAndServe returns as soon as Shutdown starts, to tell you the listener is closed. The shutdown itself, waiting for the running requests, continues in the goroutine that called Shutdown. If main exits right away, the process ends and takes the unfinished requests with it, so main has to wait for Shutdown to return.

Sources