Sub topic 6
Limiting Staleness with TTL and Join Tolerance
Assume you know basic supervised machine learning, pandas DataFrames, SQL joins, window functions, and timestamps. This chapter focuses on one decision in a point-in-time join: how old a feature value may be before it becomes unsafe to use.
Why an as-of match can still be wrong
An as-of join selects the newest feature value at or before the prediction timestamp. That prevents future information from entering the row. However, the newest available value may still be very old.
Suppose a customer has a feature value from January 1 and a prediction timestamp on March 15. The value is earlier than the prediction, so it passes the time-order rule. But a feature that is 73 days old may not describe the customer accurately enough for the model.
A time-to-live (TTL) is the maximum age allowed for a feature value. Join tolerance is the implementation of that rule in a data join. If no feature falls within the tolerance, the joined feature should be missing rather than silently using a stale value.
Choosing a tolerance
The right TTL depends on how often the source changes and how quickly the feature becomes misleading.
| Feature | Possible TTL | Reason |
|---|---|---|
| Current account balance | 1 day | The value can change quickly |
| Weekly activity count | 8 days | One missed refresh should not remove every value |
| Customer profile category | 90 days | The value changes slowly |
A short TTL reduces stale data but creates more missing values. A long TTL provides better coverage but may let outdated information influence predictions. Measure both the missing-feature rate and model performance at different TTL values.
The TTL should also account for expected data delays. If a daily feature normally arrives within a few hours, a 2-day tolerance may handle ordinary delays. It should not be used to hide a broken pipeline indefinitely.
Applying tolerance in pandas
pandas.merge_asof supports a tolerance argument. With direction="backward", it selects the most recent feature at or before the prediction time. A feature older than 30 days becomes missing.
The data must be sorted by the time columns before the join.
import pandas as pd
# labels: customer_id, prediction_ts, churned
# features: customer_id, feature_ts, avg_spend_30d
labels = labels.sort_values("prediction_ts")
features = features.sort_values("feature_ts")
train = pd.merge_asof(
labels,
features,
left_on="prediction_ts",
right_on="feature_ts",
by="customer_id",
direction="backward",
tolerance=pd.Timedelta("30D"),
)A missing avg_spend_30d means that no value was both available by the prediction time and fresh enough. Decide how the model handles this explicitly. Options include an imputation value, a missingness indicator, or leaving the value missing if the model supports it.
Applying tolerance in SQL
In SQL, put the lower time bound in the join condition. The feature must be no later than the prediction time and no older than the allowed interval.
WITH candidates AS (
SELECT
l.customer_id,
l.prediction_ts,
l.churned,
f.avg_spend_30d,
f.event_ts AS feature_ts,
ROW_NUMBER() OVER (
PARTITION BY l.customer_id, l.prediction_ts
ORDER BY f.event_ts DESC
) AS rn
FROM labels AS l
LEFT JOIN feature_history AS f
ON f.customer_id = l.customer_id
AND f.event_ts <= l.prediction_ts
AND f.event_ts > l.prediction_ts - INTERVAL '30 days'
)
SELECT customer_id, prediction_ts, churned, avg_spend_30d, feature_ts
FROM candidates
WHERE rn = 1;The exact interval syntax differs between database systems. Keep the TTL in configuration or a versioned query parameter so that the training and serving systems use the same policy.
Validate freshness after the join
Calculate the age of every matched feature. This exposes unexpected gaps and confirms that the join honored the TTL.
matched = train.dropna(subset=["feature_ts"]).copy()
matched["feature_age"] = matched["prediction_ts"] - matched["feature_ts"]
assert (matched["feature_age"] <= pd.Timedelta("30D")).all()
assert not train.duplicated(["customer_id", "prediction_ts"]).any()Also compare the missing rate by date, customer group, and feature. A sudden increase can indicate a delayed source, a changed timestamp, or an overly strict TTL.
Exercise
Why should a stale feature become missing instead of being joined automatically?
Show answer
A stale value may no longer represent the entity at prediction time. Making it missing exposes the coverage problem and lets the model or pipeline handle it explicitly.