Sub topic 4
Latency versus Throughput
This chapter assumes you know basic machine learning vocabulary (training, model, features, prediction), can read Python, and understand HTTP APIs and scheduled jobs. It builds on the earlier chapters that defined batch and real-time prediction.
Two different questions
Latency is the time one prediction takes, from the moment a request arrives to the moment the answer is returned. It is measured per request, in milliseconds or seconds.
Throughput is how many predictions the system completes per unit of time, for example 5,000 rows per second. It is measured across all work.
The two are related but not the same. A system can answer one request in 20 ms and still handle only 50 requests per second. Another system can score 10 million rows per hour but take 40 minutes before the first result appears.
| Measure | Question it answers | Typical unit | Who cares most |
|---|---|---|---|
| Latency | How long does one caller wait? | ms, p95, p99 | Real-time services, users |
| Throughput | How much work finishes per unit of time? | rows/second | Batch jobs, capacity planning |
Why batch favors throughput
A batch job already has all its input. Nobody waits on a single row, so the job can group rows together and process them as one block. Libraries like scikit-learn and NumPy run vectorized code, which applies one operation to many rows at once. This amortizes fixed costs such as function calls, memory allocation, and (on GPUs) data transfer.
The batch example from this tutorial does this in one call:
import joblib
import pandas as pd
model = joblib.load("model.joblib")
customers = pd.read_parquet("customers_2026-11-28.parquet")
feature_columns = ["tenure_months", "monthly_spend", "support_tickets"]
customers["churn_score"] = model.predict_proba(customers[feature_columns])[:, 1]
customers[["customer_id", "churn_score"]].to_parquet("scores_2026-11-28.parquet")One predict_proba call scores every row. The time for any single customer is meaningless here. What matters is that the whole job finishes before the next consumer needs the file.
Why real time favors latency
A real-time service receives one request and a caller is waiting. The service cannot hold the request until more arrive, because that would add delay. It therefore scores a single row per call and keeps the model loaded in memory.
import joblib
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
model = joblib.load("model.joblib")
class Features(BaseModel):
tenure_months: float
monthly_spend: float
support_tickets: int
@app.post("/predict")
def predict(features: Features):
row = [[features.tenure_months, features.monthly_spend, features.support_tickets]]
return {"churn_score": float(model.predict_proba(row)[0, 1])}Loading the model at startup, not inside predict, removes a large fixed cost from every request. The price is lower efficiency per prediction, since one-row calls cannot use vectorization.
Percentiles, not averages
Average latency hides slow requests. If 99 requests take 20 ms and one takes 2,000 ms, the mean is about 40 ms, yet one user in a hundred waits two seconds. Real-time services therefore set targets on percentiles. The p95 latency is the value that 95% of requests beat. The p99 is the value that 99% beat. A typical target reads "p99 under 200 ms".
The trade-off between them
Increasing one often hurts the other. Grouping requests into small batches raises throughput, because the model processes several rows per call. It also raises latency, because each request waits for the group to fill.
The chapter on micro-batching returns to this idea. For now, remember that group size is the dial that moves a system between the two goals.
Common errors
- Reporting only mean latency and missing the slow tail.
- Measuring throughput on a warm, idle machine and assuming it holds under concurrent load.
- Loading the model inside the request handler, which adds model-load time to every call.
- Benchmarking a real-time service with batch-sized inputs, or the reverse.
Exercise
A nightly job scores 20 million rows in 40 minutes. A web endpoint scores one row in 15 ms but is called 100 times per second and cannot keep up. Which system has better throughput per row, and which metric should the endpoint team track to describe what each user experiences?
Show answer
The nightly job has far higher throughput: 20 million rows in 2,400 seconds is about 8,300 rows per second. The endpoint team should track latency percentiles such as p95 and p99, because the mean hides the slow requests that users notice.