Index

S2 - Build a Load Balancer

100%Open SVG ↗
S2 · BUILD A LOAD BALANCERreverse proxy · round-robin · health checks · in GoCLIENTS:curlLOAD BALANCER · :8080pick next backend (round-robin):i = (i+1) % len(pool)skip any marked DOWNBACKEND POOL● :9001 UP✗ :9002 DOWN (skipped)● :9003 UPhealth-check loop (goroutine):every 2s → GET /health on each,flip UP/DOWN on the result.passive: also mark DOWN when areal proxied request errors.BACKENDS · your app, N copies:9001UP:9002DOWN:9003UPskippedround-robinONE REQUEST'S JOURNEY1. client hits the LB at :80802. LB picks the next UP backend (round-robin) and skips DOWN ones3. LB reverse-proxies the request → httputil.NewSingleHostReverseProxy4. backend responds → LB streams it straight back to the client5. in parallel, the health loop keeps the UP/DOWN list honestREADME / RESUME LINE“Built a concurrent HTTP loadbalancer in Go: round-robin +active/passive health checks +reverse proxying (net/http/httputil),zero-downtime backend failover.”JARGON, DECODEDreverse proxy = a server that forwards a client's request to a backend and relays the reply; the client never talks to the backend directly.round-robin = hand each new request to the next backend in turn → even spread with zero per-request state.health check = a periodic probe; a backend that fails is marked DOWN and skipped until it passes again (self-healing).active vs passive = active: LB pings on a timer. passive: LB marks a backend DOWN after a real request to it fails.

Lessons that use this board

S2 · BUILD A LOAD BALANCER reverse proxy · round-robin · health checks · in Go CLIENTS :curl LOAD BALANCER · :8080 pick next backend (round-robin): i = (i+1) % len(pool) skip any marked DOWN BACKEND POOL ● :9001 UP ✗ :9002 DOWN (skipped) ● :9003 UP health-check loop (goroutine): every 2s → GET /health on each, flip UP/DOWN on the result. passive: also mark DOWN when a real proxied request errors. BACKENDS · your app, N copies :9001 UP :9002 DOWN :9003 UP skipped round-robin ONE REQUEST'S JOURNEY 1. client hits the LB at :8080 2. LB picks the next UP backend (round-robin) and skips DOWN ones 3. LB reverse-proxies the request → httputil.NewSingleHostReverseProxy 4. backend responds → LB streams it straight back to the client 5. in parallel, the health loop keeps the UP/DOWN list honest README / RESUME LINE “Built a concurrent HTTP load balancer in Go: round-robin + active/passive health checks + reverse proxying (net/http/httputil), zero-downtime backend failover.” JARGON, DECODED reverse proxy = a server that forwards a client's request to a backend and relays the reply; the client never talks to the backend directly. round-robin = hand each new request to the next backend in turn → even spread with zero per-request state. health check = a periodic probe; a backend that fails is marked DOWN and skipped until it passes again (self-healing). active vs passive = active: LB pings on a timer. passive: LB marks a backend DOWN after a real request to it fails.

Saved in this browser only.

← S1 - Build Your Own DatabaseS3 - Build a Redis Server →