awesome-everything RU
↑ Back to the climb

Networking & Protocols

Proxy and load balancing: config and code reading

Crux Read real LB configs, a balancing snippet, a PROXY-protocol header, and a graceful-shutdown handler — then pick the behaviour or the highest-leverage fix.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at senior altitude — in orbit
◷ 14 min

LB behaviour is decided in config files and shutdown handlers, not in slide diagrams. Read each snippet, predict what it does under load, and choose the fix a senior engineer would make first.

Goal

Practise the loop you run on every LB incident: read the upstream config or the handler, predict the failure mode, and reach for the change that actually fixes it.

Snippet 1 — the nginx upstream block

upstream api {
    server 10.0.0.1:8080;
    server 10.0.0.2:8080;
    server 10.0.0.3:8080;
}

server {
    location / {
        proxy_pass http://api;
    }
}
Quiz

Request cost varies widely and one backend periodically GC-pauses. With this default config, what happens, and what is the highest-leverage one-line change?

Snippet 2 — power-of-two-choices, by hand

import random

def pick_backend(backends):
    a, b = random.sample(backends, 2)        # two distinct, uniform at random
    return a if a.active_conns <= b.active_conns else b
Quiz

This is the core of power-of-two-choices. Why does sampling exactly two — rather than scanning all N for the true minimum — matter in production?

Snippet 3 — the PROXY protocol header

PROXY TCP4 203.0.113.195 198.51.100.7 56324 443\r\n
GET /orders HTTP/1.1
Host: shop.example.com
Quiz

A raw-TCP backend (not HTTP) needs the real client IP for rate limiting. The LB prepends the line above. Which statement is correct?

Snippet 4 — graceful shutdown on SIGTERM

func main() {
    srv := &http.Server{Addr: ":8080", Handler: mux}
    go srv.ListenAndServe()

    <-sigterm                                  // LB has marked us draining
    ctx, cancel := context.WithTimeout(context.Background(), 25*time.Second)
    defer cancel()
    srv.Shutdown(ctx)                          // stop accepting, finish in-flight
}
Quiz

This handler cooperates with connection draining. What does srv.Shutdown do, and what is the one production gap to watch?

Recap

LB problems live in config and handlers. The nginx default is round-robin, so a load-aware policy like least_conn (or P2C upstream) is the fix for a load-blind pool. Power-of-two-choices is two random samples plus one comparison — O(1) and herd-desynchronizing, not merely a cheaper least-connections. The PROXY protocol preamble restores the client IP for any transport-layer protocol but must be trusted only from known proxy IPs. And graceful shutdown (close-listener, drain in-flight, timeout) only works when the app’s drain window and the LB’s drain timeout are sized together for the longest-lived connection.

Continue the climb ↑Proxy and load balancing: survive a failing backend
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources3
expand
  1. 01
  2. 02
  3. 03

Trademarks belong to their respective owners. Editorial reference only.