Reverse proxy load balancer in Go with active health checks, health-aware routing, and passive failure detection.
The system routes requests across multiple backends, avoids unhealthy servers, and updates backend health dynamically based on both periodic checks and real request failures.
I wanted to build a small but realistic systems component that models how production load balancers handle routing, health checks, and failure scenarios.
This project focuses on correctness, failure handling, and state management rather than just request forwarding.
- Reverse proxy forwarding to upstream backends
- Round-robin load balancing
- Active health checks (
/healthendpoint) - Health-aware backend selection
- Passive failure detection (mark backend unhealthy on proxy errors)
- Fail-fast behavior using upstream timeouts
- Returns
503 Service Unavailablewhen no backends are healthy - Backend abstraction via interface
Client → Load Balancer → Backend Servers
cmd/loadbalancer/ # load balancer entrypoint
cmd/backend/ # simple backend server for testing
internal/balancer/ # load balancing logic
internal/server/ # backend abstraction and proxying
scripts/ # load and failure test helpers
- The load balancer maintains a set of backend servers and their health state.
- An initial synchronous health check ensures correct routing at startup.
- A background goroutine continuously updates backend health.
- Each request selects the next healthy backend using round-robin.
- Requests are proxied using
httputil.ReverseProxy. - If a backend fails during request forwarding, it is marked unhealthy (passive check).
- If no healthy backends are available, a
503response is returned.
Run multiple backend instances:
PORT=8081 go run ./cmd/backend
PORT=8082 go run ./cmd/backend
PORT=8083 go run ./cmd/backendEach backend exposes:
- / → sample response
- /health → health check endpoint
go run ./cmd/loadbalancercurl http://localhost:8080Requests should be distributed across backends
This project includes both unit tests and integration tests to validate correctness and failover behavior.
From the project root directory:
go test ./... -v- Round-robin load balancing correctness
- Skipping unhealthy backends
- Behavior when no backends are available
- Proxy failure handling and failover across requests
Basic load testing can be performed using hey:
hey -n 1000 -c 10 http://localhost:8080This simulates concurrent traffic and helps evaluate:
- requests per second (RPS)
- latency distribution (p50, p95, p99)
- error rates under load
This project focuses on correctness and failure handling rather than raw throughput. Load testing is used to validate behavior under concurrent traffic.
- All backends healthy → baseline throughput and latency
- One backend down → verify failover behavior
- All backends down → expect
503 Service Unavailable
Mac:
brew install heyLinux:
go install github.com/rakyll/hey@latestWindows (PowerShell):
go install github.com/rakyll/hey@latestMake sure your Go bin directory is in your PATH after using go install.
This project includes scripts to simulate backend failure during active load.
-
scripts/run_failover_test.sh
Starts local backends and the load balancer, schedules backend termination, and runs a timed load test. -
scripts/kill_backend_after.sh
Terminates a backend listener on a given port after a configured delay.
./scripts/run_failover_test.sh 8083 2 5s 100You can also terminate a backend process directly after a delay:
./scripts/kill_backend_after.sh 8083 2This will:
- wait for 2 seconds
- find any process listening on port
8083 - terminate it gracefully
Useful for testing failure handling without running a full load test.
This project exposes Prometheus metrics to help analyze system behavior under load and failure.
Metrics are available at:
http://localhost:8080/metrics-
lb_requests_total
Total requests handled by the load balancer, labeled by method, path, status, and backend. -
lb_request_duration_seconds
Request latency histogram for measuring p50, p95, and p99. -
lb_proxy_errors_total
Total proxy errors per backend. -
lb_backend_health
Backend health status (1 = healthy, 0 = unhealthy).
Start Prometheus using Docker:
docker compose upThen open:
http://localhost:9090
Requests per second (RPS):
rate(lb_requests_total[10s])
Per-backend traffic distribution:
sum by (backend) (rate(lb_requests_total[10s]))
Backend health:
lb_backend_health
- Traffic distribution across backends
- Failover behavior when a backend goes down
- Error spikes during failure windows
- Latency changes under load
Observability is used here to validate correctness, understand system behavior, and reason about performance under failure scenarios.
- No retry logic for failed requests (fail-fast on upstream failure)
- No safe retry for idempotent requests
- No rate limiting or backpressure control
- No observability (metrics, tracing, structured logs)
- Static backend configuration (hardcoded)
- Safe retry for idempotent requests (e.g., GET)
- Config-driven backend management (JSON/env)
- Per-backend concurrency limits (bulkheading)
- Metrics (request count, latency, error rates)
- Additional load balancing strategies (least connections, weighted)