Sub topic 4
Storing feature history instead of only the latest value
This chapter assumes you know SQL joins and window functions, basic pandas, and the difference between a feature and a label. It also assumes you have read the chapter on event time, ingestion time, and prediction time. Here we look at how to store features so that a point-in-time join is possible at all.
Why the latest value is not enough
Many feature tables keep one row per entity. A nightly job recomputes avg_spend_30d for each customer and overwrites the old value. This is a fine layout for serving, because a model that scores a customer today wants today's value.
It is a poor layout for training. Suppose you want to build a training row for a customer whose label was observed on 2025-03-01. The overwritten table holds only the value computed last night. That value uses data from after March 2025. If you join it to the label, you attach information the model could not have had at prediction time. The leak is built into the storage layout, so no later join logic can repair it.
To rebuild what was known at any past moment, you need the past values. The rule is simple: write a new row each time a feature value is computed, and never overwrite or delete the old ones.
Latest-value table versus history table
The two layouts differ in one rule: update in place or append.
| Property | Latest-value table | Append-only history table |
|---|---|---|
| Rows per customer | One | One per computation |
| Write operation | Upsert (overwrite) | Insert only |
| Answers "what is the value now?" | Directly | With a lookup of the newest row |
| Answers "what was the value on date X?" | No | Yes |
| Storage growth | Bounded by entity count | Grows with time |
| Good for | Online serving | Training and audits |
Most feature platforms keep both. The history table is the source of truth, and a latest-value copy is derived from it for low-latency serving. Keeping the two in sync is the subject of the later chapter on training-serving consistency.
The shape of a history table
A usable history table has four kinds of columns:
- An entity key, such as
customer_id. This identifies who the feature describes. - One or more feature columns, such as
avg_spend_30d. - An event timestamp,
event_ts. This is the moment the feature value became true in the world, or the end of the window it describes. - An ingestion timestamp,
ingested_ts. This is when your system wrote the row.
Here is a small example, built in pandas so you can run it.
import pandas as pd
feature_history = pd.DataFrame(
{
"customer_id": [1, 1, 1, 2, 2],
"event_ts": pd.to_datetime(
["2025-01-31", "2025-02-28", "2025-03-31", "2025-01-31", "2025-03-31"]
),
"ingested_ts": pd.to_datetime(
["2025-02-01", "2025-03-01", "2025-04-01", "2025-02-01", "2025-04-02"]
),
"avg_spend_30d": [40.0, 55.0, 70.0, 12.0, 15.0],
}
)
print(feature_history)Customer 1 has three rows, one for each month end. Customer 2 has two rows, because no value was computed for the end of February. A latest-value table would keep only the last row for each customer: 70.0 and 15.0.
Now ask what was known about customer 1 on 2025-03-15. The answer is the row with event_ts of 2025-02-28 and a value of 55.0. The March 31 row did not exist yet. With history, the answer is a filter and a sort. Without it, the answer is not available.
as_of = pd.Timestamp("2025-03-15")
known = feature_history[
(feature_history["customer_id"] == 1)
& (feature_history["event_ts"] <= as_of)
& (feature_history["ingested_ts"] <= as_of)
]
print(known.sort_values("event_ts").tail(1))This prints the 2025-02-28 row. The later chapter on as-of joins turns this single lookup into a join over a whole label table.
Why two timestamps
A single timestamp is tempting, but it hides a real distinction. The event timestamp says when the value applies. The ingestion timestamp says when you could have used it.
In the example above, the row for customer 1 with event_ts of 2025-02-28 was ingested on 2025-03-01. At noon on 2025-02-28, the value was already true in the world but not yet in your system. A model scoring at that time could not have read it. Filtering on event_ts alone would treat the value as available a day early. For batch pipelines the gap is often small, but for data that arrives late or is corrected afterward, it can be days or weeks.
You should store both timestamps from the start. Adding ingested_ts later means guessing the values for old rows, and guesses defeat the purpose.
Append-only means never update
The history table must be append-only. Three habits keep it that way.
- Insert new rows, never update existing ones. If a value is wrong, insert a corrected row with a later
ingested_tsand keep the original. - Do not delete old rows to save space. Move them to cheaper storage, or partition by date, but keep them readable.
- Do not rewrite the table with a full backfill that replaces earlier values. If you must recompute history, write the result as new rows with a new ingestion time, or into a versioned table.
The reason for the second habit is that corrections change the answer to "what did we believe then?". A row corrected on April 10 should still show its old value when you ask about April 1. This is the idea behind bitemporal filtering, which a later chapter covers in full.
Creating the table
This is a minimal definition in generic SQL. Adjust types and partitioning for your warehouse.
CREATE TABLE feature_history (
customer_id BIGINT NOT NULL,
event_ts TIMESTAMP NOT NULL,
ingested_ts TIMESTAMP NOT NULL,
avg_spend_30d DOUBLE
);Partition or cluster by event_ts when your warehouse supports it, since nearly every training query filters on time. Cluster by customer_id as well if the table is large, because joins match on the entity key.
Do not declare a primary key on (customer_id, event_ts) alone. A correction produces a second row with the same pair and a later ingested_ts. If you want a uniqueness rule, include ingested_ts in it.
Choosing what to store
Each row is a snapshot of one entity at one moment. There are two common ways to produce snapshots.
- Periodic snapshots. A scheduled job writes a row for every entity at fixed intervals, such as daily. Rows are easy to reason about and to join, but storage grows with the number of entities times the number of runs.
- Change-only rows. A row is written only when the value changes. Storage is smaller, but a missing row now means "unchanged" and not "unknown". Your as-of join then needs a rule for how long an old value remains valid, which the next chapters on tolerance and TTL address.
Pick based on how often features change. A daily aggregate changes every day for active customers, so periodic snapshots are simple and cheap enough. A rarely edited attribute, such as a customer's plan type, suits change-only rows.
Common errors
- Keeping only the latest value, then joining it to historical labels. Offline metrics look excellent, and production results disappoint.
- Using the job's run time as
event_ts. A job that runs at 02:00 on March 2 to cover February should stamp the value with the end of its window, not with March 2, unless the value truly uses data through March 2. Stamp it wrongly and you either leak or lose usable rows. - Using
event_tsandingested_tsinterchangeably. Late data then appears available before it was. - Overwriting a row to fix an error. The fix silently changes every past training set that reads the table.
- Starting history collection only after the first model is built. The first training set then has too little past to use.
Checking your table
Before relying on a history table, run two quick checks. The first looks for rows ingested before their own event time, which usually signals a clock or stamping bug. The second counts rows per customer, to confirm that history exists.
bad_order = feature_history[
feature_history["ingested_ts"] < feature_history["event_ts"]
]
assert bad_order.empty, "Some rows were ingested before their event time"
rows_per_customer = feature_history.groupby("customer_id").size()
print(rows_per_customer)
assert (rows_per_customer > 1).any(), "No customer has more than one row: history is missing"With the example data, the first check passes and the second prints 3 rows for customer 1 and 2 rows for customer 2.
Exercise
A nightly job overwrites one row per customer in a table called customerfeatures. A teammate proposes building the training set by joining each historical label to that table on customerid. Why does this leak future information, and what two changes to the storage design make a correct join possible?
Show answer
The table holds only the value computed most recently, which uses data from after most historical prediction times, so the join attaches future information to old labels. The fix is to (1) append a new row on each computation instead of overwriting, so older values remain, and (2) store both eventts and ingestedts on each row, so a query can keep only rows that were both true and already available at the prediction time.
Exercise
Customer 2 in the example has rows with event_ts of 2025-01-31 and 2025-03-31 and no row for February. A model predicts for customer 2 on 2025-03-10. Which row is the newest one known, and what question about the data does that raise for the next chapters?
Show answer
The newest known row is the one with event_ts of 2025-01-31 (value 12.0), since the March 31 row is in the future. It is about 38 days old, which raises the question of how stale a value may be before it should be treated as missing. That limit is set with a TTL or join tolerance.