ML Model Serving: Batch vs Real-Time Inference

ML model serving explained: batch vs online inference, latency budgets, dynamic batching, GPU utilisation, model versioning, shadow and canary deployments.

Intermediate 3 min read · Updated

Model serving is how a trained model produces predictions for real users and systems. Predictions can be made in batch, precomputed for many entities on a schedule, or online, computed per request within a latency budget. Online serving adds the problems of any low-latency service (scaling, tail latency, failures) plus ML-specific ones: fetching features, using accelerators efficiently, and rolling out new model versions safely.

Batch vs online inference

Batch inferenceOnline (real-time) inference
When predictions are madeOn a schedule, for all or many entitiesPer request, when the user or system asks
LatencyMinutes to hours; nobody waitsMilliseconds to a second
FreshnessAs old as the last runUses the latest request and context
Cost profileEfficient: large batches on cheap capacityMust be provisioned for peak traffic
Failure impactA delayed run; serve the previous resultsRequests fail or fall back immediately
ExamplesNightly churn scores, weekly product recommendations by emailFraud checks at payment time, search ranking, chat responses

Many systems mix both: precompute what you can (embeddings, candidate lists, scores for inactive users), and compute online only what depends on the moment. A third option is streaming inference, where a stream processor scores events as they arrive and writes results to a store.

An online serving path

loadCallerapp · APIModel servicevalidate · route versionOnline featureskey-value lookupInference serversdynamic batching · GPU/CPUModel registryartifacts · versionsPrediction log
A prediction request: the caller hits a model service, which fetches features, batches inference on accelerators and logs inputs and outputs for monitoring.

Latency and throughput

  • Budget the whole path: network, feature lookup, preprocessing, inference and postprocessing. A 100 ms budget might leave 40 ms for the model itself.
  • Dynamic batching: accelerators are efficient on batches, but requests arrive one at a time. Inference servers queue requests for a few milliseconds and run them together, trading a little latency for much higher throughput.
  • Model optimisation: quantisation (lower-precision weights), distillation into a smaller model, compiled runtimes and pruning cut latency and cost.
  • Tail latency: a slow feature store or garbage-collection pause shows up at p99. Set timeouts and a fallback for every dependency; see timeouts and retries.
  • Right hardware: small tree models are cheapest on CPUs; large neural networks need GPUs or other accelerators, which are expensive to leave idle.

Deploying new versions safely

StrategyHow it worksWhat it catches
Shadow (dark launch)New model receives a copy of live traffic; its predictions are logged, not usedCrashes, latency and prediction distribution differences, with no user impact
CanaryA small percentage of traffic uses the new model, rising if metrics holdReal-world regressions at limited blast radius
A/B testUsers are randomly split between models and business metrics are comparedWhether the new model actually improves outcomes
Blue-greenSwitch all traffic to a fully provisioned new version, keeping the old one for rollbackFast cutover and rollback

Version every model artifact in a model registry, keep the previous version warm for instant rollback, and record which model version made each prediction. See also deployment strategies and A/B testing.

Failure handling

  • Fallbacks: if the model times out, return a simpler model's answer, a cached prediction, or a safe default (allow the payment and flag for review, show popular items).
  • Input validation: reject or default malformed inputs rather than producing confident nonsense.
  • Autoscaling: scale on queue depth or GPU utilisation, and keep warm capacity because model loading can take minutes. See autoscaling.
  • Monitoring: watch latency, error rates and prediction distributions, and feed logged predictions into model monitoring.

Where it’s used

Payments fraud

Online models score each transaction within tens of milliseconds, falling back to rules if the model is unavailable.

Marketing

Batch jobs score every customer nightly for churn risk and write results to the CRM.

Search and feeds

Ranking models score hundreds of candidates per request with dynamic batching on GPUs.

In an interview

  • Choose batch or online from freshness and latency needs, and precompute where possible.
  • Break down the latency budget and mention dynamic batching.
  • Roll out with shadow traffic and canaries, with instant rollback.
  • Define a fallback for timeouts and log every prediction with its model version.

Further reading