# Windowed Inference

> Externalizes time-dependent feature computation into stream processing

- **Category**: Machine Learning
- **Subcategory**: Serving and Operational Resilience
- **Canonical URL**: https://designpattern.fyi/patterns/windowed-inference/

---

## Description
**Intent**: Handle time-dependent features (rolling counts, sums, rates) correctly and consistently at serving time, especially in real-time contexts where recomputing windows over history is impractical.

**Context**: Some features are inherently time-dependent—average transaction amount over last 30 minutes, count of logins in past hour. Computing them correctly at serving time in real-time context is far harder than in training pipeline that can look back over historical data.

**Solution**: Externalize necessary state and window computation into stream-processing component that continuously maintains rolling aggregate as new events arrive. At prediction time, model simply reads current value of precomputed windowed feature rather than recomputing window over history per request.



## Use Cases
- Fraud detection with time-based features
- Anomaly detection with rolling aggregates
- Real-time personalization with recency features
- Any model using time-windowed features in real-time serving






## Trade-offs


### Advantages

- Consistent time-windowed features between training and serving

- Efficient real-time feature computation

- Handles real-time serving constraints

- Maintains rolling aggregates continuously




### Considerations & Drawbacks

- Requires real stream-processing infrastructure

- Careful engineering needed for training/serving consistency

- Added operational complexity

- Need to replicate windowing logic for historical training data







---
**Reference**: [Original Source](https://github.com/GoogleCloudPlatform/ml-design-patterns)

