# Feature Store

**Also known as:** Feature platform · Offline/online store  
**Category:** AI Patterns (Model in the loop)  
**Complexity:** 4/5  
**Guide:** https://layra4.dev/architectures/ai/ai-feature-store

*Compute a feature once, and give training and production the same number.*

A model learns from features: a user's orders in the last 30 days, a merchant's chargeback rate. At prediction time it needs the same features, computed the same way. Often they are written twice: a SQL query over the warehouse for training, and service code for live requests. The two drift apart, and the model meets inputs in production it never saw in training. That is training–serving skew, and nothing crashes when it happens.

A feature store fixes this with one definition per feature, written to two stores. The offline store keeps full history for building training sets. The online store keeps the latest value per key for millisecond reads at inference. The subtle part is the offline side: each training row must see a feature as it was when the label was recorded, never a later value.

> **In one sentence.** A feature store is a promise that training and serving read the same values. Its hardest job is answering what a value was at a past moment.

## Origin (2017)

Uber described a shared feature store as part of its Michelangelo ML platform in 2017. The open-source Feast project, started by Gojek with Google Cloud, followed in 2019 and made the offline/online split a common vocabulary.

## The parts

### Feature definitions as code

Each feature is declared in the repo: the entity it belongs to, its source, its transformation and window, its timestamp, its owner and how long a value stays valid. Pipelines and both stores are generated from that one definition, so there is no second copy to drift.

- Reviewed and versioned like any other code
- A changed definition is a new version, not an edit in place
- An owner and a description make the feature findable for reuse

> orders_30d: entity user_id, count of orders over 30 days, refreshed hourly, valid for 2 days.

### The offline store

The full history of feature values, each with its timestamp, kept in a warehouse or columnar files. It builds training sets and runs batch scoring. Throughput matters here, not latency.

- Append history; never overwrite a past value
- Backfills recompute a new feature over past data
- Batch scoring reads the same values training did

### The online store

The latest value of each feature per entity key, in a key-value store, with Redis, DynamoDB or Cassandra as common examples. Inference reads it in a few milliseconds, many features in one lookup.

- Every value carries a time-to-live, so stale data expires
- Missing values get the same default the model saw in training

### Point-in-time join (As-of join)

How a training set is built. For each label row, an entity and a timestamp, take the latest feature value known at or before that time. A plain join on the key takes today's value instead, and the model trains on information from the future.

- Join on when a value became available, not only when the event happened
- Respect the feature's time-to-live, as the online store does
- Let the store generate this join; hand-written versions are where leaks hide

### Materialization (Batch and streaming pipelines)

The jobs that compute features and write them to both stores. Batch jobs run hourly or daily over the warehouse. Streaming jobs update fast features, such as logins in the last five minutes, within seconds. Freshness is a target you set per feature.

- One computation feeds both stores, so they agree
- Monitor lag per feature, and alert when it breaks its target

## Design rules

### Define once, compute in one place

The same code must produce the offline history and the online values. A feature that can only be computed at request time, such as the distance from the current location, shares one function between training and serving.

### Join as of the moment the label was known

Never join training labels to current values. Use the time a value became available, not only its event time: data that landed two hours late was not known when the decision was made.

### Log the features you served

Record the exact values each prediction used. Comparing them with what the offline store would have produced is the only direct measure of skew, and the logs make a clean training set later.

### Give every feature a freshness budget

Set a time-to-live and a lag target per feature, and monitor both. An expired value should fall back to the training default, visibly, not be served as if it were fresh.

## Examples

### A point-in-time correct training set

```sql
-- Training set: one row per label, features as they were known at label time.
select
  l.user_id,
  l.label_time,
  l.churned            as label,
  f.orders_30d,
  f.avg_basket_30d
from labels l
left join lateral (
  select h.orders_30d, h.avg_basket_30d
  from user_features_history h
  where h.user_id = l.user_id
    and h.available_at <= l.label_time                    -- knowable then, not true later
    and h.available_at >  l.label_time - interval '2 days' -- the feature's time-to-live
  order by h.available_at desc
  limit 1
) f on true;   -- no row in the window: nulls, handled like a missing online value
```

One row per label, with each feature as it was known at that moment. Replace the lateral join with a plain join on user_id and offline accuracy rises, because the model can see the future.

## When to use

- Several models use the same features, and teams compute them separately
- Models need fresh per-user or per-item values at request time: fraud, ranking, recommendations
- Offline scores look great and production results do not

## When not to use

- One model scored nightly in batch: a warehouse table and a scheduled job are enough
- The model's context is documents or passages: that is retrieval, not features
- Every input comes with the request, so there is nothing to look up

## Benefits

- Training and serving read the same values by construction
- Point-in-time correct training sets without hand-written, leak-prone SQL
- Features can be found and reused across models and teams

## Costs

- Two stores and the pipelines between them, to run and monitor
- Backfills: a new or changed feature must be recomputed over history
- A shared platform needs an owner, and a shared feature cannot change freely

## Pitfalls

- Joining labels to current feature values, so offline accuracy is a mirage.
- Using event time where arrival time matters, so late data leaks.
- Editing a feature in place under a model trained on the old version.
- Online values with no time-to-live, served stale as if they were fresh.

## Related patterns

- [Batch Processing](https://layra4.dev/architectures/data-storage/batch-processing) — the jobs that build offline history and run backfills
- [Stream Processing](https://layra4.dev/architectures/data-storage/stream-processing) — how fast features stay fresh to within seconds
- [Distributed Cache](https://layra4.dev/architectures/data-storage/distributed-cache) — the online store is this kind of fast key-value read path
- [Retrieval-Augmented Generation](https://layra4.dev/architectures/ai/rag) — context for a language model: retrieved text, not computed features

## Further reading

- *Designing Machine Learning Systems* — Chip Huyen (2022) — Feature engineering, data leakage and training–serving skew, explained plainly.
- *Hidden Technical Debt in Machine Learning Systems* — Sculley et al. (2015) — Why the code around a model, features included, is where the debt piles up.
- *Rules of Machine Learning: Best Practices for ML Engineering* — Martin Zinkevich (Google) — Log the features you serve, and train like you serve.
