Applied ML systems
Batch vs online inference
Batch scoring writes predictions ahead of time. Online scoring answers while a user waits. Choose by how fresh the features must be and how much latency the product can afford.
Some predictions can wait an hour. Others must return before a page loads. The choice between batch and online inference sets your infrastructure, your latency budget and how much can go wrong at serving time.
The big idea
| Mode | When it fits | Typical latency | Example |
|---|---|---|---|
| Batch | Predictions can wait minutes to hours | Throughput matters, not per-request time | Nightly churn scores written to a table |
| Online | A user is waiting | Often 10 to 100 ms at the 99th percentile for classical models | Fraud check at checkout |
| Nearline | Asynchronous but event-driven | Seconds | Recompute embeddings when a video is uploaded |
| On-device | Privacy or offline use | The device's CPU or NPU budget | Keyboard next-word suggestions |
Batch is simpler. You can replay it, debug it and rerun it, and nothing depends on a live feature store. The cost is staleness: a nightly score is up to a day old when a user acts on it.
Online is required when the best features exist only at request time, such as the contents of a shopping cart. Every online feature becomes a runtime dependency, so each one needs an owner and an outage plan.
Walking through a latency budget
Suppose the product needs a 200 ms response at the 99th percentile. Authentication and the database take 80 ms. That leaves 120 ms for the model. If fetching features takes 40 ms, the model itself has 80 ms.
That arithmetic decides the model size, how fast the feature store must be, and whether you should precompute scores instead.
Choosing a mode
- Does the best feature exist only at request time? If yes, go online.
- Is a score that is hours old still useful? If yes, go batch.
- Otherwise, use nearline, updating scores as events arrive.
Trade-off
Batch scoring is simpler, because it needs no feature-store latency guarantee and it is easy to replay. Online scoring is required when request-time features matter. Do not build an online path just in case. Each online dependency adds operational risk.
Packaging the model
Model packaging matters as much for serving as the choice of mode. Pickle files are fragile across library versions. ONNX, TorchScript and model servers such as Triton are more portable. Pin the dependency versions. Store the training commit SHA and the dataset snapshot ID next to the artifact.
Common mistakes
- Building an online path "just in case". Every online feature is a dependency that can fail at 3 a.m.
- Assuming batch scores are fresh. A nightly score can be a day old.
- Leaving the latency budget until launch. Work out the budget before choosing the model size.
Check yourself
- A product shows recommendations when a page loads, from features that change weekly. Batch or online? Answer: Batch. The features barely change within a day, so scores can be precomputed and looked up.
- A fraud check needs the items currently in the cart. Batch or online? Answer: Online, because the cart exists only at request time.
Key takeaways
- Batch is simpler and easier to replay. Online is required when features exist only at request time.
- The latency budget is a sum of parts, and the model gets what is left.
- Each online feature is an operational dependency.
- Package the model with pinned versions and a record of its training inputs.
Further learning
Next, check that training and serving compute the same features: training-serving skew and point-in-time features.
Related
ML in production
Feature stores, training-serving skew, monitoring for drift and the lifecycle of a deployed model.
Training-serving skew and point-in-time features
Training-serving skew happens when the code or data used to predict in production differs from the code or data used to train. Point-in-time joins and shared feature definitions prevent most of it.
Deployment strategies and safe releases
Rolling, blue-green and canary deploys, automated rollback, and zero-downtime schema changes - ship often without betting the fleet on every commit.