Sub topic 10
Prevention: Feature Stores and a Single Feature Definition
This chapter assumes basic Python and pandas, a supervised machine-learning workflow, and a general understanding of batch jobs and model APIs. You should also know what a feature is: a model input such as orders_7d or customer_age.
A feature store is a system that defines, computes, stores, and serves features for both training and prediction. It usually has two parts:
- An offline store keeps historical feature values for building training sets.
- An online store keeps recent values for low-latency production requests.
The goal is not simply to store columns. The goal is to give a feature one definition, one meaning, and consistent time behavior in every path.
Why one definition matters
Suppose a churn model uses orders_7d, the number of orders a customer made in the previous seven days. The batch training job might count completed orders. The API handler might count all orders, including canceled ones. Both paths produce a column called orders_7d, but the model sees different measurements.
A feature definition should specify:
- The source data.
- The entity, such as
customer_id. - The calculation.
- The time window.
- The update schedule.
- Missing-value behavior.
- The historical behavior needed for training.
| Feature | Entity | Definition | Online use |
|---|---|---|---|
| `orders_7d` | `customer_id` | Completed orders in the previous 7 days | Read the latest materialized value |
How a feature store prevents skew
A feature store centralizes the transformation and makes it available to both consumers:
- A feature author defines
orders_7donce. - A historical job computes values for past timestamps and writes them to the offline store.
- A materialization job computes current values and writes them to the online store.
- Training reads historical values using the label timestamp.
- Serving reads the current value for the same customer.
The offline and online stores may use different technologies. That is acceptable when they follow the same registered definition and tests compare their results.
A feature store does not automatically prevent leakage. Historical retrieval must still be point-in-time correct. For a label created on January 10, the training value must not include an order placed on January 11.
A small feature definition
The following example uses a shared definition object. A real feature-store platform would add storage, scheduling, metadata, and access controls.
from dataclasses import dataclass
from datetime import datetime, timedelta
@dataclass(frozen=True)
class FeatureDefinition:
name: str
window: timedelta
allowed_status: str = "completed"
def compute(self, events, customer_id, as_of):
start = as_of - self.window
return sum(
event["customer_id"] == customer_id
and start < event["time"] <= as_of
and event["status"] == self.allowed_status
for event in events
)
orders_7d = FeatureDefinition("orders_7d", timedelta(days=7))
events = [
{"customer_id": 7, "time": datetime(2026, 1, 5), "status": "completed"},
{"customer_id": 7, "time": datetime(2026, 1, 9), "status": "canceled"},
]
as_of = datetime(2026, 1, 10)
print(orders_7d.compute(events, 7, as_of)) # 1The important property is that training code and serving code call the same definition. They should not reimplement its filter or time window.
Choosing when to use a feature store
A feature store is useful when many models share features, online predictions need low latency, or historical and current features must stay aligned. It may be unnecessary for a small batch-only project. In that case, a bundled preprocessing pipeline and parity tests can provide the same basic protection with less infrastructure.
Exercise
A feature is correct in the offline store but differs in the online store. What should you check first?
Show answer
Check whether both stores use the same versioned definition, source data, time window, status filters, and missing-value rules. Then compare both results for the same entity and timestamp.