Sub topic 14
TTL, staleness and keeping training and serving consistent
This chapter assumes you can define an entity, a data source and a feature view. It also assumes you have run feast apply, built a training set with get_historical_features, and materialized values into the online store. Here you will see how the ttl setting and the materialization schedule determine whether the values a model sees in production match the values it was trained on.
What "stale" means in Feast
A feature value is stale when the world has changed but the value you read has not. Feast does not track the world. It only compares timestamps:
- The event timestamp of a feature row says when the value was true.
- The entity timestamp in a training request says when the prediction would have been made.
- The current time matters at serving time.
- The TTL (time to live) on the feature view sets the maximum age a value may have and still be used.
The offline and online paths apply TTL differently, and that difference is the main source of surprises.
TTL in historical retrieval
For each row in your entity dataframe, get_historical_features looks for the latest feature row at or before the row's event_timestamp. It then checks that this feature row is no older than ttl relative to the entity timestamp. If it is older, the feature value comes back as null.
With ttl=timedelta(days=1) in the feature view from earlier chapters, take an entity row at 2025-01-03 12:00. The driver's latest stats row is from 2025-01-01 09:00, which is more than a day earlier. The returned conv_rate is null, even though a value exists.
Two consequences follow:
- A TTL that is too short produces nulls in training data. Your model then learns from missing values that may not occur in production, or the reverse.
- A TTL of zero means values never expire. This is simple, but Feast must then scan back through all history for each entity row, which can make the query slow on large sources.
TTL and materialization
The online store holds one row per entity: the latest value Feast has copied in. TTL influences that copy in two ways.
- When you run
feast materialize-incrementalfor a feature view for the first time, Feast has no previous run to start from. It uses roughlynow - ttlas the start of the window. A short TTL therefore means a short initial backfill, and older rows are never loaded. - Later incremental runs start where the previous run ended, so they pick up only new rows.
Materialization is a batch copy. Between runs, the online store does not change, so the freshness of a served value is bounded by two things: how often you materialize, and how recent the newest row in the offline source is.
TTL in online retrieval
How get_online_features treats an old value depends on your Feast version and online store. Recent versions compare the stored event timestamp with the current time. If the value is older than the TTL, they return null and mark the field's status as outside its maximum age. Older versions return whatever is stored. Do not rely on either behavior without testing it against your installed version.
Whatever your version does, design as if staleness is your responsibility:
- Choose a TTL that matches how long a value stays meaningful for the model. Hourly driver stats might tolerate a day. A "number of clicks in the last 5 minutes" feature does not.
- Schedule materialization more often than the TTL, with margin. If TTL is 24 hours and you materialize every 24 hours, a single delayed job leaves the online store with expired values.
- Handle nulls in the serving code, either with a documented default or by falling back to a safe model path.
Sources of training-serving skew
Skew means the model sees different feature values in production than it saw in training for the same situation. TTL and freshness are only some of the causes.
| Cause | Symptom | Mitigation |
|---|---|---|
| Entity timestamps in training use the current time instead of the label time | Training data includes information from after the event (label leakage) | Use the timestamp of the prediction or label event in `entity_df` |
| Online store updated less often than assumed in training | Served values are older than training values | Schedule materialization against a freshness target and monitor lag |
| TTL differs in effect between paths | Nulls in one path but values in the other | Check null rates in both paths and review the TTL |
| Features computed by different code offline and online | Same entity, different numbers | Define the logic once, in the feature view or an on-demand feature view |
| Different feature lists at training and serving | Missing or reordered inputs | Use one feature service for both paths |
| Duplicate rows with the same event timestamp | Nondeterministic value choice | Set `created_timestamp_column` so Feast picks the most recently created row |
Using one feature service for both paths
A feature service names a fixed set of features. Passing the same object to both retrieval calls removes the chance of listing features differently in two places.
from datetime import datetime, timezone
import pandas as pd
from feast import FeatureStore
store = FeatureStore(repo_path=".")
service = store.get_feature_service("driver_activity_v1")
entity_df = pd.DataFrame(
{
"driver_id": [1001, 1002],
"event_timestamp": [datetime.now(timezone.utc)] * 2,
}
)
offline = store.get_historical_features(
entity_df=entity_df,
features=service,
).to_df()
online = store.get_online_features(
features=service,
entity_rows=[{"driver_id": 1001}, {"driver_id": 1002}],
).to_df()
print(offline[["driver_id", "conv_rate", "acc_rate"]])
print(online[["driver_id", "conv_rate", "acc_rate"]])The two printed tables should agree whenever the latest source row has been materialized. A difference tells you either that materialization is behind or that the TTL has expired a value on one path. Running this check on a sample of entities after each materialization job gives a cheap consistency test.
Measuring freshness
Feast does not alert you when the online store lags. You can measure it yourself. Two numbers are worth tracking:
- Materialization lag: the current time minus the end timestamp of the last successful materialization run.
- Value age: the current time minus the event timestamp of values actually served.
Log both from your scheduler or serving layer and alert when they approach the TTL. Alerting at about half the TTL leaves time to react before nulls appear in production.
Here is a materialization command that always stops at the current time. Run it from a scheduler more often than your TTL:
feast materialize-incremental $(date -u +"%Y-%m-%dT%H:%M:%S")If a run fails, rerun it. Incremental runs resume from the last successful end time, so a gap is filled by the next successful run rather than lost.
Choosing a TTL
- Estimate how long a value remains a faithful description of the entity. Use the update cadence of the source as a lower bound.
- Set the TTL to a few times that cadence, not to the longest value you can imagine.
- Confirm the materialization interval is well below the TTL.
- Compare null rates for the feature in training data and in served responses. They should be similar.
- Revisit the TTL when you change the source's update schedule.
Exercise
A feature view has ttl=timedelta(hours=6). The source writes a new row for each driver every 4 hours, and materialization runs every 8 hours. Describe what a serving request sees just before the next materialization run, assuming your Feast version returns null for values older than the TTL, and propose a fix.
Show answer
The online store holds values copied at the last run. Just before the next run, those values are up to 8 hours older than the run time, plus however old the source row was when copied. That exceeds the 6-hour TTL, so many requests return null for these features. Training data would not show this, because offline retrieval measures age against the entity timestamp, and the source rows are at most 4 hours old there. That gap is training-serving skew. Fix it by materializing more often than the TTL, for example every hour, or by raising the TTL to match the real tolerance of the model. Also monitor materialization lag so a failed run is noticed before values expire.