Topic 2 of 5 · 4 min read
Serving models in batch or real time
Goal: Choose batch or real-time serving for a use case and state its latency and cost budget per prediction.
Choose how a model serves predictions from when the answer is needed, not from what is fashionable: if nobody needs it within seconds, score in batch. Real-time serving is the more expensive and more fragile option.
Check yourself · 1 of 3
A model ranks products for a weekly marketing email. Which serving mode fits best?
Why it matters
A recommendation model that refreshes overnight and a fraud model that must answer while a customer waits at checkout have different needs, even if both are gradient-boosted trees. Choosing real time for the first wastes engineering effort and money. Choosing batch for the second lets fraud through.
The two modes
- Batch scoring: a scheduled job reads many rows, scores them and writes the results to a table. Predictions can be a few hours old. Failures are easy to retry, and you can use a big machine for a short time.
- Real-time serving: an HTTP or gRPC endpoint scores one request, or a few, as they arrive. It needs low latency, autoscaling, health checks and a fallback when it is down.
- A hybrid is common: precompute scores for most users in batch, and call the endpoint only for cases where fresh features matter.
How to decide
- Ask when the prediction is used. Before a user acts, in a nightly email, in a weekly report?
- Ask how fresh the inputs must be. If the key feature is "items in the cart right now", batch cannot see it.
- Write a budget per prediction: a latency limit such as "95 percent of requests under 150 ms", and a cost limit such as "under 0.1 cent per prediction". Without a budget there is nothing to test the choice against.
- Plan the fallback: a default score, a cached score or a simple rule when the model is down.
Portable formats
A model trained in PyTorch or scikit-learn does not have to be served by the same library. ONNX is an open format that lets you export the model once and run it in a lightweight runtime, often with lower latency and without shipping the training framework. Test that the exported model gives the same outputs as the original on a sample before you trust it.
Before and after
Before: "We will deploy the churn model as an API." After: "Churn scores are used by the retention team each Monday. Run a batch job on Sunday night, write scores to the churn_scores table, and alert if the job has not finished by 06:00. No endpoint is needed."
What goes wrong
- Training-serving skew: features computed one way in training and another in the endpoint, so live accuracy falls below test accuracy.
- No latency budget: the model passes offline tests and then times out under real traffic.
- No fallback: one crashed container stops checkout.
Examples
Nightly batch for a weekly decision
A telecom retention team calls at-risk customers each Monday. A batch job runs on Sunday night, scores all active customers and writes the scores to a table the call-centre tool reads. No endpoint exists, so there is no latency budget to meet and a failed job is simply rerun.
Checkout fraud check
A payment model must answer before the card is charged. Budget: 95 percent of calls under 100 ms, with a rule-based fallback that approves small orders and holds large ones if the model times out. The model is exported to ONNX and checked against the original on 1,000 saved transactions before release.
Key points
- Pick batch or real time from when the prediction is needed and how fresh its inputs must be.
- Batch is simpler, cheaper and easier to retry, so use it whenever nobody needs the answer within seconds.
- Write a latency and cost budget per prediction before building, so the choice can be tested.
- Export to a portable format such as ONNX when serving does not need the training framework, and check that outputs match.
- Plan a fallback for when the endpoint is slow or down.
Common mistakes
- Building a real-time endpoint for a decision that is made once a week.
- Setting no latency or cost budget, then discovering the limit in production.
- Computing features differently in training code and in the serving code.
- Exporting to ONNX and never comparing its outputs with the original model.
Check yourself · 2 of 3
What is the main reason to write a latency and cost budget per prediction?
Check yourself · 3 of 3
After exporting a model to ONNX, what should you check before serving it?
Practice items, not counted towards your mastery.
Start here
- Google: Rules of Machine Learning (opens in a new tab)ArticleFree
Practical rules from Google engineers on pipelines, monitoring, skew and launching models.
- Hidden Technical Debt in Machine Learning Systems (opens in a new tab)PaperFree
Why ML systems accumulate debt in data dependencies, glue code and feedback loops.
- MLflow documentation (opens in a new tab)DocsFree
How to log parameters, metrics, artefacts and model versions so runs can be compared and rebuilt.
More for this topic
- ONNX: Open Neural Network Exchange (opens in a new tab)DocsFree
The portable model format used to move a trained model between frameworks and runtimes.