A lightweight, high-performance load balancer implementation in Python with support for multiple load balancing algorithms, health checks, and fault tolerance.
-
Multiple Load Balancing Algorithms
- Round Robin - evenly distribute requests
- Least Connections - route to server with fewest active connections
- IP Hash - consistent routing based on client IP for sticky sessions
-
Health Monitoring
- Periodic health checks for all backend servers
- Automatic failover when servers become unavailable
- Graceful recovery when servers come back online
-
High Performance
- Asynchronous I/O using asyncio for handling concurrent requests
- Non-blocking request processing
- Configurable connection limits and timeouts
-
Observability
- Comprehensive request logging
- Real-time metrics collection (RPS, response times, error rates)
- Status and metrics endpoints for monitoring
-
Easy Deployment
- Docker Compose setup for local development and testing
- Configurable via JSON configuration file
- Multiple backend servers for testing scalability
βββββββββββββββ βββββββββββββββββββ βββββββββββββββ
β Client βββββΆβ Load Balancer βββββΆβ Backend 1 β
βββββββββββββββ β β βββββββββββββββ€
β - Algorithms β β Backend 2 β
βββββββββββββββ β - Health Check β βββββββββββββββ€
β Client βββββΆβ - Metrics βββββΆβ Backend 3 β
βββββββββββββββ β - Logging β βββββββββββββββ
βββββββββββββββββββ
- Python 3.8+
- Docker and Docker Compose (for containerized testing)
- Required Python packages (see requirements.txt)
pip install -r requirements.txt# Start individual backend servers
python backend_servers/server.py --port 8001 --name backend-1 &
python backend_servers/server.py --port 8002 --name backend-2 &
python backend_servers/server.py --port 8003 --name backend-3 &python src/main.py --config config.jsonThe load balancer will start on http://localhost:8080
# Make requests to the load balancer
curl http://localhost:8080/
# Check load balancer status
curl http://localhost:8080/__status__
# View metrics
curl http://localhost:8080/__metrics__# Start all services (load balancer + 3 backend servers)
docker-compose up -d
# Scale to more backend servers
docker-compose --profile scaling up -d
# View logs
docker-compose logs -f load-balancerdocker-compose downEdit config.json to customize the load balancer:
{
"proxy_port": 8080,
"proxy_host": "0.0.0.0",
"algorithm": "round_robin",
"backend_servers": [
{"host": "localhost", "port": 8001, "weight": 1},
{"host": "localhost", "port": 8002, "weight": 1},
{"host": "localhost", "port": 8003, "weight": 1}
],
"health_check_interval": 30,
"health_check_timeout": 5,
"max_connections": 1000,
"connection_timeout": 30,
"request_timeout": 30,
"enable_logging": true,
"enable_metrics": true
}round_robin- Distribute requests evenly across serversleast_connections- Route to server with fewest active connectionsip_hash- Use client IP hash for consistent routing (sticky sessions)
# Basic load test
python tests/load_test.py --requests 1000 --concurrency 20
# Heavy load test
python tests/load_test.py --requests 5000 --concurrency 100
# Test specific endpoint
python tests/load_test.py --requests 500 --concurrency 25 --path /info
# Save results to file
python tests/load_test.py --requests 1000 --concurrency 50 --output results.json# Run comprehensive test suite
chmod +x tests/run_tests.sh
./tests/run_tests.sh/__health__- Load balancer health status/__metrics__- Performance metrics (JSON)/__status__- Detailed status with backend information
{
"uptime_seconds": 300.45,
"total_requests": 1500,
"total_errors": 12,
"requests_per_second": 4.98,
"error_rate": 0.8,
"response_times": {
"average": 45.2,
"minimum": 12.1,
"maximum": 342.7,
"p95": 156.3
},
"status_codes": {
"200": 1488,
"500": 12
}
}Test different load balancing algorithms:
# Test Round Robin
sed -i 's/"algorithm": ".*"/"algorithm": "round_robin"/' config.json
python src/main.py &
python tests/load_test.py -n 100 -c 5
# Test Least Connections
sed -i 's/"algorithm": ".*"/"algorithm": "least_connections"/' config.json
# Restart and test...
# Test IP Hash
sed -i 's/"algorithm": ".*"/"algorithm": "ip_hash"/' config.json
# Restart and test...# Start all services
docker-compose up -d
# Run continuous load test
python tests/load_test.py -n 1000 -c 10 &
# Simulate server failure
docker-compose stop backend-1
# Observe automatic failover in logs
docker-compose logs -f load-balancer
# Restart failed server
docker-compose start backend-1
# Verify recovery# Start with 3 backend servers
docker-compose up -d
# Baseline performance test
python tests/load_test.py -n 1000 -c 50 -o baseline.json
# Scale to 5 backend servers
docker-compose --profile scaling up -d
# Performance test with more servers
python tests/load_test.py -n 1000 -c 50 -o scaled.json
# Compare resultsmini-load-balancer/
βββ src/
β βββ main.py # Entry point
β βββ proxy_server.py # Main proxy logic
β βββ load_balancer.py # Load balancing algorithms
β βββ health_checker.py # Health monitoring
β βββ logger.py # Request logging
β βββ metrics.py # Metrics collection
β βββ config.py # Configuration management
βββ backend_servers/
β βββ server.py # Test backend server
βββ tests/
β βββ load_test.py # Load testing utility
β βββ run_tests.sh # Test automation script
βββ logs/ # Log files
βββ config.json # Configuration file
βββ docker-compose.yml # Docker orchestration
βββ Dockerfile # Load balancer container
βββ Dockerfile.backend # Backend server container
βββ requirements.txt # Python dependencies
- Edit
src/load_balancer.py - Add new algorithm method
- Update
get_next_server()method - Update configuration validation
- Edit
src/metrics.py - Add new metric collection methods
- Update metrics endpoint in
proxy_server.py
Create custom backend servers by extending the base server in backend_servers/server.py or implementing the health check endpoint (/health) in your existing services.
Typical performance on modern hardware:
- Light Load: 1,000+ RPS with <50ms average response time
- Medium Load: 500+ RPS with <100ms average response time
- Heavy Load: 200+ RPS with <200ms average response time
Performance varies based on:
- Backend server response times
- Network latency
- System resources
- Configuration settings
- Fork the repository
- Create a feature branch
- Make changes and add tests
- Ensure all tests pass
- Submit a pull request
MIT License - see LICENSE file for details
-
Port Already in Use
# Find and kill process using port lsof -ti:8080 | xargs kill -9
-
Backend Servers Not Responding
# Check if backend servers are running curl http://localhost:8001/health curl http://localhost:8002/health curl http://localhost:8003/health -
Docker Issues
# Restart all services docker-compose down && docker-compose up -d # Check container logs docker-compose logs load-balancer docker-compose logs backend-1
-
High Error Rates
- Check backend server health
- Verify configuration settings
- Monitor system resources
- Review logs for specific errors
Enable verbose logging:
python src/main.py --verboseThis provides detailed debug information about request routing, health checks, and system operations.