The methods used to compute features fundamentally shape the characteristics of a feature store and its suitability for different ML applications. Understanding the core decision between computing features periodically in large batches versus generating them closer to when they are needed, either through continuous streaming or precisely at request time (on-demand), is essential. This choice involves significant trade-offs across latency, data freshness, computational cost, system complexity, and data consistency.
Batch Computation: Scalability and Simplicity
Batch computation involves processing large volumes of data at scheduled intervals, perhaps hourly or daily. This approach typically utilizes distributed processing frameworks like Apache Spark or Apache Flink (in batch mode) operating over data lakes (e.g., S3, ADLS, GCS with formats like Parquet) or data warehouses (e.g., BigQuery, Redshift, Snowflake).
Advantages:
- Cost Efficiency: Processing data in large chunks allows for optimized resource utilization and often results in a lower cost per computed feature value, especially for complex transformations involving significant historical data.
- High Throughput: Batch systems are designed to handle massive datasets efficiently, making them suitable for large-scale feature engineering tasks.
- Simplified Consistency for Training: Generating training datasets often requires point-in-time correct features. Batch processing naturally aligns with creating historical snapshots, simplifying the task of joining features accurately as of specific event timestamps.
- Mature Tooling: The ecosystem for batch processing is well-established, offering tools and familiar programming models (SQL, PySpark DataFrames).
Disadvantages:
- Inherent Latency: The most significant drawback is data freshness. Features are only as current as the last successful batch run. If a batch runs daily, the features could be up to 24 hours old, rendering them unsuitable for applications requiring near real-time information.
- Resource Scheduling: Batch jobs often cause peaks in resource demand, requiring careful capacity planning or reliance on elastic compute environments.
Batch computation is well-suited for features that change slowly, derive from extensive historical analysis, or where moderate staleness (hours or days) is acceptable. Examples include calculating lifetime customer value, segmenting users based on long-term behavior, or generating features for model training that require complex historical lookups.
Real-time Computation: Freshness and Responsiveness
Real-time computation encompasses two main patterns: streaming and on-demand.
- Streaming Computation: Features are computed continuously as new data events arrive. Stream processing frameworks like Apache Flink, Kafka Streams, or Spark Streaming consume data from message queues (e.g., Kafka, Pulsar) and update feature values incrementally, often storing the results in a low-latency online store.
- On-Demand Computation: Features are computed only when an inference request is made. This might involve simple lookups, transformations on data provided in the request itself, or retrieving very fresh data from operational systems.
Advantages:
- Maximum Freshness: Streaming provides features with latencies typically measured in seconds or milliseconds. On-demand computation offers zero staleness relative to the inference request context. This is essential for use cases like real-time fraud detection, algorithmic trading, or dynamic content personalization based on immediate user actions.
- Immediate Reactivity: Systems can respond instantly to the latest available information.
Disadvantages:
- Increased Complexity: Building, managing, and monitoring reliable real-time data pipelines is generally more complex than batch processing. Challenges include state management, fault tolerance, exactly-once semantics, and handling out-of-order events.
- Higher Potential Cost: Maintaining always-on streaming infrastructure can incur significant operational costs. On-demand computation can add latency to the important path of model serving if the computation is non-trivial.
- Consistency Challenges: Ensuring that features computed via streaming for serving are consistent with features used for training (often generated via batch or requiring complex point-in-time joins across streams) is a common difficulty, potentially leading to online/offline skew. We will address this in Chapter 3.
- Computational Limitations: Performing complex aggregations over long historical windows is often computationally infeasible or prohibitively expensive in a pure streaming or on-demand context.
Real-time computation is necessary when feature freshness is a primary application requirement. Streaming is ideal for features based on recent event sequences (e.g., number of clicks in the last 5 minutes), while on-demand is suited for features derived from request context (e.g., time of day) or simple, fast lookups.
Hybrid Approaches: Balancing the Trade-offs
In practice, many sophisticated ML systems employ a hybrid approach. Core, stable features might be computed via batch, while rapidly changing or event-driven features are handled by streaming pipelines. The online store then aggregates features from both sources for serving.
For example, a recommendation system might use:
- Batch: User embeddings and long-term preference profiles updated daily.
- Streaming: Features capturing user activity within the current session (items viewed, added to cart) updated in near real-time.
- On-Demand: Features extracted from the request context (e.g., device type, time of day).
While offering flexibility, hybrid systems require careful design to manage the integration points and ensure consistency between the different computation paths.
Making the Choice: Deciding Factors
Selecting the appropriate computation strategy depends heavily on the specific requirements of each feature:
| Factor |
Favors Batch |
Favors Streaming |
Favors On-Demand |
| Freshness Need |
Hours / Days |
Milliseconds / Seconds |
Instantaneous (relative to request) |
| Computation |
Complex, historical aggregations |
Incremental, windowed aggregations |
Simple lookups, request-based transforms |
| Data Source |
Data Lake / Warehouse |
Event Streams (Kafka, Pulsar) |
Request Payload, Operational DBs |
| Cost Profile |
Lower per-value cost at scale |
Constant operational cost |
Cost added per inference request |
| System Complexity |
Generally lower |
Higher (state, fault tolerance) |
Lower (computation) / Higher (latency) |
| Training Data Gen |
Simpler point-in-time correctness |
Requires careful time alignment |
Requires careful time alignment |
Comparison of Batch, Streaming, and On-Demand feature computation characteristics. The optimal choice depends on balancing these factors for specific feature requirements.
Understanding these trade-offs is fundamental to designing efficient and effective feature engineering pipelines. The choice impacts not only the feature store's architecture but also the downstream performance and accuracy of your ML models. As we move into discussions on data consistency (Chapter 3) and performance optimization (Chapter 4), the implications of these computation strategies will become even more apparent.