Model serving
A model only creates value when its predictions reach a decision. Compare batch, online and streaming serving, roll out a new model safely with a canary release, and find where the milliseconds go in a real-time prediction.
In this lecture you will
- Choose between batch, online and streaming serving for a use case
- Package a model behind a versioned prediction API
- Roll out a new model safely with shadow, canary and automated rollback
- Break down prediction latency and meet a tail-latency objective
A trained model is a file. It creates value only when its predictions reach a decision: an offer in an app, a flagged payment, a ticket routed to the right team. Model serving is how predictions get there — reliably, fast enough, and without breaking anything when the model changes.
Batch, online or streaming?
There are three basic ways to deliver predictions. The right one depends on how fresh a prediction must be when it is used.
- Three patternsPredictions can be computed ahead of time, on request, or on every event. Each suits a different kind of decision.
- BatchA scheduled job scores every customer overnight and writes the results to a table. Applications simply look them up the next day. Cheap, simple and easy to check.
- OnlineAn always-on API computes a prediction for each request, using the latest features from an online store, and answers in tens of milliseconds.
- StreamingA stream processor scores events — such as card payments — as they arrive, and acts within seconds: here, a suspicious payment is flagged.
- Choose the simplestAll three can run side by side. Use the simplest pattern whose predictions are fresh enough for the decision they drive.
| Batch | Online | Streaming | |
|---|---|---|---|
| Freshness | Hours to a day | Now | Seconds |
| Typical use | Churn lists, recommendation e-mails, demand forecasts | Search ranking, pricing, credit decisions | Fraud detection, anomaly alerts |
| Cost and effort | Low | Medium — always on | Highest — streaming infrastructure |
Packaging a model as a service
For online serving, the model is wrapped in a small web service with a versioned API and a strict input schema, packaged as a container, and deployed like any other service. Every response should say which model version produced it, and every prediction should be logged with its inputs — you will need them for monitoring and debugging.
from fastapi import FastAPI
from pydantic import BaseModel
import joblib
app = FastAPI()
model = joblib.load("churn-model-v7.joblib") # pulled from the model registry at build time
class Customer(BaseModel):
orders_30d: int
avg_basket_usd: float
days_since_last_order: float
@app.post("/v1/churn")
def predict(c: Customer):
p = model.predict_proba([[c.orders_30d, c.avg_basket_usd, c.days_since_last_order]])[0, 1]
return {"churn_probability": round(float(p), 4), "model_version": "v7"}
- Model server
- A service that loads a model and answers prediction requests, such as a FastAPI app, KServe, Triton or a managed endpoint.
- Shadow deployment
- Sending a copy of live traffic to a new model and logging its predictions without using them.
- Canary release
- Sending a small, growing share of real traffic to a new version while comparing it with the stable one.
- Rollback
- Returning all traffic to the previous version, ideally automatically.
- SLO
- Service-level objective: a measurable target such as “99% of predictions within 150 ms”.
Rolling out a new model safely
A model that scored better offline can still fail in production: a missing feature, a new library, a slower instance type. Safe rollouts limit the blast radius. A shadow deployment first runs the new model on copied traffic with no effect on users. A canary release then sends it a small share of real traffic, compares it with the stable model on guardrail metrics — errors, latency, business KPIs — and increases the share step by step.
Try the automated rollout with each version of v2. Red dots mark failed requests; they are shown more often than they really occur, so they are easy to see. The panels use every simulated request.
Notice that at 5% traffic the canary’s numbers are noisy: small samples need time before a decision is trustworthy. Real rollout tools wait for a minimum number of requests at each step, and compare versions with a statistical test rather than a single threshold.
Where the milliseconds go
Online predictions usually have a latency objective, stated for the tail — for example, 99% of requests within 150 ms. Break a request into its stages and see which choices blow the budget.
The numbers are illustrative but typical in proportion. Try a transformer on CPU, then on GPU, then distilled; compute features per request; add a cache; switch to peak load.
The biggest levers are usually the model (smaller, distilled or quantized models, or a GPU), the feature source (precomputed features in an online store instead of computing them per request) and capacity (enough replicas and autoscaling so requests do not queue at peak).
Once a model is live, the work changes from shipping to watching. The last lecture of this module covers monitoring and drift.
Key takeaways
- Batch scoring is the simplest and cheapest pattern; use online or streaming serving only when decisions need fresher predictions.
- Package models as versioned, containerized services with a clear input schema, and log every prediction with its model version.
- Never switch all traffic at once — shadow and canary releases expose a new model to real traffic gradually, with automatic rollback.
- Design for the tail (p99), not the average; model size, feature retrieval and load usually dominate it.
Check your understanding
4 questions · answers explainedQ1A retailer emails personalized offers every Monday. Which serving pattern fits best?
Q2During a canary release, the new model’s error rate is 4% while the stable model’s is 0.3%. What should an automated rollout do?
Q3A prediction cache with a 60% hit rate is added. What happens to the p99 latency?
Q4What is a shadow deployment?