StoicBuilder

HandbookXIII the AqueductLoad balancing

XIIIthe Aqueduct, Scale it

Load balancing

1 min readUpdated 28 November

In plain words

A load balancer sits in front of several copies of your app and passes each request to a healthy one, so traffic is shared and one failing server doesn't take the site down.

Where you see it

Harbor's API runs as six containers behind an AWS Application Load Balancer. Every few seconds it calls /healthz on each one, and when a container stops answering, the balancer stops sending it traffic until it recovers.

services/api/server.tsserver
let shuttingDown = false;process.on("SIGTERM", () => { shuttingDown = true; // fail health checks first setTimeout(() => server.close(), 15_000); // then finish in-flight requests}); app.get("/healthz", (req, res) => shuttingDown ? res.status(503).send("draining") : res.send("ok"));
On a deploy, the instance fails its health check, the balancer stops sending it new requests, and it finishes the ones in flight before closing.

What goes wrong

  1. Health checks that test the whole world

    If every instance's check queries the shared database, one database blip fails them all at once and the balancer has no healthy instance left to choose. Check what belongs to the instance, and watch shared dependencies separately.

  2. Deploys that cut requests off mid-flight

    Killing an instance while it's still in rotation drops whatever it was serving. Fail the health check, wait for the balancer to drain it, then shut down.

  3. Taking turns with uneven work

    Round robin sends the next request to the next server even if it's busy with three slow exports. A least-connections or least-outstanding-requests setting, which most balancers offer, sends new work to the least busy instance.

What to tell your agent

Copy this into Lovable, Bolt, Cursor or any agent
Add a /healthz endpoint that checks only this instance's own state, and handle SIGTERM by failing the health check, waiting for the load balancer to drain connections, then closing the server. Tell me which balancing algorithm and health check interval we use, and whether they suit our slow routes.

Quick check

Sign in to earn +10 XP for it

Harbor's database has a 20-second hiccup, and the load balancer marks every API instance unhealthy at once. What caused that?

Got a prompt that touches load balancing?

Paste it. You get a better prompt first, then the cards that explain it.

Check your own prompt

Free, no sign-up.