Sub topic 15
Serving features online at low latency
This chapter assumes you can already create a feature table, build a training set, and log a model with fe.log_model, as covered in earlier chapters. It also assumes you know what a REST endpoint is. Here you will see why batch scoring doesn't fit interactive use, how features reach an endpoint at low latency, and what can go wrong.
Why batch lookups are not enough
A Unity Catalog feature table is a Delta table. Delta is built for large scans and bulk writes, so a single-row read takes far too long for an interactive request. Fetching one user's features from a Delta table can take hundreds of milliseconds to seconds. A checkout page or fraud check usually has a budget of tens of milliseconds for the whole prediction.
Online serving solves this with a second copy of the feature data in a store built for point reads by primary key. The offline Delta table stays the source of truth. The online copy is derived from it.
| Property | Offline feature table (Delta) | Online store |
|---|---|---|
| Access pattern | Scans, joins, bulk reads | Single-key lookups |
| Typical latency | Seconds | Milliseconds |
| Used for | Training sets, batch scoring | Real-time inference |
| Source of truth | Yes | No, a synced copy |
| Freshness | As of the last write | As of the last sync |
The working model
Four pieces cooperate at request time.
- A feature table has a primary key and is refreshed by a scheduled pipeline.
- A sync process copies the table's rows into an online store, keyed by the same primary key.
- A model was logged with
fe.log_model, so it carries its feature lookup metadata. - A model serving endpoint hosts that model. A request supplies only the lookup keys. The endpoint reads the remaining features from the online store, assembles the input row, and runs the model.
This is the same contract as fe.score_batch, applied at request time. The model knows which features it needs and where they live, so the caller never rebuilds them.
Prerequisites for a feature table to be served
- The table needs a primary key. The online store indexes rows by that key, so the key defines what a lookup can ask for.
- The table must be in Unity Catalog and be a Delta table.
- The model must be logged with
fe.log_modeland atraining_set. A model logged with plainmlflow.sklearn.log_modelhas no lookup metadata. Its endpoint would require the caller to send every feature. - The sync needs Change Data Feed on the source table in many configurations. Check the current documentation for the option you use.
Time series feature tables
Point-in-time lookups matter for training, where each label needs the feature values as of its own timestamp. At serving time you want the latest values, because "now" is the only moment that matters. Online stores generally hold one current row per primary key. If your offline table uses a timestamp key, check how the sync option handles it. It typically publishes only the latest row per non-timestamp key. A mismatch here produces a model that is correct offline and wrong online.
Deploying a model that looks up features
The model from the earlier chapters is logged to Unity Catalog as main.ml.churn_model. This code creates an endpoint for version 1 and queries it with only a key.
from databricks.sdk import WorkspaceClient
from databricks.sdk.service.serving import EndpointCoreConfigInput, ServedEntityInput
w = WorkspaceClient()
w.serving_endpoints.create_and_wait(
name="churn-endpoint",
config=EndpointCoreConfigInput(
served_entities=[
ServedEntityInput(
entity_name="main.ml.churn_model",
entity_version="1",
workload_size="Small",
scale_to_zero_enabled=True,
)
]
),
)
response = w.serving_endpoints.query(
name="churn-endpoint",
dataframe_records=[{"user_id": 42}],
)
print(response.predictions)The request contains user_id and nothing else. The columns order_count_90d and avg_order_amount_90d come from the online store.
Freshness and the cost of a sync
The online store is only as fresh as the last sync, and the sync only sees what the offline table contains. Staleness therefore adds up across stages:
- The time between source data changing and your pipeline running.
- The pipeline's own run time.
- The delay between the Delta write and the online copy updating.
If your pipeline runs hourly, a feature can be an hour old plus the sync delay. Decide what freshness the model needs before choosing a design. A churn model scoring from 90-day order aggregates tolerates daily refreshes. A fraud model using "transactions in the last five minutes" does not. Such features may need streaming computation or request-time calculation instead of a synced table.
Sync can run in a snapshot mode, a triggered mode, or a continuous mode. Continuous is fresher but costs more because it keeps compute running. Triggered after each pipeline run is a common middle path.
Common errors
- Missing keys. A user absent from the online store gets null features, or the request may fail depending on configuration. Models that were trained with non-null features can behave badly on nulls. Decide a policy for new users and apply the same defaults in training.
- Wrong key type. A
user_idsent as the string"42"when the table key isBIGINTcan fail a lookup. Send the same type the table uses. - Model without lookup metadata. The endpoint then expects all feature columns and returns a schema error when you send only the key.
- Permissions. The endpoint's identity needs access to the feature table and the online store. A lookup that works in your notebook can fail in the endpoint because it runs as a different principal.
- Cold starts. With
scale_to_zero_enabled=True, the first request after idle time is slow. Disable scale to zero for latency-sensitive paths. - Version drift. Changing a feature's computation in the pipeline changes what the already deployed model receives. Treat feature definition changes as model changes and retrain or add a new feature column.
When to skip online serving
Online serving adds a second copy of the data, a sync to monitor, and an always-on endpoint. If predictions are consumed in reports or nightly jobs, fe.score_batch is simpler and cheaper. Choose online serving when a user or system waits on the prediction, and when features can be precomputed per key. Features that depend on the request itself, such as the current cart contents, can't be precomputed. Send them in the request alongside the key.
Exercise
Your churn model is served through an endpoint and returns a schema error saying that columns ordercount90d and avgorderamount90d are missing, although your request contains userid. Name the most likely cause and the fix.
Show answer
The model was probably logged with plain mlflow.sklearn.logmodel instead of fe.logmodel with a trainingset, so it has no feature lookup metadata and the endpoint expects every feature in the request. Re-log the model with fe.logmodel and the training set, register the new version, and update the endpoint to serve that version.
Exercise
A feature table is refreshed by a pipeline every 6 hours, and the online sync runs after each refresh and takes about 20 minutes. What is the worst-case age of a feature value served at request time, and what does that imply for a model that needs features under 15 minutes old?
Show answer
The worst case is about 6 hours 20 minutes, which is the refresh interval plus the sync time. A model that needs features under 15 minutes old cannot use this design. It needs a faster pipeline and sync, streaming feature computation, or features calculated at request time.