Sub topic 6
Where features come from at prediction time
A model only predicts well if it receives the same kind of inputs at serving time that it saw during training. This chapter covers where those inputs come from in batch and real-time systems, and why the source matters.
Prerequisites: you know what a feature is, can read basic Python and pandas, and have a rough idea of how an HTTP API and a scheduled job work.
Offline and online features
A feature is an input value the model uses, such as tenure_months or support_tickets. Where you read it from depends on how you serve the model.
- Offline features live in bulk storage: a data warehouse, a data lake, or Parquet files. You can read millions of rows at once. Reads are slow per request but cheap per row.
- Online features live in a low-latency store, such as a key-value database or cache. You look up one entity (for example one customer) by key and get an answer in milliseconds.
Batch jobs usually use offline features. Real-time services usually need online features, because they cannot scan a warehouse while a user waits.
How batch gets its features
A batch job reads a whole table in one pass. The batch example from this tutorial does exactly that:
import pandas as pd
customers = pd.read_parquet("customers_2026-11-28.parquet")
feature_columns = ["tenure_months", "monthly_spend", "support_tickets"]
X = customers[feature_columns]
print(X.shape)The features arrive as columns in a file or table. Joins, aggregations over months of history, and heavy transformations are all affordable here. The cost is that the data is only as fresh as the last time the table was built.
How real time gets its features
A real-time service has two options for each feature:
- The caller sends it in the request. This is simple and fast. The service trusts the caller to compute values correctly.
- The service looks it up. The request carries only an ID, such as
customer_id, and the service fetches the features from an online store.
Lookups add latency and a new dependency. If the online store is slow or down, predictions are slow or fail. Some features cannot be looked up cheaply at all, such as "average spend over the last 12 months" computed from raw transactions. Those must be precomputed and stored.
Comparing the two
| Aspect | Batch (offline features) | Real time (online features) |
|---|---|---|
| Read pattern | Scan many rows | Fetch one key |
| Typical latency per read | Seconds to minutes for the whole job | Milliseconds |
| Feature freshness | As of the last table build | As of the last store update or request |
| Complex aggregations | Easy | Must be precomputed |
| Extra infrastructure | Warehouse or files | Online store, plus a sync process |
The consistency problem
Offline and online features often come from two separate code paths. A data engineer writes SQL for the warehouse, and a service developer writes Python for the API. If they compute monthly_spend differently, the model sees different numbers in training and in production. This mismatch is called training-serving skew, and the next chapter covers it in detail.
Two common causes show up even in simple code. The first is column order. The online example builds a plain list, so the order matters:
row = [[features.tenure_months, features.monthly_spend, features.support_tickets]]If training used a different order, the model silently reads the wrong values. The second cause is a feature that exists offline but not online, for example a column built by a nightly join.
Practical guidance
- List every feature and mark whether it is available offline, online, or both.
- Avoid features that only exist in the warehouse if you plan to serve in real time.
- Share one definition of each feature between training and serving, ideally in a single piece of code or a feature store.
- Store the feature names and order alongside the model.
Exercise
A fraud model uses "number of transactions by this card in the last 10 minutes" and "customer's average monthly spend over 24 months." You must serve it in real time with a 100 ms limit. Which feature is harder to serve, and what would you do?
Show answer
The 24-month average is harder because computing it from raw transactions at request time would be too slow. Precompute it in a batch job, store it in an online store keyed by customer, and look it up per request. The 10-minute count changes constantly, so keep it updated by a streaming process or increment a counter in the online store as transactions arrive.