A high-performance HTTP load balancer built in Go with optional rate limiting and JWT authentication.
-
🔄 Load Balancing Algorithms
- Round Robin
- Least Connections
-
🛡️ Optional Security
- JWT-based authentication (optional)
- Rate limiting with token bucket algorithm (optional)
- Per-client IP rate limiting
-
🏥 Health Monitoring
- Automatic backend health checks
- Real-time backend status monitoring
-
⚡ Performance
- HTTP reverse proxy
- Concurrent request handling
- Configurable middleware
git clone https://github.com/Rx-11/load-balancer-go.git
cd load-balancer-go
go mod tidy
go build -o load-balancer main.goCreate a config.json file:
{
"port": "8080",
"jwt_secret": "",
"load_balancer": {
"algorithm": "round_robin"
},
"rate_limit": {
"enable": false,
"requests_per_second": 100,
"burst_size": 50
},
"backends": [
{
"id": "backend1",
"url": "http://localhost:8081",
"health": true
},
{
"id": "backend2",
"url": "http://localhost:8082",
"health": true
}
]
}go run main.go# Basic request
curl http://localhost:8080/
# Health check
curl http://localhost:8080/health| Field | Description | Example |
|---|---|---|
port |
Server port | "8080" |
jwt_secret |
JWT secret (leave empty to disable auth) | "" |
"load_balancer": {
"algorithm": "round_robin"
}Available algorithms:
round_robin- Distributes requests evenlyleast_connections- Routes to backend with fewest connections
"rate_limit": {
"enable": true,
"requests_per_second": 100,
"burst_size": 50
}- Set
enable: falseto disable rate limiting requests_per_second- Sustained request rateburst_size- Maximum burst capacity
"backends": [
{
"id": "backend1",
"url": "http://localhost:8081",
"health": true
}
]When jwt_secret is set, all requests except /health require a JWT token:
curl -H "Authorization: Bearer YOUR_JWT_TOKEN" http://localhost:8080/The load balancer sends GET requests to /health on each backend every 30 seconds. Backends should return HTTP 200 for healthy status.
Available endpoints:
GET /health- Load balancer health status
load-balancer-go/
├── main.go # Application entry point
├── config/
│ └── config.go # Configuration management
├── loadbalancer/
│ ├── roundrobin.go # Round robin algorithm
│ ├── leastconn.go # Least connections algorithm
│ └── balancer.go # Load balancer interface
├── middleware/
│ ├── auth.go # JWT authentication
│ └── ratelimiter.go # Rate limiting
├── proxy/
│ └── http.go # HTTP reverse proxy
└── health/
└── checker.go # Health monitoring
- API Gateway - Route requests with optional authentication
- Web Application Load Balancer - Distribute traffic across multiple servers
- Development Environment - Simple load balancing for local testing
- Fork the repository
- Create a feature branch
- Make your changes
- Test your changes
- Submit a pull request
This project is licensed under the MIT License.
Made with ❤️ in Go