Sub topic 14
Keeping features fresh with scheduled pipelines
This chapter assumes you can create a feature table, write to it with write_table, and run Databricks notebooks and jobs. A feature table is only useful if its values match the current state of the raw data. A model that scores users with last month's order counts will quietly degrade. This chapter covers how to refresh features on a schedule and how to catch stale data.
The working model
Freshness is the age of the newest data in a feature table, compared with what the model needs. A churn model that uses 90-day order counts can tolerate a daily refresh. A fraud model that uses "transactions in the last 5 minutes" cannot.
A feature pipeline is a notebook or script that does three things in order:
- Reads raw tables.
- Recomputes the features.
- Merges the result into the feature table.
You run it as a Databricks job on a schedule, such as every night.
A refresh script
This script reuses the aggregation from earlier chapters and merges the result. It is complete enough to run as a job task.
from pyspark.sql import functions as F
from databricks.feature_engineering import FeatureEngineeringClient
fe = FeatureEngineeringClient()
orders = spark.table("main.sales.orders")
user_features_df = (
orders
.filter(F.col("order_ts") >= F.date_sub(F.current_date(), 90))
.groupBy("user_id")
.agg(
F.count("*").alias("order_count_90d"),
F.avg("amount").alias("avg_order_amount_90d"),
F.max("order_ts").alias("last_order_ts"),
)
)
fe.write_table(
name="main.ml.user_features",
df=user_features_df,
mode="merge",
)Merge updates rows whose user_id already exists and inserts new users. Because the job recomputes the whole 90-day window each run, rerunning it gives the same result. This property is called idempotence, and it makes retries after a failure safe.
Common errors
- Users who disappear. Merge never deletes rows. A user with no orders in the last 90 days is absent from
user_features_df, so the old row keeps its stale counts. Either include all users and fill zeros, or overwrite the table. - Schema drift. If you rename a column in the pipeline, the write fails or adds a new column, and models that look up the old name break. Change feature names through a new version, not in place.
- Silent failures. A job that fails overnight leaves yesterday's values in place, and nothing downstream complains. Turn on job failure notifications.
- Wrong cadence. Running hourly a feature that only changes daily wastes compute.
Checking freshness
The refresh script keeps last_order_ts, but that shows the newest order, not when the job ran. Add a timestamp column such as computed_at using F.current_timestamp(). Then a monitoring query or job task can fail when max(computed_at) is older than your limit.
from pyspark.sql import functions as F
latest = (
spark.table("main.ml.user_features")
.agg(F.max("computed_at").alias("latest"))
.first()["latest"]
)
print(latest)Exercise
A nightly job merges 90-day order counts into a feature table. A user placed their last order 100 days ago and still shows an order count of 2. Why, and how can you fix it?
Show answer
The user has no orders in the 90-day window, so they are missing from the computed DataFrame. Merge only updates or inserts rows that appear in the DataFrame, so the old row stays. Fix it by including all users in the computation and filling missing counts with 0, or by writing the table with overwrite mode.