A high-performance HTTP load balancer implemented in Rust with multiple load balancing algorithms, health checking, and real-time monitoring.
loom_1080p.1.mp4
Uploading loom.mp4β¦
- HTTP Request Forwarding: Full HTTP request/response proxying
- Multiple Load Balancing Algorithms:
- Round Robin (default)
- Least Connections
- Random
- Weighted Round Robin
- Health Checking: Automatic backend monitoring with configurable intervals
- Connection Tracking: Real-time active connection counting
- Monitoring Endpoints:
/lb-health- Load balancer health status/lb-stats- Detailed statistics and metrics
- Custom Headers: Adds
X-Forwarded-ByandX-Backend-Serverheaders - Command-Line Interface: Full CLI with help and configuration options
- Structured Logging: Comprehensive logging with tracing
- Error Handling: Graceful handling of backend failures
- Async/Await: High-performance concurrent request handling
- Thread-Safe: Safe concurrent access to shared state
- Automatic Failover: Removes unhealthy backends from rotation
- Auto-Recovery: Re-enables backends when they become healthy
cargo build --release# Terminal 1 - Backend server on port 3001
PORT=3001 cargo run --bin test_server
# Terminal 2 - Backend server on port 3002
PORT=3002 cargo run --bin test_server
# Terminal 3 - Backend server on port 3003
PORT=3003 cargo run --bin test_server# Terminal 4 - Load balancer on port 8080
cargo run --bin load_balancer# Send requests to see load balancing in action
curl http://localhost:8080/
# Check load balancer health
curl http://localhost:8080/lb-health
# View statistics
curl http://localhost:8080/lb-statscargo run --bin load_balancer -- --helpOptions:
-p, --port <PORT> Port to listen on [default: 8080]
-b, --backends <BACKENDS> Backend server addresses (comma-separated) [default: 127.0.0.1:3001,127.0.0.1:3002,127.0.0.1:3003]
-a, --algorithm <ALGORITHM> Load balancing algorithm [default: round_robin]
--health-check-interval <HEALTH_CHECK_INTERVAL> Health check interval in seconds [default: 30]
-h, --help Print help
-V, --version Print version
# Use least connections algorithm
cargo run --bin load_balancer -- --algorithm least_connections
# Custom backends and port
cargo run --bin load_balancer -- --port 9000 --backends "192.168.1.10:8001,192.168.1.11:8001,192.168.1.12:8001"
# Random algorithm with frequent health checks
cargo run --bin load_balancer -- --algorithm random --health-check-interval 10Distributes requests evenly across all healthy backends in sequential order.
Routes requests to the backend server with the fewest active connections.
Randomly selects a healthy backend server for each request.
Similar to round robin but considers server weights (currently all servers have equal weight).
Returns the health status of the load balancer and all backend servers.
{
"status": "ok",
"healthy_backends": 3,
"total_backends": 3,
"backends": [
{
"address": "127.0.0.1:3001",
"healthy": true,
"connections": 0,
"response_time_ms": 1
}
]
}Provides detailed statistics about the load balancer and backend servers.
{
"algorithm": "RoundRobin",
"backends": [
{
"address": "127.0.0.1:3001",
"healthy": true,
"connections": 2,
"weight": 1,
"response_time_ms": 5,
"last_health_check": 15
}
]
}The load balancer adds the following headers to forwarded requests:
X-Forwarded-By: rust-load-balancer- Identifies the load balancerX-Backend-Server: <backend_address>- Shows which backend handled the request
- Health checks are performed on the
/healthendpoint of each backend server - Configurable interval (default: 30 seconds)
- Automatic failover when backends become unhealthy
- Backends are automatically re-enabled when they recover
Backend servers should:
- Health Endpoint: Respond to
GET /healthwith HTTP 200 for healthy status - HTTP Protocol: Support standard HTTP requests and responses
- Port Binding: Listen on the configured IP address and port
- Test Server: Simple backend server for testing (
test_server.rs) - Multiple Binaries: Separate binaries for load balancer and test server
- JSON Responses: Structured responses for easy testing
# Run tests
cargo test
# Check for linting issues
cargo clippy
# Format code
cargo fmtThe load balancer is built using:
- Hyper: High-performance HTTP server and client
- Tokio: Async runtime for concurrent request handling
- Tracing: Structured logging and diagnostics
- Clap: Command-line argument parsing
- Serde: JSON serialization for monitoring endpoints
The load balancer is designed for high performance:
- Async/await for concurrent request handling
- Minimal memory allocation during request forwarding
- Lock-free operation for request routing (minimal mutex usage)
- Efficient health checking without blocking request processing
This load balancer is production-ready with enterprise-level features:
- High Performance: Built with Rust's zero-cost abstractions
- Memory Safety: Rust's ownership system prevents common bugs
- Concurrent: Async/await for handling thousands of concurrent requests
- Observable: Comprehensive logging and monitoring endpoints
- Configurable: CLI-based configuration for deployment flexibility
- Resilient: Automatic failover and recovery mechanisms