Sub topic 14
Choosing between batch and real time
This chapter assumes you know basic ML vocabulary (training, features, prediction), can read Python, and understand HTTP APIs and scheduled jobs. It also assumes you have seen the two serving patterns. Batch prediction scores many records on a schedule. Real-time prediction scores one request when it arrives. Here you will turn the earlier comparisons into a decision process.
Start with the questions, not the tool
Teams often pick real time because it feels modern. That choice adds an always-on service, a low-latency feature path and on-call duty. Pick it only when the use case needs it. Work through these questions in order.
- Who consumes the prediction, and when? If a person or system reads it later, such as a daily email list or a dashboard, batch is enough. If a user is waiting on a screen, you need real time.
- How fast do the inputs change? If the features move slowly (tenure, plan type), a prediction made last night is still valid. If they change by the minute (the current cart, a card swipe), a stale score is wrong.
- Is the full set of inputs known in advance? Batch needs to know which records to score. If requests carry new data that you cannot predict, such as a free-text query, you must score live.
- How tight is the latency budget? Real time usually means tens to hundreds of milliseconds for the whole request, including feature lookup.
- Can you get the same features online? A real-time service needs features at request time. If they come from a heavy warehouse query, you cannot serve them live without building an online feature path. Computing features differently in training and serving causes training-serving skew.
- What does it cost? Batch pays for compute only while it runs. An online service pays for idle capacity, so it is wasteful when traffic is low or spiky.
- What happens on failure? A failed batch job can be rerun. A failed online service breaks a user-facing request right away.
Mapping answers to a pattern
The table gives the same guidance in a compact form.
| Situation | Typical choice | Reason |
|---|---|---|
| Weekly churn list for the retention team | Batch | Nobody waits, features change slowly |
| Fraud check during a card payment | Real time | A user waits and the data is brand new |
| Product recommendations on a home page | Hybrid | Most users are known, new users need a fallback |
| Inventory risk scores refreshed every few minutes | Micro-batch | Fresh enough, cheaper than a live service |
A hybrid in code
The hybrid pattern returns a precomputed score when it exists. Otherwise it scores live. The function also reports which path it used, so you can monitor the share of fallbacks.
def get_score(customer_id, precomputed, live_predict, features_for):
score = precomputed.get(customer_id)
if score is not None:
return score, "batch"
return live_predict(features_for(customer_id)), "realtime"
precomputed = {"c1": 0.12}
def live_predict(features):
return 0.5
def features_for(customer_id):
return [10, 40.0, 1]
print(get_score("c1", precomputed, live_predict, features_for))
print(get_score("c2", precomputed, live_predict, features_for))This prints (0.12, 'batch') and then (0.5, 'realtime'). If the fallback share grows, the batch job may be missing records, and you should check it.
Common mistakes
- Choosing real time for a daily decision. If the business acts on the score once a day, a live service adds cost and risk and gives no benefit.
- Choosing batch when freshness matters. A score computed overnight cannot reflect what the customer did this morning.
- Ignoring the feature path. Teams build a fast model endpoint and then discover that the features take seconds to compute.
- Treating the choice as permanent. Many systems start with batch and add a live path for the few cases that need it.
Exercise
A bank wants to score each loan application while the applicant is on the web form, using the answers they just typed plus their stored credit history. Which pattern fits, and what must you check before you build it?
Show answer
Real time, because a person is waiting and the inputs (the typed answers) are not known in advance. Before building it, check that the stored credit history features are available online within the latency budget, and that they are computed the same way as in training to avoid skew.
Exercise
A retailer scores 2 million customers for a weekly email campaign. A manager proposes a real-time API "for flexibility". Give two reasons to keep batch.
Show answer
Nobody waits for the result, and the features change slowly, so weekly scores are fresh enough. Batch also costs less, because compute runs only during the job instead of idling in an always-on service, and a failed run can simply be rerun.