A Go-based HTTP load balancer that distributes incoming client requests across multiple backend servers. It supports several load balancing algorithms, active and passive health checking, runtime horizontal and vertical scaling, and a built-in monitoring interface with Prometheus metrics. Example backend servers are included to test and demonstrate it.
- Load Balancing Algorithms:
round_robin,weighted_round_robin(smooth, nginx-style),least_connections,randomandip_hash(sticky per client IP). - Health Checking: Backends are probed periodically and taken out of rotation when they fail. A backend that refuses a connection is marked down immediately, and safe requests (
GET/HEAD/OPTIONSwithout a body) are retried on another backend. - Scaling: In
horizontalmode backends can be added/removed at runtime withinmin_servers..max_servers; inverticalmode backend weights can be changed at runtime. - Monitoring: Web dashboard, JSON status API and Prometheus metrics on a separate port.
- Graceful Shutdown: In-flight requests are drained on
SIGINT/SIGTERM. - Example Servers: Simple Go HTTP servers included to simulate and test load balancing.
load-balancer/
├── cmd/
│ └── load-balancer/
│ └── main.go # Entry point for the load balancer
├── internal/
│ ├── balancer/
│ │ ├── balancer.go # Request routing, health checks, scaling operations
│ │ └── algorithms.go # Load balancing algorithms
│ ├── server/
│ │ └── server.go # Backend server (reverse proxy) management
│ ├── monitor/
│ │ ├── metrics.go # Prometheus metrics
│ │ └── web.go # Monitoring web interface and admin API
│ └── config/
│ └── config.go # Configuration loading and validation
├── examples/
│ ├── server1/main.go # Example server 1
│ ├── server2/main.go # Example server 2
│ ├── docker-compose.yml # Docker Compose setup (servers + load balancer)
│ └── load-balancer.compose.yaml # Load balancer config used by Docker Compose
├── configs/
│ └── load-balancer.yaml # Load balancer configuration file
├── deploy/
│ ├── Dockerfile # Multi-stage Dockerfile
│ └── kubernetes.yaml # Kubernetes ConfigMap, Deployment and Services
├── docs/
│ └── README.md # Helper script documentation
├── scripts/ # Build, run, test and load-test scripts
└── tests/
└── balancer_test.go # Integration tests for the load balancer
-
Clone the repository:
git clone https://github.com/Olooce/load-balancer.git cd load-balancer -
Install dependencies:
./scripts/install_deps.sh
-
Build the Load Balancer:
./scripts/build.sh # produces bin/load-balancer
The load balancer is configured using a YAML file, configs/load-balancer.yaml by default (override with -config <path>):
port: 8080
servers:
- address: "http://localhost:8081"
weight: 1
- address: "http://localhost:8082"
weight: 2
load_balancing_algorithm: "round_robin"
scaling:
mode: "horizontal" # options: horizontal, vertical
max_servers: 10
min_servers: 1
health_check:
enabled: true
path: "/health"
interval: 10s
timeout: 2s
monitor:
enabled: true
port: 9090| Key | Description | Default |
|---|---|---|
port |
Port that client traffic is accepted on | 8080 |
servers |
Backend servers with their addresses and weights (weight is used by weighted_round_robin) |
required |
load_balancing_algorithm |
round_robin, weighted_round_robin, least_connections, random or ip_hash |
round_robin |
scaling.mode |
horizontal (add/remove servers at runtime) or vertical (change weights at runtime) |
horizontal |
scaling.min_servers / max_servers |
Bounds on the number of backends | 1 / number of servers |
health_check.enabled |
Enable periodic probes and passive failure detection | false |
health_check.path |
Path probed on each backend; any status below 500 is healthy | /health |
health_check.interval / timeout |
Probe interval and timeout | 10s / 2s |
monitor.enabled / port |
Serve the monitoring interface on this port | false / 9090 |
The configuration is validated at startup; invalid values (unknown algorithm, too many servers, port clash, …) cause a clear error.
The quickest way to run everything locally is:
./scripts/run.shOr start the pieces individually:
go run ./examples/server1 & # listens on :8081 (override with PORT)
go run ./examples/server2 & # listens on :8082
./bin/load-balancer -config configs/load-balancer.yamlThen send requests:
curl http://localhost:8080/helloAlternatively, use Docker Compose (from the repository root):
docker compose -f examples/docker-compose.yml up --buildIf monitoring is enabled, the monitor port (default 9090) serves:
| Endpoint | Description |
|---|---|
GET /monitor |
HTML dashboard with each backend's health, weight, active connections and request count |
GET /metrics |
Prometheus metrics (load_balancer_requests_total, load_balancer_request_duration_seconds, load_balancer_backend_up, load_balancer_no_backend_total) |
GET /api/servers |
Backend status as JSON |
The same port exposes endpoints to change the backend pool at runtime. Changes are not persisted to the configuration file.
# Horizontal mode: add a backend (rejected above max_servers)
curl -X POST localhost:9090/api/servers -d '{"address":"http://localhost:8083","weight":1}'
# Horizontal mode: remove a backend (rejected below min_servers)
curl -X DELETE 'localhost:9090/api/servers?address=http://localhost:8083'
# Vertical mode: change a backend's weight
curl -X PATCH localhost:9090/api/servers -d '{"address":"http://localhost:8082","weight":5}'The monitor port has no authentication. Do not expose it publicly.
Run all unit and integration tests:
./scripts/run_tests.sh # or: go test -race ./...To generate load against a running instance:
./scripts/load_test.sh -n 1000 -u http://localhost:8080docker build -f deploy/Dockerfile -t load-balancer .
docker run -d -p 8080:8080 -p 9090:9090 \
-v "$PWD/configs/load-balancer.yaml:/app/configs/load-balancer.yaml:ro" load-balancerEdit the server addresses in the ConfigMap in deploy/kubernetes.yaml, make the load-balancer:latest image available to your cluster, then:
kubectl apply -f deploy/kubernetes.yamlThis project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.