A simple HTTP proxy and load balancer with weighted round-robin, sticky sessions, retries, health checks, circuit breaker, backpressure, and connection pooling, built with aiohttp.
load-balancer/
├── app/
│ ├── app.py # entry point
│ ├── backends.py # backend server model
│ ├── balancer.py # weighted round-robin logic
│ ├── proxy.py # request forwarding & retries
│ ├── health.py # health check loop
│ ├── config.py # settings
│ ├── logging_conf.py # logging configuration
│ └── tests/
│ ├── __init__.py
│ ├── conftest.py
│ ├── test_backend.py
│ ├── test_balancer.py
│ └── test_proxy_integration.py
├── mock/
│ ├── backend_1.py # mock server on port 9001
│ └── backend_2.py # mock server on port 9002
├── requirements.txt
├── README.md
└── main.py
- Weighted round-robin load balancing
- Sticky sessions (cookies)
- Health checks
- Retries & request timeouts
- Circuit breaker for failing backends
- Backpressure control
- Connection pooling
- Request logging
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtpython mock/backend_1.py &
python mock/backend_2.py &Backends listen on ports 9001 and 9002:
GET /→{"server": "backend1", "message": "ok"}GET /health→{"status": "alive"}
python main.pyListens on http://localhost:8080
curl http://localhost:8080/pytest -vTests cover:
- Circuit breaker logic
- Weighted round-robin selection
- Proxy forwarding
- Sticky sessions
python app/tests/load_test.pySends concurrent requests and reports average/max latency.
Edit app/config.py:
REQUEST_TIMEOUT— Backend request timeoutRETRIES— Retry attemptsHEALTH_CHECK_INTERVAL— Health check frequencyCIRCUIT_BREAKER_THRESHOLD— Failures before circuit opensCIRCUIT_BREAKER_TIMEOUT— Duration backend stays disabledMAX_INFLIGHT_REQUESTS— Concurrent request limitSTICKY_COOKIE— Session cookie name
- WeightedRoundRobinBalancer (
app/balancer.py) — Selects backends by weight - Proxy (
app/proxy.py) — Forwards requests with retries & backpressure - Health checks (
app/health.py) — Monitors backend availability - Circuit breaker — Disables unhealthy backends