How to Build a Feature Store for Machine Learning
Serve consistent features to training and inference by separating offline batch computation from an online low-latency store with point-in-time correctness.
On this page
Introduction
The most common cause of a model that performs well in notebooks and badly in production is training-serving skew: the features the model learned from are computed differently, or from different data, than the features it sees when it serves a request. A feature store is the infrastructure that prevents this by computing each feature once, storing it in a form that supports both historical training and low-latency online serving, and guaranteeing that the value used at training time for a given event is the value that was known at that moment. This article describes the offline and online halves of a feature store, point-in-time correctness, and the materialization step that keeps them aligned.
Why Feature Consistency Matters
Features are usually the same computation with two different call sites: a batch job that builds a training set and an online service that answers requests. If those two code paths diverge even slightly, the model sees distributions at inference that it never trained on. The divergence is often invisible: a different window length, a different time zone, a missing null fill, or a definition of active user that changed in one path but not the other. The model’s accuracy silently degrades and nobody can reproduce the failure in a notebook.
A feature store centralizes the definition so both paths read the same logic. The training path reads historical values; the serving path reads the latest value. When the definition changes, both change together, and the change is versioned so old models keep reading the values they were trained on.
Offline Store and Point-in-Time Joins
The offline store holds every historical version of each feature, keyed by entity and event time. Training needs point-in-time correctness: for each labeled event, join the feature values as they existed at that event’s timestamp, not the values that exist now. Without this, a training row can contain a feature computed after the label, which leaks the future into the model and inflates offline metrics that then fail in production.
-- Point-in-time join: for each label event, take the latest feature row
-- whose valid_from is at or before the event time.
SELECT l.entity_id,
l.event_time,
l.label,
f.amount_sum_7d
FROM labels l
LEFT JOIN LATERAL (
SELECT amount_sum_7d
FROM feature_user_daily f
WHERE f.entity_id = l.entity_id
AND f.valid_from <= l.event_time
ORDER BY f.valid_from DESC
LIMIT 1
) f ON TRUE;
Materializing to an Online Store
The online store holds only the latest value per entity, in a system that answers a lookup in single-digit milliseconds: Redis, DynamoDB, or a key-value table with an in-memory cache. Materialization is the job that copies freshly computed offline values into the online store. It must be idempotent, so a retry does not duplicate or regress values, and it must be versioned, so a bad materialization can be rolled back to a previous snapshot.
import json
def materialize(offline_rows, online_client, feature_version):
"""Copy the latest feature values into the online store, idempotently."""
written = 0
for row in offline_rows:
key = f"user:{row['entity_id']}"
payload = {
"features": {"amount_sum_7d": row["amount_sum_7d"]},
"valid_from": row["valid_from"],
"version": feature_version,
}
current = online_client.get(key)
if current is not None:
cur = json.loads(current)
if cur.get("valid_from", 0) >= payload["valid_from"]:
continue # never regress to an older snapshot
online_client.set(key, json.dumps(payload))
written += 1
return written
Serving Features at Inference Time
At request time the online service looks up each feature the model needs by entity key and assembles a feature vector. Two rules keep this correct. First, apply the same defaults and null handling the training path applied, or a missing feature becomes a different vector online. Second, record the feature values and their versions alongside the prediction, so a later investigation can replay why the model returned what it did. Without a log of the served vector, debugging a bad prediction is guesswork.
Latency budgets are tight, so batch the lookups: fetch all entities’ features in one round trip rather than one call per feature. If a feature is unavailable, decide explicitly whether to fall back to a default or to reject the request; silently serving a zero where training saw a real value is a skew bug that hides in aggregate metrics.
Avoiding Training-Serving Skew
The discipline that prevents skew is one definition, two readers. Define each feature as a transformation over source data in one place, then generate both the batch computation and the online lookup from that definition. Add a consistency test that recomputes the online value from the offline history for the same entity and timestamp and asserts they match within tolerance. This test catches the divergence the day it is introduced, not the day accuracy drops.
Log the feature vector at serving time and periodically join it back to the offline store to compare. Any systematic difference between served and historical values is skew, and it is worth an alert. Treat the online path as untrusted until this reconciliation passes.
Freshness, Backfill and Versioning
Features have a freshness requirement that comes from how they are used. A fraud score needs the last few minutes; a churn model tolerates daily. Set the materialization cadence to meet the tightest requirement among the models that consume the feature, and monitor the age of the online value so a stalled job is detected. A stale online value is often worse than a missing one, because it looks plausible.
When a feature definition changes, compute the new version for both the historical and online stores, and let models pin a version. Backfill the historical store so new models can train on the corrected definition, but never overwrite history that an existing model depends on. Versioning turns a definition change from an outage into a migration.
Conclusion
A feature store is the answer to training-serving skew: compute each feature once, keep every historical version in an offline store that supports point-in-time joins, materialize the latest values into a low-latency online store, and serve them with the same defaults the training path used. Test consistency between the two paths and log the served vector so predictions are explainable. Set the freshness cadence to the tightest consumer and version feature definitions so changing one does not break a model already in production.
Sources: Feast: open-source feature store, What is a feature store, Rules of Machine Learning.
FAQ
- What is point-in-time correctness and why does it matter?
- It means the feature value used for a training example is the value that was available at the example event timestamp, not the current value. Skipping it leaks future information into training and produces models that look good offline but fail in production.
- Do I need a dedicated feature-store product to avoid skew?
- Not necessarily. The essential pieces are a single definition per feature, an offline history keyed by event time, a latest-value online store, and a consistency test between the two paths. A product packages these; you can also build them on your existing warehouse and cache.
Report a problem with this articleOpens a pre-filled issue on GitHub.