Sub topic 12
Serving Features with get_online_features
Prerequisites and the serving path
This chapter assumes you know Python, pandas DataFrames, model inference, and the difference between batch and real-time data.
Feast uses an online store for low-latency feature retrieval during inference. The online store contains the most recently materialized value for each entity. It is not usually the place where you build a large training dataset.
The serving path has four steps:
- A feature view defines the features and their entity keys.
- A materialization job copies recent values from the offline source into the online store.
- An inference request supplies entity keys, such as a driver ID.
- Feast returns the requested feature values for those entities.
Materialize values before reading them
Defining a feature view does not automatically populate the online store. Run materialization after applying the repository definitions.
feast apply
feast materialize-incremental "$(date -u +"%Y-%m-%dT%H:%M:%S")"The timestamp tells Feast how far into the offline source it should materialize. In a production system, a scheduler normally runs this command or an equivalent job repeatedly.
If materialization has not run, a valid entity key may return no value. If materialization is delayed, the returned value can be stale.
Retrieve features for entity keys
The getonlinefeatures method accepts feature references and entity rows. A feature reference uses the form featureviewname:feature_name.
from feast import FeatureStore
store = FeatureStore(repo_path=".")
online_features = store.get_online_features(
features=[
"driver_hourly_stats:conv_rate",
"driver_hourly_stats:acc_rate",
],
entity_rows=[
{"driver_id": 1001},
{"driver_id": 1002},
],
).to_dict()
print(online_features)The entity key in each request must match the join key declared by the entity. In this example, driver_id identifies the driver entity. The result is a dictionary containing the entity key and the requested feature columns. Your model-serving code can convert those values into the input format expected by the model.
| Access method | Typical purpose | Data returned |
|---|---|---|
| get_online_features | Real-time inference | Latest materialized values |
| get_historical_features | Training and evaluation | Point-in-time-correct historical values |
Online retrieval does not perform a historical join for each request. It reads the current value stored for each entity. This makes it fast, but it also means that freshness depends on the materialization schedule and the source data.
Check the returned data
Before connecting the result to a model, verify that every requested entity has the expected fields. Handle missing values explicitly. A missing entity may indicate that the source has no record, materialization has not reached the record, or the request used the wrong join-key type.
Exercise
Why can getonlinefeatures return an old value even when the offline source contains a newer value?
Show answer
The newer value may not have been materialized into the online store yet. Online retrieval reads the latest value available in that store, not the offline source directly.