Sub topic 2
Freshness and Staleness of Predictions
Assume you know basic machine learning terms, Python, scheduled jobs, and HTTP APIs. This chapter focuses on how prediction data changes after a model is deployed.
Freshness and staleness
Freshness describes how recently a prediction used current input data. A prediction becomes stale when important inputs change after scoring.
Batch scoring creates predictions on a schedule, such as every night. It can efficiently score millions of rows, but each result may be hours old. Real-time scoring computes a result when a request arrives, so it can use newer information. It usually costs more per prediction and requires a continuously available service.
| Approach | Freshness | Typical use |
|---|---|---|
| Batch | Minutes to days old | Daily churn lists |
| Real time | Usually seconds old | Fraud checks at checkout |
Where features come from at prediction time
A feature is an input value used by a model. Batch jobs usually read features from a warehouse or data lake. Online services may read them from an online feature store, cache, or operational database.
The source must contain the same information and use the same time meaning as the training data. For example, a “last 30 days of purchases” feature must not include purchases made after the prediction time.
Avoiding training-serving skew
Training-serving skew occurs when a feature is calculated differently during training and prediction. Common causes include different code paths, time windows, missing-value rules, or feature ordering.
Store feature transformations in shared, versioned code when possible. Validate feature names, types, order, and ranges before scoring. Track the feature version with each model.
Cost and resource utilization
Batch workloads favor throughput: they keep compute busy while processing many rows. Online workloads favor low latency: they reserve capacity to answer individual requests quickly. Compare compute cost, peak traffic, storage, retries, and on-call work. Do not choose real time only because it sounds faster.
Building a batch scoring pipeline
A basic pipeline reads a partition of features, loads a versioned model, scores rows, and writes results with a scoring timestamp.
import joblib
import pandas as pd
model = joblib.load("model.joblib")
customers = pd.read_parquet("customers_2026-11-28.parquet")
features = ["tenure_months", "monthly_spend", "support_tickets"]
customers["churn_score"] = model.predict_proba(customers[features])[:, 1]
customers["scored_at"] = "2026-11-28T10:00:00Z"
customers[["customer_id", "churn_score", "scored_at"]].to_parquet(
"scores_2026-11-28.parquet"
)Exercise
A fraud score is used during checkout. Should the system use nightly batch scoring?
Show answer
No. Checkout needs current inputs, so an online scorer is more suitable. Batch results could still support reports or investigations.