Skip to main content
Version: Beta ๐Ÿšง

Feature Services

A Feature Service is a Tecton object that groups together one or more Feature Views to create a reusable bundle of features. Feature Services are the unit of consumption for both real-time inference and offline training. By defining a Feature Service, you ensure that the same logic, transformations, and features are used consistently across your model lifecycle.

It is generally recommended that each model deployed in production have one associated Feature Service deployed, which serves features to the model.

A Feature Service provides:

  • An HTTP API to access feature values at the time of prediction
  • A one-line method call to rapidly construct training data for user-specified timestamps and labels
  • The ability to observe the endpoint where the data is served to monitor serving throughput, latency, and prediction success rate

Feature Services Workflowโ€‹

To use a Feature Service:

  1. Select one or more Feature Views to include.
  2. Create a FeatureService object and assign it a name.
  3. Apply the Feature Service to a workspace using the Tecton CLI.
  4. Use the Feature Service to fetch training data or serve features in real-time.
  5. Optionally create variants of a Feature Service to support A/B testing, staging, or incremental model development.

Minimal Exampleโ€‹

from tecton import FeatureService

fraud_detection = FeatureService(name="fraud_detection", features=[user_transaction_averages])

What's Nextโ€‹

Once you've created a Feature Service, here are some recommended next steps:

How to Use Feature Servicesโ€‹

Example: Defining a Feature Serviceโ€‹

The following example defines a Feature Service.

from tecton import FeatureService
from feature_repo.shared.features.ad_ground_truth_ctr_performance_7_days import (
ad_ground_truth_ctr_performance_7_days,
)
from feature_repo.shared.features.user_total_ad_frequency_counts import (
user_total_ad_frequency_counts,
)
from feature_repo.shared.features.user_ad_impression_counts import (
user_ad_impression_counts,
)

ctr_prediction_service = FeatureService(
name="ctr_prediction_service",
description="A Feature Service used for supporting a CTR prediction model.",
online_serving_enabled=True,
features=[
# add all of the features in a Feature View
user_total_ad_frequency_counts,
# add a single feature from a Feature View using double-bracket notation
user_ad_impression_counts[["count"]],
],
)
  • The Feature Service uses the user_total_ad_frequency_counts, and user_ad_impression_counts Feature Views.
  • The list of features in the Feature Service are defined in the features argument. When you pass a Feature View in this argument, the Feature Service will contain all the features in the Feature View. To select a subset of features in a Feature View, use double-bracket notation (e.g. FeatureView[['my_feature', 'other_feature']].)
ParameterDescription
nameUnique name for the Feature Service
featuresList of Feature Views or other Feature Services to include
descriptionOptional description of the Feature Service
ownerOptional owner metadata
tagsOptional tags for categorization or search
prevent_destroyOptional flag to prevent accidental deletion of critical Feature Services
on_demand_environmentRuntime environment for evaluating real-time transformations

Create a Feature Service with Multiple Featuresโ€‹

from tecton import FeatureService

fraud_detection_v2 = FeatureService(
name="fraud_detection_v2",
features=[transaction_amount_is_higher_than_average, user_transaction_amount_averages, user_credit_card_issuer],
)

Use the Feature Service for Trainingโ€‹

import pandas as pd

training_events = pd.read_parquet("training_events.parquet")
training_data = fraud_detection_v2.get_features_for_events(events=training_events).to_pandas()

Use the Feature Service for Online Inferenceโ€‹

features = fraud_detection_v2.get_online_features(join_keys={"user_id": "user_123"}, request_data={"amount": 750.00})

print(features.to_dict())

Update a Feature Serviceโ€‹

Tecton objects are immutable. To change the features in a Feature Service, create a new one (e.g., fraud_detection_v3) with the updated list of features.

Using the low-latency HTTP API Interfaceโ€‹

See the Reading Online Features Using the HTTP API guide.

Using the Offline Feature Retrieval SDK Interfaceโ€‹

Use the offline or batch interface for batch prediction jobs or to generate training datasets. To fetch a dataframe from a Feature Service with the Python SDK as a client, use the FeatureService.get_features_for_events() method.

To make a batch request, first create a context consisting of the join keys for prediction and the desired feature timestamps:

events = spark.read.parquet("dbfs:/sample_events.pq")
display(events)

Sample output (data not shown):

ad_iduser_uuidtimestampclicked
............
............

Then, using get_features_for_events(), generate the feature values:

import tecton

ws = tecton.get_workspace("prod")
feature_service = ws.get_feature_service("price_prediction_feature_service")
result_spark_df = feature_service.get_features_for_events(events).to_pandas()

Sample output (data not shown):

ad_iduser_uuidtimestampclickedad_ground_truth_ctr_performance_7_days__ad_total_clicks_7daysad_ground_truth_ctr_performance_7_days__ad_total_impressions_7days
..................
..................

Feature Namingโ€‹

Feature names in Tecton are determined based on the feature type. Some feature types require a name in the feature definition. For others (Aggregation for example), Tecton will apply a default name based on the feature definition, etc. unless a name is specified via the name parameter:

@batch_feature_view(
# ...
features=[
Aggregate(
input_column=Field("another_column", Int64),
function="mean",
time_window=TimeWindow(window_size=timedelta(days=1)),
name="1d_average",
description="my aggregate feature description",
tags={"tag": "value"},
),
],
)
def my_feature_view(my_agg):
return my_agg[...]

Using Feature Loggingโ€‹

Feature Services have the ability to continuously log online requests and feature vector responses as Tecton Datasets. These logged feature datasets can be used for auditing, analysis and training dataset generation.

Feature Logging Diagram

To enable feature logging on a FeatureService, simply add a LoggingConfig like in the example below and optionally specify a sample rate. You can also optionally set log_effective_times=True to log the feature timestamps from the Feature Store. As a reminder, Tecton will always serve the latest stored feature values as of the time of the request.

Run tecton apply to apply your changes.

from tecton import LoggingConfig

ctr_prediction_service = FeatureService(
name="ctr_prediction_service",
features=[ad_ground_truth_ctr_performance_7_days, user_total_ad_frequency_counts],
logging=LoggingConfig(
sample_rate=0.5,
log_effective_times=False,
),
)

This will create a new Tecton Dataset under the Datasets tab in the Web UI. This dataset will continue having new feature logs appended to it every 30 mins. If the features in the Feature Service change, a new dataset version will be created.

Logged Features

This dataset can be fetched in a notebook using the code snippet below.

info

AWS EMR users will need to follow instructions for installing Avro libraries notebooks to use Tecton Datasets since features are logged using Avro format.

import tecton

ws = tecton.get_workspace("prod")
dataset = ws.get_dataset("ctr_prediction_service.logged_requests.4")
display(dataset.to_pandas())

Sample output (data not shown):

ad_iduser_uuidtimestampclickedad_ground_truth_ctr_performance_7_days__ad_total_clicks_7daysad_ground_truth_ctr_performance_7_days__ad_total_impressions_7days
..................
..................

Was this page helpful?