A high-performance, production-ready HTTP load balancer written in Go. Features multiple load balancing strategies, health checking, metrics, and more.
-
Multiple Load Balancing Strategies
- Round Robin
- Least Connections
- Random
- Weighted Round Robin
- IP Hash
-
Sticky Sessions (Session Affinity)
- Cookie-based sticky sessions
- IP hash-based sticky sessions
- Configurable session TTL
- Automatic session cleanup
-
Health Checking
- Automatic backend health monitoring
- Configurable check intervals
- Automatic failure detection and recovery
-
Production Ready
- Reverse proxy with proper request forwarding
- Graceful shutdown
- Request/response logging
- Error handling and recovery
- CORS support
- Rate limiting
-
Metrics & Monitoring
- Real-time statistics endpoint
- Request tracking
- Connection monitoring
- Response time measurement
- Success rate calculation
- Session statistics
-
Easy Deployment
- Docker support
- Docker Compose for full stack
- Command-line configuration
- Minimal dependencies
# Clone the repository
git clone https://github.com/TaiTitans/go-balancer.git
cd go-balancer
# Install dependencies
go mod download
# Build
go build -o go-balancer cmd/main.go# Terminal 1
cd examples/backend-server
go run main.go -port 8081 -name "Backend-1"
# Terminal 2
go run main.go -port 8082 -name "Backend-2"
# Terminal 3
go run main.go -port 8083 -name "Backend-3"# Terminal 4
go run cmd/main.go \
-port 8080 \
-backends "http://localhost:8081,http://localhost:8082,http://localhost:8083" \
-strategy roundrobin# Send requests
curl http://localhost:8080
# View statistics
curl http://localhost:8080/stats
# Health check
curl http://localhost:8080/health# Build and run with Docker Compose
docker-compose up --build
# Test
curl http://localhost:8080go-balancer [options]
Options:
-port int
Load balancer port (default 8080)
-backends string
Comma-separated list of backend URLs
(default "http://localhost:8081,http://localhost:8082,http://localhost:8083")
-strategy string
Load balancing strategy: roundrobin, leastconnections, random
(default "roundrobin")
-health-interval duration
Health check interval (default 10s)
-health-timeout duration
Health check timeout (default 5s)
-sticky-session bool
Enable sticky sessions (default false)
-sticky-mode string
Sticky session mode: cookie, iphash (default "cookie")
-sticky-cookie string
Cookie name for sticky sessions (default "BALANCER_SESSION_ID")
-sticky-ttl duration
Sticky session TTL (default 30m)# Enable cookie-based sticky sessions
go run cmd/main.go \
-port 8080 \
-backends "http://localhost:8081,http://localhost:8082,http://localhost:8083" \
-strategy roundrobin \
-sticky-session=true \
-sticky-mode=cookie \
-sticky-ttl=1h
# Enable IP hash sticky sessions
go run cmd/main.go \
-port 8080 \
-backends "http://localhost:8081,http://localhost:8082,http://localhost:8083" \
-strategy leastconnections \
-sticky-session=true \
-sticky-mode=iphashpackage main
import (
"context"
"time"
"github.com/TaiTitans/go-balancer/balancer"
"github.com/TaiTitans/go-balancer/strategy"
)
func main() {
config := balancer.Config{
BackendURLs: []string{
"http://localhost:8081",
"http://localhost:8082",
"http://localhost:8083",
},
Strategy: strategy.NewRoundRobin(),
HealthCheckInterval: 10 * time.Second,
HealthCheckTimeout: 5 * time.Second,
}
lb, err := balancer.NewLoadBalancer(config)
if err != nil {
panic(err)
}
ctx := context.Background()
lb.Start(ctx)
// Use lb as http.Handler
http.ListenAndServe(":8080", lb)
}Distributes requests evenly across all healthy backends in a circular order.
strategy := strategy.NewRoundRobin()Routes requests to the backend with the fewest active connections.
strategy := strategy.NewLeastConnections()Randomly selects a healthy backend for each request.
strategy := strategy.NewRandom()Distributes requests based on backend weights.
weights := map[*backend.Backend]int{
backend1: 3, // 3x more requests
backend2: 2,
backend3: 1,
}
strategy := strategy.NewWeightedRoundRobin(weights)Access /stats to view load balancer statistics:
ββββββββββββββββββββββββββββββββββββββββββ
β Load Balancer Statistics β
ββββββββββββββββββββββββββββββββββββββββββ
Strategy: RoundRobin
Uptime: 1h23m45s
Total Backends: 3
Alive Backends: 3
Total Requests: 15234
Failed Requests: 12
Success Rate: 99.92%
Active Connections: 5
Sticky Sessions:
Mode: cookie
Active Sessions: 42
TTL: 30m0s
Cookie Name: BALANCER_SESSION_ID
Backend Details:
ββββββββββββββββββββββββββββββββββββββββ
[1] http://localhost:8081
Status: β Healthy
Connections: 2
Response Time: 15ms
Fail Count: 0
[2] http://localhost:8082
Status: β Healthy
Connections: 1
Response Time: 12ms
Fail Count: 0
[3] http://localhost:8083
Status: β Healthy
Connections: 2
Response Time: 18ms
Fail Count: 0
# Run all tests
go test ./...
# Run with coverage
go test -cover ./...
# Run specific package tests
go test ./backend
go test ./strategy
go test ./balancer
# Benchmark
go test -bench=. ./...go-balancer/
βββ backend/ # Backend server management
βββ balancer/ # Main load balancer logic
βββ cmd/ # Main application entry point
βββ config/ # Configuration management
βββ examples/ # Example applications
β βββ backend-server/
β βββ simple/
βββ healthcheck/ # Health checking logic
βββ middleware/ # HTTP middleware
βββ session/ # Sticky session management
βββ strategy/ # Load balancing strategies
βββ Dockerfile # Docker configuration
βββ docker-compose.yml
βββ go.mod
βββ go.sum
βββ LICENSE
βββ Makefile
βββ README.md
export BACKEND_URLS="http://localhost:8081,http://localhost:8082"
export LB_STRATEGY="leastconnections"
export LB_PORT="8080"
export HEALTH_CHECK_INTERVAL="10s"
export STICKY_SESSION="true"
export STICKY_MODE="cookie"See config.example.yaml for a complete example.
server:
port: 8080
readTimeout: 15s
writeTimeout: 15s
backends:
- url: http://localhost:8081
weight: 3
- url: http://localhost:8082
weight: 2
],
"healthCheck": {
"interval": "10s",
"timeout": "5s"
},
"strategy": {
"type": "roundrobin"
},
"stickySession": {
"enabled": true,
"mode": "cookie",
"cookieName": "BALANCER_SESSION_ID",
"ttl": "30m"
}
}Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Inspired by production load balancers like NGINX and HAProxy
- Built with Go's powerful
net/httpandhttputilpackages - Thanks to the Go community for excellent documentation
TaiTitans - @TaiTitans
Project Link: https://github.com/TaiTitans/go-balancer
- Circuit breaker pattern
- Rate limiting
Made with β€οΈ using Go