07 · Serving and scale

Batch, online, and streaming inference

9 min read

Serving is the part of an ML system that hands the model’s prediction to the product. The first serving decision is when that prediction gets computed. It can be worked out ahead of time for everyone, worked out at the moment someone asks, or kept up to date as events arrive. These three choices are called batch, online, and streaming inference, and the right one depends on how fast the inputs change and how long the product can wait for an answer.

An everyday example

A bakery has the same decision to make every morning. It can bake 200 loaves before opening, which is cheap and quick to sell but leaves unsold bread at closing time and nothing fresh for the customer who wanted rye. It can bake each loaf to order, which gives every customer exactly what they asked for and makes them wait 40 minutes. Or it can bake a stock before opening and adjust the next batch through the day as it watches what sells, which is the pattern a busy bakery settles on.

A recommendation system faces the same trade. Scoring every user overnight is the baked-ahead shelf, scoring at the moment a request arrives is baking to order, and updating a stored result as a user clicks through the day is the adjusted batch. The analogy breaks on speed, because a computer can bake to order in a tenth of a second, so the choice is about freshness and cost rather than about whether the customer will wait. That is why the sections below keep returning to two questions, how stale the answer may be and how much computation is wasted on people who never ask.

Batch, online, and streaming inference

Batch inference scores a whole collection ahead of use, such as preparing a list of ten recommendations for every user during the night. When a user opens the app the next morning, the service reads the prepared list, which takes a few milliseconds, and the model does no work at that moment. The costs are freshness, since the list cannot know what the user did five minutes ago, and waste, since most of the scored users will not open the app tomorrow.

Online inference computes the prediction for the current request, at the moment it arrives. It can use the last thing the user did, but everything has to happen inside the response deadline, which for a web page is usually a few hundred milliseconds. That includes the lookup of the inputs the model needs, which are called features, the model itself, and every other service it has to call. Streaming inference sits between the two. It reacts to arriving events, such as a purchase or a skip, and updates a stored prediction or a stored summary of the user, so the next read sees something recent without running the full model on every request.

The table below is the comparison a designer makes in the first minute. The useful question for each row is which inputs change between one request and the next, because an input that does not change can be computed ahead and an input that changes every second cannot.

Where the prediction gets computedPrediction needed, Shopper opens the app. Prepared earlier, Read a stored result. Computed now, Run the model. Streaming keeps the stored result on the upper path fresh as events arrive.Where the prediction gets computedPrediction neededShopper opens the appPrepared earlierRead a stored resultComputed nowRun the model
Streaming keeps the stored result on the upper path fresh as events arrive.On a narrow screen, scroll the figure sideways to see every step.
Batch, online, and streaming inference
PatternUseful whenMain cost
BatchInputs change slowly and results can be prepared overnightStale answers and work spent on users who never return.
OnlineThe current request changes the answerEvery lookup sits inside the deadline, and a slow dependency slows the page.
StreamingEvents should update the stored answer within secondsLate or duplicated events and a processing lag to watch.

What a nightly batch costs

Assume a service with 2 million registered users and a model that takes 50 milliseconds to score one user on one machine. Those are assumptions for this exercise, so state them out loud in an interview. Scoring everyone takes 100,000 seconds of machine time, which is nearly 28 hours, so a single machine cannot finish before morning and the job needs at least four machines running in parallel to fit in a seven-hour night.

Assume further that only 400,000 of those users, one in five, open the app on a given day. Then four fifths of the night’s computation produced lists nobody read, and a design that scores only users active in the last 30 days, or that scores on request, spends far less. The number that decides between batch and online is this ratio of users scored to users served, and a designer who says it aloud has shown the trade-off without naming a single tool.

A night of scoring and a morning of readsNight, Score every user. Dawn, One list per user stored. Morning, User opens the app. Same second, Stored list read, no model. All the model work happens hours before anyone asks.A night of scoring and a morning of readsNightScore every userDawnOne list per userstoredMorningUser opens the appSame secondStored list read, nomodel
All the model work happens hours before anyone asks.On a narrow screen, scroll the figure sideways to see every step.
2,000,000 users × 50 ms = 100,000 s ≈ 27.8 hours on one machine
100,000 s / 7 hour night ≈ 4 machines in parallel
400,000 active users / 2,000,000 scored = 20 percent of the work is used

A hybrid recommendation request

A music home page combines the patterns instead of choosing one. During the night it prepares the slow-changing parts, such as a numeric summary of each listener’s long-term taste and a numeric summary of each song, and stores them. When a request arrives, a small model reads those stored summaries together with what the listener did in the last few minutes, such as skipping two songs, and orders the page. The listener gets a page that reflects the current session without the service having to read their whole listening history on every request.

