Sub topic 5
Freshness and staleness of predictions
This chapter assumes you know basic machine learning vocabulary (model, features, prediction), can read Python, and understand scheduled jobs and HTTP APIs. It covers how old a prediction can be when someone uses it, and what that costs you.
What staleness means
A prediction is stale when the world has changed since it was computed. Staleness is the time between the data a prediction was based on and the moment the prediction is used. Freshness is the opposite: a fresh prediction reflects recent data.
Staleness has two parts:
- Input age: how old the feature values were when the model read them.
- Storage age: how long the finished score sat before someone used it.
A real-time service has almost no storage age. It scores on demand, so the score is used right away. Its input age depends on how recently the feature values were updated. A batch job has both. The nightly job reads yesterday's data, and the score then waits until the next run.
Worst-case staleness in batch
Suppose a batch job runs once a day at 02:00 and reads data up to 01:00. A customer's score is used at 23:00 the same day. That score is based on data that is 22 hours old at the time of use. Just before the next run, the age approaches 25 hours.
The worst case is roughly the run interval, plus the age of the input data, plus the time the job takes to finish. Running the job more often shortens the interval, but each run costs compute.
from datetime import datetime, timedelta
def worst_case_staleness(run_interval, data_lag, job_duration):
return run_interval + data_lag + job_duration
worst = worst_case_staleness(
run_interval=timedelta(hours=24),
data_lag=timedelta(hours=1),
job_duration=timedelta(minutes=30),
)
print(worst) # 1 day, 1:30:00Measuring staleness
Store a timestamp with every score. Then the consumer can compute age at read time and decide whether to trust it.
import pandas as pd
scores = pd.DataFrame({
"customer_id": [1, 2],
"churn_score": [0.82, 0.15],
"scored_at": pd.to_datetime(["2026-11-28 02:30", "2026-11-27 02:30"]),
})
now = pd.Timestamp("2026-11-28 10:00")
scores["age_hours"] = (now - scores["scored_at"]).dt.total_seconds() / 3600
print(scores[["customer_id", "age_hours"]])Customer 2's score is about 31.5 hours old, which suggests the last batch run missed that row or failed. Without the timestamp, this problem is invisible.
When staleness matters
Staleness only matters if the underlying signal changes quickly relative to the age of the score. Consider these cases:
| Use case | How fast the signal changes | Tolerable staleness |
|---|---|---|
| Monthly churn outreach list | Weeks | Days |
| Product recommendations on a home page | Hours | Hours |
| Card fraud check | Seconds | Seconds or less |
Ask what decision the score drives and what would change in the last few hours. If nothing meaningful would change, a stale score is fine and batch is cheaper.
Common errors
- Measuring only the schedule. Teams say "we run daily" and forget input lag and job duration.
- Fresh model, stale features. A real-time endpoint that reads features from a table refreshed nightly is still a day stale.
- Silent failures. If a batch run fails, consumers keep reading old scores without any error. Check
scored_ator the run date. - Treating all rows alike. Active users may need fresher scores than dormant ones.
Exercise
A batch job runs every 6 hours, reads data that is 2 hours behind real time, and takes 45 minutes to finish. What is the worst-case staleness of a score at the moment it is used?
Show answer
8 hours 45 minutes. Add the run interval (6 hours), the data lag (2 hours) and the job duration (45 minutes).
Exercise
Your real-time API reads features from a table that a nightly job refreshes. Are its predictions fresh? Why or why not?
Show answer
No. The model runs on demand, but its input features can be up to a day old, so the predictions are as stale as the feature table.