Learning path: Serve a Model in Production: Docker, Latency and Scaling · Step 1 of 9
Online, Batch or Streaming? Picking the Serving Pattern for an ML Model
Before writing any API code, decide how a model's answers should reach people. Two questions, who is waiting and when is the input known, pick between an online API, batch scoring and streaming.
What you will learn
- Tell online, batch and streaming serving apart
- Pick a pattern with two questions: who is waiting, and when is the input known
- Name what each pattern costs to run
- Place real cases, including mixed ones, in the right pattern
Before you start
- Deploy a Machine Learning Model with FastAPI
The model is trained and saved. Now, how should its answers reach people? It's tempting to start writing an API straight away, but pick the serving pattern first. There are three, and two small questions decide between them.
Three kitchens
Think of three kitchens serving the same food.
- Cook to order. An order comes in, the cook makes it right then, and the plate goes straight back out. Somebody is always waiting for each plate. That's an online API.
- Meal-prep Sunday. You cook once, store the lunches in the fridge, and during the week a lunch is just taken out. Nobody waits, because it's ready when you ask. That's batch scoring.
- A conveyor-belt sushi bar. The plates never stop moving, and people pick the ones they want as they pass. That's streaming.
Same food, three very different ways of serving it.
Two questions pick the pattern
- Is someone waiting for the answer?
- If yes: how does the input arrive? One request at a time, when a person asks, means online. Events that keep flowing, one after another, means streaming.
- If nobody is waiting: when is the input known? If it's known ahead of time, score the whole table on a schedule. That's batch.
Online: one request in, one answer out
Three people, Anna, Ben and Chloe, each send their inputs to the rain API and wait for an answer. Every one of them is waiting while the model runs.
What does that cost you? The service must be always running, it must answer quickly, and it must handle spikes when many people ask at once. The rain API from Deploy a Machine Learning Model with FastAPI is an online service, and A Production FastAPI for a Model makes it ready for real traffic.
Batch: score everything ahead of time
Take a table of towns where every input is known. A scoring job runs on a schedule, say every night. It loads the model once and goes through the rows: Leeds is rain, Bath is dry, and so on. Each answer is saved into a predictions table. (The towns are just an example.)
Later, when the rain app needs Bath, it simply looks the answer up. No model runs at that moment, so the answer is instant.
The catch: if a new town, say Newtown, shows up after the job has run, it has no answer yet. It waits for the next run. So batch answers are fast and cheap, but they can be a little old. Batch Scoring builds such a job on a million rows.
Streaming: events keep coming
Think of sensors on a machine that send readings all day. Each reading flows into a scorer, which scores it as it arrives. Normal readings go to a log, and nothing else happens. When one looks wrong, an alert goes out right away. Nobody asked; the model simply reacted.
Often one reading isn't enough, so the scorer keeps a small window of the last few readings, and that window slides along as new ones arrive. Streaming gives you quick reactions, but you also need a place for events to flow, and some memory of the recent past.
Five cases, three lanes
| Case | Who waits? When is the input known? | Pattern |
|---|---|---|
| Approving a card payment at the till | A person is waiting; the input arrives with the request | Online |
| A rain forecast for every town each morning | Every input is known the night before | Batch |
| A weekly list of customers likely to leave | The team reads it on a schedule | Batch |
| A machine sensor that starts to look wrong | Readings never stop; an alert should be quick | Streaming |
| Home-page picks for each shopper | Computed ahead, looked up when the page loads | Batch, with a small online part |
The last one shows that many real systems mix patterns.
Climb a step only when the need forces you
Think of the patterns as steps on a staircase:
- Batch is the bottom step. It's a scheduled job, and if it fails, you rerun it. Start here.
- Online is higher. You need a server that's always up, fast answers, and a way to scale with traffic.
- Streaming is the top step. Everything online needs, plus a stream platform and memory of recent events.
Rule of thumb: if nobody is waiting, start with batch. This is advice, not a statistic. The Google Cloud documentation for Vertex AI makes the same split: online inference when requests answer application input, batch inference when you don't need an immediate response.
The simplest pattern that meets the need is the right one.
Common mistakes
- Building an API by default. If the inputs are known the night before, an always-on server is cost and risk you don't need.
- Forgetting that batch answers age. A row that arrives after the run has no answer until the next run.
- Calling anything fast "streaming". Streaming means events keep flowing in and the model reacts without being asked. A quick API is still online.
- Forcing one pattern on a mixed system. Compute ahead where you can, look up online where you must.
Quick recap
| Pattern | Who is waiting | What it costs |
|---|---|---|
| Online | A person, for each request | Always up, fast, handles spikes |
| Batch | Nobody; answers are looked up later | A scheduled job; answers can be a little old |
| Streaming | Nobody asks; the model reacts to events | A stream platform and a window of recent events |
Try it yourself
Pick one model you know, from a course or from life. Ask who is waiting, and when the input is known. Then name its pattern.
Built this? Share your solution in the StudyEasy AI Community.
Join free on Skool →StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Machine Learning Engineer: From First Model to ML Engineer
Learn machine learning in Python, then ship it: data analysis, regression, classification, tuning, deployment and MLOps.View on Udemy ↗: Machine Learning Engineer: From First Model to ML Engineer (opens in a new tab)Related articles
Python Virtual Environments: One Project, One Toolbox
Why every Python project gets its own virtual environment: two projects that need different versions of requests, python -m venv .venv, activation, pip install and the PyCharm setting that activates it for you.
Git and GitHub Fundamentals: Commits, Branches, Push and Pull
The Git you use every day, on a tiny hello-ml project: commits as save points, the staging area, a branch merged with a fast-forward, then push, clone, pull and pull requests with a teammate.
Testing a FastAPI App with pytest: Three Small Tests
A tiny FastAPI app with a Hello World route and a name route that refuses numbers, three pytest tests that check it, and what happens when the validation is deleted by mistake.
Config and Logging Instead of Hard-Coded Values in ML Training
Take the hard-coded values out of a car price training script: one config.toml read with tomllib, logging with levels and a run id on every line, and secrets read from the environment. Same RMSE, 2195, before and after.