A high-performance, production-ready HTTP Load Balancer built with Python 3.12+ and asyncio. This project demonstrates mastery of networking, concurrency, resilience, and system architecture.
- HTTP reverse proxy with round-robin load balancing
- Async request forwarding using aiohttp
- Configurable backend server pool
- Request/response logging
- Periodic health checks for backend servers
- Automatic failover for unhealthy servers
- Health status tracking
- Sticky sessions (session affinity)
- Weighted round-robin
- Least connections algorithm
- Prometheus-compatible metrics endpoint
- Request/response statistics
- Latency tracking
- Error rate monitoring
- Rate limiting per client IP
- LRU caching layer
- Load-aware scheduling
- Web dashboard for real-time stats
- Python 3.11+ (tested with 3.12)
- aiohttp >= 3.9.0
# Clone the repository
git clone https://github.com/Pneha1234/pyLoadBalancer.git
cd pyLoadBalancer
# Create virtual environment
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt# Start the load balancer
python -m load_balancer.mainThe load balancer will start on http://localhost:8080
# Build and run with docker-compose
docker-compose up --build
# Or build manually
docker build -t py-loadbalancer .
docker run -p 8080:8080 py-loadbalancerload_balancer/
βββ main.py # Application entry point
βββ config.py # Configuration settings
βββ balancer.py # Core reverse proxy logic
βββ server_pool.py # Server pool management
βββ metrics.py # Metrics collection (Phase 4)
βββ utils/
βββ health_checker.py # Health check logic (Phase 2)
tests/ # Unit and integration tests
docs/ # Documentation
βββ ARCHITECTURE.md
βββ TECH_JUSTIFICATION.md
βββ LOAD_TESTING.md
# Run all tests
pytest tests/ -v
# Run with coverage
pytest tests/ --cov=load_balancer --cov-report=html
# Run specific test file
pytest tests/test_server_pool.py -vConfiguration:
- Tool: wrk
- Concurrent connections: 100
- Duration: 30 seconds
- Backend servers: 3
Results:
- Throughput: 5,000+ requests/second
- Average latency: 2-5ms
- P95 latency: < 10ms
- Error rate: < 0.1%
- CPU usage: ~30% (single core)
- Memory: ~50MB base + ~5MB per 1000 connections
Note: Results vary based on hardware and network conditions
See ARCHITECTURE.md for detailed architecture documentation.
Client β Load Balancer (Port 8080) β Backend Servers (9001, 9002, 9003)
Edit load_balancer/config.py to configure:
LB_PORT = 8080
BACKEND_SERVERS = [
"http://localhost:9001",
"http://localhost:9002",
"http://localhost:9003",
]
REQUEST_TIMEOUT = 30-
Python + asyncio
- Pros: Great developer experience, async I/O, large ecosystem
- Cons: Slightly slower than Go/C++, but sufficient for most use cases
-
aiohttp
- Pros: Single framework for client/server, async-native, mature
- Cons: Slightly more complex than Flask, but better for high concurrency
-
Round-robin (Phase 1)
- Pros: Simple, fair distribution, stateless
- Cons: Doesn't account for server capacity differences
-
In-memory metrics (Phase 4)
- Pros: Zero latency, no external dependencies
- Cons: Lost on restart, not suitable for distributed systems
-
Database Integration
- Add Redis for distributed rate limiting and caching
- Add PostgreSQL/InfluxDB for metrics persistence
-
Advanced Load Balancing
- Implement least connections algorithm
- Add geographic load balancing
- Implement consistent hashing
-
Security
- Add TLS/SSL termination
- Implement authentication/authorization
- Add DDoS protection
-
Observability
- Integrate with Prometheus + Grafana
- Add distributed tracing (OpenTelemetry)
- Structured logging with correlation IDs
-
Scalability
- Horizontal scaling with multiple LB instances
- Service discovery integration (Consul, etcd)
- Auto-scaling backend pool
-
Performance
- Connection pooling optimization
- HTTP/2 support
- WebSocket load balancing
This is a learning project. Contributions welcome!
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
MIT License - feel free to use this project for learning!
- asyncio Documentation
- aiohttp Documentation
- System Design Interview - Volume 2
- High Performance Browser Networking
For questions or suggestions, open an issue or reach out!
Built with β€οΈ for learning system design and high-performance Python