Sub topic 15
Serving Features Online at Low Latency
Prerequisites
This chapter assumes you can create a feature table with a primary key. It also assumes you have logged a model with fe.log_model, so the model carries its feature lookups. If not, review those chapters first.
Why batch lookups are not enough
Batch scoring reads feature tables as Delta tables. That works when you score millions of rows on a schedule. A request-time use case is different. A checkout page that needs a churn score within tens of milliseconds can't scan a Delta table.
Online serving fixes this by copying feature values into a low-latency key-value store. A model serving endpoint reads from that store by primary key.
How online serving works
The offline feature table stays the source of truth. A synchronized copy is published to an online store. The model's logged feature metadata tells the endpoint which features to fetch and which key to use.
Steps to go online
- Keep the feature table's primary key stable. The online lookup uses it.
- Publish the feature table to an online store. The exact API and store type depend on your Databricks release, so follow the current documentation for your workspace.
- Deploy the model you logged with
fe.log_modelto a serving endpoint. Because the model was logged with itstraining_set, the endpoint knows which lookups to run. - Grant the endpoint's identity read access to the feature table and the online store.
Calling the endpoint
The client sends only the lookup key. The endpoint retrieves the other features itself, as score_batch does in batch mode.
import os
import requests
host = os.environ["DATABRICKS_HOST"].rstrip("/")
token = os.environ["DATABRICKS_TOKEN"]
response = requests.post(
f"{host}/serving-endpoints/churn-model/invocations",
headers={"Authorization": f"Bearer {token}"},
json={"dataframe_records": [{"user_id": 1042}]},
timeout=10,
)
response.raise_for_status()
print(response.json())Here churn-model is the name you gave the endpoint. Replace it with your own.
Freshness and consistency
The online store is only as fresh as its last sync. Total staleness is the pipeline schedule plus the sync delay. A feature computed over 90 days tolerates hours of delay. A "clicks in the last five minutes" feature does not.
Common errors
| Symptom | Likely cause |
|---|---|
| Endpoint returns null features | The key is missing from the online store, or the sync has not run yet. |
| Permission denied on lookup | The endpoint identity lacks access to the feature table or online store. |
| Key not found or type mismatch | The request sends `user_id` as a string, but the table key is an integer. |
| Predictions differ from batch | The online store is stale compared with the offline table. |
Exercise
A model scored well in batch, but the live endpoint returns different predictions for the same user. The feature pipeline runs hourly, and the online store syncs every 30 minutes. Name two possible causes and one check for each.
Show answer
First, staleness. The online values can lag the offline table by up to the pipeline interval plus the sync delay, so compare the online value with the current offline row for that user_id. Second, a missing or mismatched key. If the lookup fails, the features may be null or defaulted, so confirm the key exists in the online store and that the request's key type matches the table's primary key.