A final-year B.Tech major project (ABES Institute of Technology, Ghaziabad). It is a load balancer that does not only react to how busy servers are right now: it also predicts how busy each server will be a few seconds from now and routes requests to the server expected to be least loaded.
control-service(Java 21, Spring Boot) is the load balancer. It collects live metrics from the backend servers, selects a server, and forwards requests.ml-service(Python, FastAPI) is a separate prediction service. It forecasts a server's CPU load about 6 seconds ahead using a trained model.- Backend servers are three instances of the same Spring Boot application, started on ports 9001, 9002 and 9003.
flowchart LR
Client([Client]) -->|GET /api/route/predictive| CS
subgraph CS [control-service :8080]
Collector[Metrics collector<br/>polls every 2 s]
Selector[Server selection<br/>round robin / weighted / predictive]
end
Collector -->|GET /backend/metrics| A[server-a :9001]
Collector -->|GET /backend/metrics| B[server-b :9002]
Collector -->|GET /backend/metrics| C[server-c :9003]
Collector -->|append rows| CSV[(data/metrics.csv)]
Selector -->|POST /predict| ML[ml-service :8001<br/>FastAPI + trained model]
Selector -->|GET /backend/info| A
CSV -->|train.py| Model[(models/cpu_forecaster.joblib)]
Model --> ML
How a predictive request flows
- Every 2 seconds the collector reads CPU, memory and request rate from each backend and keeps the last 20 CPU readings per server. A backend that stops answering is marked
DOWNand is skipped. - For each healthy server, the control service sends the current reading and recent CPU history to the ML service.
- The ML service returns the predicted CPU load about 6 seconds ahead.
- The server with the lowest predicted load receives the request.
- If the ML service is unavailable, routing falls back to the weighted-load strategy, and the strategy name in the response shows that a fallback happened.
| Strategy | Endpoint | How it chooses |
|---|---|---|
| Round robin | GET /api/route |
Cycles through healthy servers in order |
| Weighted load | GET /api/servers/recommended |
Lowest score, where score = 0.5 x CPU + 0.3 x memory + 0.2 x normalized request rate |
| Predictive | GET /api/route/predictive |
Lowest CPU predicted by the ML model |
.
├── control-service/ Spring Boot load balancer and backend server
│ ├── pom.xml
│ ├── data/metrics.csv Collected training data
│ └── src/main/java/com/yuva/loadbalancer/
│ ├── controller/ LoadBalancerController, BackendController
│ ├── service/ ServerSelectionService, MetricsCollector, MlClient,
│ │ RequestRateTracker, RequestCountingFilter
│ └── model/ ServerInfo, BackendMetrics, Prediction*, ...
├── ml-service/ FastAPI prediction service
│ ├── app/main.py API (/health, /model-info, /predict)
│ ├── app/features.py Feature engineering shared by training and serving
│ ├── train.py Training and evaluation script
│ ├── models/ Saved model and results.json
│ └── requirements.txt
├── generate-load.ps1 Random load bursts used to create training data
└── README.md
- JDK 21
- Maven 3.9+
- Python 3.11+
- Windows PowerShell (the commands below are for PowerShell)
Open four PowerShell windows, one for each part, from the repository root.
cd ml-service
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -r requirements.txt
uvicorn app.main:app --reload --port 8001API docs: http://127.0.0.1:8001/docs
Build the jar once:
cd control-service
mvn clean package -DskipTestsStart three backends, each with its own port and ID. The collector is disabled on them because only the control service should collect metrics:
$jar = "target\load-balancer-control-service-0.1.0.jar"
Start-Process powershell -ArgumentList "-NoExit","-Command","java -jar $jar --server.port=9001 --app.server-id=server-a --app.collector.enabled=false"
Start-Process powershell -ArgumentList "-NoExit","-Command","java -jar $jar --server.port=9002 --app.server-id=server-b --app.collector.enabled=false"
Start-Process powershell -ArgumentList "-NoExit","-Command","java -jar $jar --server.port=9003 --app.server-id=server-c --app.collector.enabled=false"cd control-service
mvn spring-boot:runDo not use mvn clean while the backends are running, because it deletes the jar they were started from.
- http://localhost:8080/api/servers shows all three servers as
HEALTHYwith live metrics. - http://localhost:8080/api/route/predictive returns a response from the selected backend.
- http://localhost:8080/api/ml-health shows that the ML service is up.
| Method | Path | Description |
|---|---|---|
| GET | /api/servers |
Latest collected metrics for every server |
| GET | /api/servers/round-robin |
Next server in round-robin order |
| GET | /api/servers/recommended |
Server with the lowest weighted load score |
| GET | /api/route |
Forward a request using round robin |
| GET | /api/route/predictive |
Forward a request using the predictive strategy |
| GET | /api/ml-health |
Health of the ML service |
| POST | /api/predictions |
Get a prediction for one server |
| GET | /actuator/health |
Spring Boot health check |
Example prediction request:
$body = @{
serverId = 'server-a'
cpuPercent = 61
memoryPercent = 48
requestsPerSecond = 35
recentCpuPercent = @(45, 49, 54, 58, 61)
} | ConvertTo-Json
Invoke-RestMethod -Method Post -Uri http://localhost:8080/api/predictions -ContentType 'application/json' -Body $body| Method | Path | Description |
|---|---|---|
| GET | /backend/info |
Identifies the backend that answered |
| GET | /backend/metrics |
CPU, heap memory and request rate of this backend |
| GET | /backend/work?ms=300 |
Burns CPU for the given time, used to create test load |
| Method | Path | Description |
|---|---|---|
| GET | /health |
Service status and loaded model |
| GET | /model-info |
Training results of the saved model |
| POST | /predict |
Predict CPU load about 6 seconds ahead |
Each backend reports metrics about its own process:
- CPU: process CPU usage from the JVM.
- Memory: used heap as a percentage of maximum heap.
- Requests per second: smoothed request rate over
/backend/*endpoints.
The control service appends one row per server every 2 seconds to control-service/data/metrics.csv:
timestamp,server_id,cpu_percent,memory_percent,requests_per_second
generate-load.ps1 sends random bursts of work to random backends, with idle gaps in between, so the collected data contains both quiet and busy periods. Run it while the whole system is up:
powershell -ExecutionPolicy Bypass -File .\generate-load.ps1The current dataset has roughly 10,000 readings (about 3,500 per server). The load is synthetic, so results describe this test setup and not real production traffic.
- Target: the server's CPU 3 readings ahead (about 6 seconds).
- Features (
ml-service/app/features.py): current CPU, memory and request rate; the four previous CPU readings; the mean and least-squares slope of the last five readings; and the latest CPU change. - The same feature code is used for training and for live predictions, so the two cannot drift apart.
- Chronological split per server: the first 80% of the time is used for training and the last 20% for testing. Data is never shuffled.
- A gap of 3 readings between the two periods keeps training targets from overlapping the test period.
- Metrics: MAE and RMSE (lower is better).
cd ml-service
.\.venv\Scripts\python.exe train.pyThis trains the models, prints the comparison table, and saves the best model by RMSE to models/cpu_forecaster.joblib together with models/results.json.
Two datasets were collected:
- v1: brief CPU spikes on an otherwise idle system (about 10,500 readings). Heavy load was extremely rare.
- v2: sustained load that rises and falls on all three servers at once, collected with
generate-load-v2.ps1(3,942 readings).
Results on dataset v2 (3,144 training rows, 780 test rows, chronological split):
| Model | MAE | RMSE |
|---|---|---|
| Baseline: persistence (CPU stays the same) | 2.953 | 6.457 |
| Baseline: linear trend extrapolation | 4.300 | 7.390 |
| Ridge regression | 3.172 | 6.312 |
| Random forest | 4.671 | 7.379 |
| Gradient boosting | 3.732 | 6.520 |
Findings:
- The trained models did not outperform the persistence baseline on this workload. Ridge has a marginally lower RMSE (about 2%), which is within noise for a test set of this size, and a higher MAE.
- The load generator holds each load level for 15 to 50 seconds and then changes it by a random step. Over a 6-second horizon the CPU usually stays the same, and the timing and direction of changes are not predictable from history.
- A model trained on dataset v1 (brief spikes) performs very poorly on v2 (MAE 19.10 compared with 3.17), which shows that forecasting quality depends heavily on the load pattern in the training data.
- Results are specific to this synthetic workload and a single chronological split.
| Area | Status |
|---|---|
| Round-robin, weighted-load and predictive routing | Done |
| Real metrics from backends, with health detection | Done |
| Metrics logging to CSV | Done |
| Training pipeline with chronological evaluation | Done |
Serve the trained model through the FastAPI /predict endpoint |
Done |
| Move predictions into the collector loop (faster routing) | Planned |
| Load tests comparing the three strategies (latency, errors, load spread) | Planned |
| Accuracy report on busy periods only | Planned |
| Spring Cloud Gateway, Eureka, Prometheus and Grafana | Planned |
- The backend list is fixed in
MetricsCollector(three servers on localhost). - All servers run on one machine, so they compete for the same CPU.
- Memory is JVM heap usage, which changes very little in this setup.
- The training load is synthetic and mostly idle with bursts to full CPU.
- Each predictive request currently makes one ML call per server, which adds latency.