The design question in a hybrid is which pieces may be stale and by how much. A song’s summary can be a week old without harm, while a flag that says the song has been removed from the catalog must be current. One rule follows from that, which is that permission and deletion checks never hide behind a long-lived recommendation cache, a store of recent answers kept so a repeated request is answered without recomputing, even when the ranking itself is allowed to be a day old.

Prepare stable work, score the current contextSmall ranker, Combines both inputs. Stored summaries, Computed overnight. Live request, The last few minutes. The two inputs have different ages, hours for one and seconds for the other.Prepare stable work, score the current contextStored summariesComputed overnightLive requestThe last few minutesSmall rankerCombines both inputs
The two inputs have different ages, hours for one and seconds for the other.On a narrow screen, scroll the figure sideways to see every step.

Define the serving contract

A serving contract is the written agreement between the product and the model service about what a request contains, what comes back, how long it may take, and what happens when something is wrong. The service checks that a request has the fields it expects and rejects a malformed one with a clear error, because a model handed a missing field will still return a number and that number means nothing. Each response carries the model version and the feature version that produced it, so that a complaint next week can be traced to the exact thing that was running.

Batching means scoring several items in one call to the model instead of one at a time, and the contract should say which kind is meant. Batching the 500 candidates of one request together is almost free, because they are already waiting. Batching requests from different users together makes the hardware more efficient but forces the first request to wait for the others to arrive, and that waiting comes out of the response deadline.

Before trusting the chosen pattern, a team tests it on realistic traffic with an empty cache and with one dependency switched off, and measures waiting time separately from computing time. For a product that chooses among millions of items, the amount of model work also depends on how the candidates were narrowed down first, which is the subject of the next topic.

Where people get confused

Newcomers hear that online inference is more accurate and treat batch as the inferior option. Batch is the better design whenever the inputs change slowly, because the same prediction costs less and the page loads faster, and a nightly list of films to watch loses nothing by being eight hours old. Online wins only when something in the current request, such as a search phrase or the last click, changes the answer.

Streaming inference is also taken to mean running the model on every event. In most designs the events update a stored summary, such as the count of items viewed in the last hour, and the model runs later, either on request or on a schedule. The streaming part is the bookkeeping, and the model work can still be batch or online.

The cache gets treated as part of the model, when it has its own rules about how old an entry may be and when it must be thrown away, and those rules belong to the product, since a stale film list is harmless and a stale permission check is a security bug.

In the interview

Right after the pipeline sketch and before the data discussion, the inference pattern takes two to three minutes. Name the two questions, how fresh the answer must be and how much of the scored work will be used, and then choose a pattern with one sentence of reasoning, such as scoring active users nightly and reranking on request. If the interviewer wants the detail of the hybrid or the cache rules, offer to come back to it in the deep dive rather than spending the data minutes on it.

Interview questions

Why not precompute recommendations for every user every minute?

I would start by estimating the work. With 2 million users and 50 milliseconds per user, one pass takes about 28 machine-hours, so running it every minute would need over 1,600 machines doing nothing but scoring, and most of that work would be for users who are asleep.

Instead I would refresh the slow-moving parts, such as long-term taste summaries, once a night, update a stored summary when a user does something meaningful, and run a small model at request time to react to the current session. I would keep a simple popular-items list for users who have no prepared result yet.

That costs engineering complexity, since three paths exist instead of one, and it buys a tenth of the compute and a page that still reacts to the last click.

When would you choose online inference over batch for this product?

I would choose online inference when the request itself carries information that changes the answer, such as a search phrase, a shopping cart, or the location of a delivery, because no overnight job could have known it.

The cost is that every lookup and the model itself must finish inside the deadline, which I would set at about 200 milliseconds for a page element, and a slow dependency then slows the page for every user. For a product where the inputs change weekly, I would keep batch and spend the saved effort elsewhere.

What goes stale in a batch system, and how do you handle it?

Two things go stale. The prediction itself misses anything the user did after the job ran, and the catalog it was drawn from misses items added or removed since. I would handle the first with a light request-time step that reorders the stored list using the last few actions, and the second with a current eligibility check that removes sold-out or deleted items before the list is shown.

The check matters more than the reorder, because showing a removed item is a visible bug while a slightly old ordering is not. I would state that the stored list may be up to 24 hours old and the eligibility check is never cached.

What to practice next

These problems let you build the request-time feature lookup, pick the top items from a batch of scores, and feed examples through in batches, which are the three pieces a hybrid serving path is made of.