A high-performance HTTP load balancer written in Go with support for multiple load balancing algorithms, session persistence methods, and path-based routing capabilities.
-
Multiple Load Balancing Algorithms
- Round Robin: Distributes requests sequentially across backends
- Weighted Round Robin: Distributes requests proportionally to backend weights
- Least Connections: Routes requests to the backend with fewest active connections
-
Session Persistence Methods
- Cookie-based persistence: Tracks client sessions with HTTP cookies
- IP hash persistence: Maps client IPs to specific backends
- Consistent hashing: Provides stable request distribution
-
Path-Based Routing
- Route requests to different backend pools based on URL path patterns
- Support for exact matches and regex-based patterns
- Configure different algorithms and persistence methods per route
-
WebSocket Support
- Seamless WebSocket connection proxying
- Maintains long-lived connections
- Automatic failover for failed backend servers
-
Health Checking
- Automatic detection of failed backends
- Self-healing with automatic recovery
- Configurable health check intervals
-
Monitoring API
- JSON API for runtime statistics
- Health status endpoints
- Per-backend metrics
-
Production Ready
- Docker and Docker Compose support
- Customizable via command-line flags
- Graceful shutdown handling
The load balancer consists of the following core components:
- HTTP Proxy: Routes requests to backend servers
- Backend Pool: Manages the set of available backend servers
- Algorithm Strategies: Implements different load balancing algorithms
- Session Persistence: Maintains client sessions with the same backend
- Health Checker: Periodically checks backends and removes unhealthy ones
- Configuration Parser: Reads and validates the config file
- Monitoring API: Provides runtime statistics
The easiest way to try the load balancer is with Docker Compose:
git clone https://github.com/The-iyed/go-load-balancer.git
cd go-load-balancer
docker-compose up -d
This starts:
- The load balancer on port 8080 for traffic and 8081 for the admin API
- Three backend web servers with different weights
git clone https://github.com/The-iyed/go-load-balancer.git
cd go-load-balancer
go build -o loadbalancer cmd/server/main.go
./loadbalancer --config=conf/loadbalancer.conf
Usage of ./loadbalancer:
--algorithm string
Override load balancing algorithm: round-robin, weighted-round-robin, least-connections
--config string
Path to configuration file (default "conf/loadbalancer.conf")
--persistence string
Override session persistence method: none, cookie, ip_hash, consistent_hash
--path-routing
Enable path-based routing
--port int
Port to listen on (default 8080)
--admin-port int
Port for admin API server (default 8081)
method weighted_round_robin
persistence cookie
backend http://localhost:8001 weight=5
backend http://localhost:8002 weight=3
backend http://localhost:8003 weight=1
route /api/* backend_pool=api_servers
route /auth/* backend_pool=auth_servers
route /static/* backend_pool=static_servers
pool api_servers method=least_connections
backend http://api1:8080 pool=api_servers
backend http://api2:8080 pool=api_servers
pool auth_servers method=ip_hash persistence=cookie
backend http://auth1:8080 pool=auth_servers
backend http://auth2:8080 pool=auth_servers
pool static_servers method=weighted_round_robin
backend http://static1:8080 weight=3 pool=static_servers
backend http://static2:8080 weight=1 pool=static_servers
This method adds a cookie to track which backend server a client should use:
+---------+ 1. Request +---------------+ 2. Select +----------+
| Client |-------------->| Load Balancer |-------------->| Backend1 |
+---------+ +---------------+ +----------+
| |
| | 3. Set cookie: GOLB_SESSION=0:hash
| |
| 4. Next request with cookie
| GOLB_SESSION=0:hash |
v v
+---------+ 5. Use cookie +---------------+ 6. Route to +----------+
| Client |--------------->| Load Balancer |--------------->| Backend1 |
+---------+ +---------------+ +----------+
The cookie contains:
- Backend server index
- MD5 hash for verification to prevent tampering
- Default 24-hour expiration (configurable)
This method uses the client's IP address to determine backend server assignment:
+---------+ 1. Request from +---------------+
| Client |-------------------->| Load Balancer |
| IP: x.x.x.x +---------------+
+---------+ |
| 2. Hash IP: hash(x.x.x.x) % backends
v
+----------+
| Backend2 |
+----------+
All subsequent requests from the same IP address will route to the same backend server.
This method uses consistent hashing of the request path:
Backend1
/ \
/ \
/ \
-------+------------+------- Hash Ring
| |
| |
+ +
Backend3 Backend2
Requests are routed to the nearest server on the hash ring.
Key features:
- Each backend has multiple virtual nodes on the ring based on its weight
- Only a portion of requests are reassigned when servers change
- Provides good distribution while maintaining consistency
The admin API is available on the admin port (default 8081):
GET /api/health
- Check if the load balancer is healthyGET /api/stats
- Get current load balancer statistics with detailed backend information
Example /api/stats
response:
{
"method": "weighted_round_robin",
"persistenceType": "cookie",
"totalRequests": 1024,
"uptime": "1h24m15s",
"backends": [
{
"url": "http://backend1:8080",
"alive": true,
"weight": 5,
"requestCount": 512,
"errorCount": 2,
"loadPercentage": 50.0,
"responseTimeAvg": 15
},
{
"url": "http://backend2:8080",
"alive": true,
"weight": 3,
"requestCount": 307,
"errorCount": 0,
"loadPercentage": 30.0,
"responseTimeAvg": 12
},
{
"url": "http://backend3:8080",
"alive": true,
"weight": 1,
"requestCount": 205,
"errorCount": 1,
"loadPercentage": 20.0,
"responseTimeAvg": 10
}
]
}
Benchmarks show the load balancer can handle:
- 10,000+ requests per second on modest hardware
- Low latency overhead (typically < 1ms)
- Graceful handling of backend server failures
- Go 1.21+
- Docker (optional, for containerized development)
go build -o loadbalancer cmd/server/main.go
# Run all tests
go test ./...
# Run specific tests
go test ./internal/balancer
go-load-balancer/
βββ cmd/ # Application entry points
β βββ server/ # Load balancer server
βββ conf/ # Configuration files
βββ internal/ # Internal packages
β βββ balancer/ # Load balancing implementation
β βββ logger/ # Logging utilities
βββ docs/ # Documentation
βββ examples/ # Example backend servers
βββ Dockerfile # Container definition
βββ docker-compose.yml # Multi-container setup
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
- The Go Team for the excellent standard library
- The Nginx project for inspiration on the configuration format