We are going to design a streaming ML feature store with lambda-kappa duality.
The system should support:
Ingestion: real-time and batch
Backfill: ability to reprocess historical data
Consistency checks: between real-time and batch views
Cost budgets: monitor and control costs, especially for cloud services
We'll use the following concepts:
Lambda architecture: separate batch and speed layers, serving layer
Kappa architecture: unify batch and stream processing with a single codebase
Since we want to leverage the benefits of both, we'll design a hybrid (lambda-kappa) where:
We have a stream processing layer (for real-time) and a batch layer (for backfill and reprocessing).
However, we try to use the same processing logic for both (kappa approach) when possible.
Components:
Data Sources:
Real-time: Kafka, Kinesis, etc.
Batch: Files in S3, HDFS, etc.
Stream Processing Layer (Speed Layer):
Uses a stream processing framework (e.g., Flink, Spark Structured Streaming) to process events in real-time.
Outputs to a real-time serving store (e.g., Redis, Cassandra) for low-latency lookups.
Batch Processing Layer (Batch Layer):
Uses a batch processing framework (e.g., Spark, Flink batch) to process historical data.
Outputs to the same serving store (but with a different TTL or versioning) and also to a historical store (for training and backfill).
Serving Layer:
Two parts:
a. Real-time store: for online features (low latency, high throughput)
b. Historical store: for training and batch inference (e.g., data warehouse like BigQuery, Redshift, or HDFS)
Consistency Checks:
Compare the results of the batch and stream processing for the same time window to ensure they match (within tolerance).
Can be done by running the batch job on a recent window (e.g., last hour) and comparing with the stream's output for that window.
Backfill:
The batch layer can be used to backfill the historical store and also to correct the real-time store if needed (by reprocessing and then updating the real-time store for the past period? but note: real-time store is for current, so we might only update historical store for backfill and then use that for training. For online store, we might not change past data because it's already used?).
Alternatively, we can have a mechanism to correct the online store for a past period if we find an error, but that might be complex and not always necessary.
Cost Budgets:
Monitor the cost of each component (cloud resources, storage, network, etc.)
Set budgets and alerts (e.g., using cloud provider's budgeting tools)
Optimize: use auto-scaling, spot instances, choose appropriate storage classes, etc.
Implementation Plan:
Step 1: Define the feature processing logic (as a library) that can be used in both stream and batch.
Step 2: Set up the stream processing job (using Flink or Spark Streaming) that:
Reads from the real-time source (Kafka)
Applies the feature processing logic
Writes to the real-time serving store (Redis) and also to a distributed file system (for raw events and maybe for the batch layer to use?).
Step 3: Set up the batch processing job that:
Reads from the historical data (from the distributed file system or data lake)
Applies the same feature processing logic
Writes to the historical store (for training) and also to the real-time store (for the same time window? but note: real-time store is for current, so we might not write past data to real-time store?).
However, for consistency, we want the batch and stream to produce the same features for the same event. So we do:
The stream job processes events in real-time and writes to real-time store with event-time or processing-time? We need to think about time semantics.
Let's assume we use event-time for both.
For the batch job, we process a fixed window of events (by event-time) and write to the historical store. We can also write to the real-time store for the same event-time window? But the real-time store is for current, so if we backfill we might overwrite existing data in the real-time store? That might be acceptable if we are correcting errors.
Alternatively, we can have versioning in the real-time store so that we can have multiple versions of features for the same entity-time.
But typically, the real-time store is for the latest features. So for backfill, we only update the historical store and then retrain models. We don't update the real-time store for past events because they are no longer needed for real-time serving? However, if we have a correction, we might want to update the real-time store for the corrected period? But that period is in the past and no longer used for real-time? Actually, real-time store might be used for the last few days? So if we backfill the last day, we might want to update the real-time store for that day? But then we have to be cautious about the current state.
Let's define:
Real-time store: holds features for the last N days (e.g., 7 days) for online serving.
Historical store: holds all historical features for training.
So when we backfill, we are filling the historical store and also updating the real-time store for the backfilled period (if it falls within the last N days). We must update the real-time store for that period because the online serving might have been using wrong features for that period.
How to update? We can delete the old features for that entity-time and write the new ones. But note: the real-time store might have multiple features for the same entity? We need to design the key appropriately.
Let's assume the key in the real-time store is (entity_id, feature_name, event_time). But for online serving, we want the latest feature value for an entity up to now. So we might have a design where we store the latest value per entity and feature. Then if we backfill and find that the latest value for an entity at a past time was wrong, we might not be able to update because we only store the latest. So we need a different design.
Alternatively, we can store in the real-time store by (entity_id, feature_name) and the value is the latest. Then if we backfill and find an error in the past, we might have to recompute the entire history for that entity and then update the latest? But that's complex.
Given the complexity, maybe we don't update the real-time store for past events. Instead, we only update the historical store and then retrain the model. For online serving, we accept that the past features might be wrong? That might not be acceptable.
Another approach: in the real-time store, we store a time-series of features for each entity for the last N days. Then we can update a specific time point. But then when serving, we need to get the feature value at the model's training time? That's not real-time.
Let's clarify: in online serving, we want the feature value at the time of prediction. The prediction is happening now, so we want the latest feature value. But if we are making a prediction for a past event (like in batch inference), we use the historical store.
So for online serving, we only care about the latest. Therefore, if we backfill and find that the latest value for an entity (as of now) was computed incorrectly, then we should update the real-time store. But if the error is in the past (and the latest value is correct because it was updated by a later event), then we don't need to update the real-time store because the latest value is correct.
Example:
Event1 at time t1: feature = 1 (wrongly computed as 2)
Event2 at time t2: feature = 2 (correctly computed as 2)
code
The latest value at time t3 (now) should be 2. Even if we backfill and correct Event1, the latest value is still 2. So we don't need to update the real-time store.
But if Event2 was missing and then we backfill Event1, then the latest value becomes 1 (from Event1) and we should update the real-time store.
So we need to update the real-time store only when the backfilled data changes the latest value for an entity.
How to do that?
- We can compute the latest value for each entity in the backfill batch job and compare with the current latest in the real-time store.
- If different, update.
But note: the real-time store might have been updated by the stream processing for events after the backfill window. So we have to be careful.
Alternatively, we can design the real-time store to have a version number or timestamp of when the feature was computed. Then we can overwrite only if the backfilled computation is more recent? But the backfill is for a past window, so its event-time is in the past. We don't want to overwrite a more recent correct value.
So we only update the real-time store for an entity if the backfilled feature value is for an event-time that is more recent than the current one in the real-time store? But note: the real-time store might have multiple events for the same entity? We only store the latest by event-time?
Let's assume the real-time store stores the latest feature value by event-time. Then:
code
current_value_in_realtime = get(entity_id, feature_name) // this is the value for the latest event-time we have for that entity.
backfill_value = the value for that entity in the backfill batch for the latest event-time in the backfill window? But note: the backfill batch might cover a window [t_start, t_end]. The latest event-time in that window is t_end. But the real-time store might have an event after t_end (from the stream) so we don't want to overwrite.
So we only update if the backfilled latest event-time for that entity is greater than the current latest event-time in the real-time store? But wait, if the current latest event-time is from the stream and is after the backfill window, then we don't update. If the current latest event-time is within the backfill window and we have a corrected value, then we update.
How do we know the current latest event-time for that entity in the real-time store? We might store it as part of the value? Or we have a separate metadata?
Alternatively, we can change the real-time store to store a map of event-time to feature value? Then when serving, we take the maximum event-time that is <= prediction time? But that's more complex.
Given the complexity, maybe we do:
Real-time store: key = (entity_id, feature_name, event_time) -> value. But we don't store by event_time in the key? We store by entity_id and feature_name, and the value is a struct with (value, event_time). Then we can have multiple event_times? But then we need to query for the latest event_time <= given time.
That's more like a time-series store.
Alternatively, we can have:
Real-time store: for each (entity_id, feature_name) we store the latest event_time and value. Then when we backfill, we compute for each entity the latest event_time in the backfill window and its value. Then we compare with the stored event_time in the real-time store. If the backfilled event_time is more recent, we update. If the backfilled event_time is the same but the value is different, we update. If the backfilled event_time is older, we don't update.
But note: what if the backfill window covers multiple events for the same entity? We want the latest one in that window.
Steps for backfill update to real-time store:
code
For each entity in the backfill batch:
backfill_latest_event_time = max(event_time for that entity in the backfill batch)
backfill_value = the value for that max event_time
current = query real-time store for (entity_id, feature_name) -> (current_event_time, current_value)
if backfill_latest_event_time > current_event_time:
update real-time store with (backfill_value, backfill_latest_event_time)
else if backfill_latest_event_time == current_event_time and backfill_value != current_value:
update real-time store with (backfill_value, backfill_latest_event_time)
But note: the real-time store might have been updated by the stream for an event that is after the backfill window. So we are safe because we only update if the backfill event_time is more recent.
However, what if the stream has an event for the same entity with event_time within the backfill window but we haven't processed it yet? Then the current_event_time in the real-time store might be older, and the backfill will update with the backfilled value. Then when the stream event comes, it will overwrite. That's acceptable because the stream event is the same event? Actually, the stream event and the backfill event are the same event? Or different?
We are backfilling from the same source of truth (the data lake). So if the stream event and the backfill event are the same event (same event_time, same entity), then they should produce the same feature value. But if there was a bug in the stream processing, then the backfill might correct it. Then the stream event (when it comes) will overwrite with the same corrected value? Not necessarily, because the stream processing might have the same bug? We fixed the bug in the processing logic? Then the backfill and the stream (after the fix) will produce the same value.
But if the stream processing is not fixed, then we have a problem.
So we must ensure that the processing logic is the same and fixed.
Given the above, we might not need to update the real-time store from backfill if we are going to fix the stream processing and let the stream processing correct itself? But the stream processing might not see the past events again.
Therefore, we do need to update the real-time store from backfill for the affected entities.
But note: the stream processing might have already processed the event and written a wrong value. Then the backfill will correct it. Then the stream processing might process the same event again? We don't want duplicates. So we must design the stream processing to be idempotent and to handle out-of-order events? And we must have a way to know that an event has already been processed? We can use event_id and deduplicate.
Alternatively, we can design the real-time store to allow updates (overwrite) and the stream processing to write with the same event_time (and entity_id) and then the latest event_time wins.
So the plan:
Stream processing: for each event, compute feature and write to real-time store with key=(entity_id, feature_name) and value=(feature_value, event_time). The write is an upsert: if the event_time is greater than the current stored event_time, then update.
Batch processing (backfill): for each event in the backfill window, compute feature and write to the real-time store in the same way. But note: the batch job might process events in any order, but we are writing the latest for each entity. So we can do:
For each entity, we compute the latest event_time in the backfill window and the corresponding feature value, then write that to the real-time store (with the same upsert logic).
But wait: what if the backfill window has multiple events for the same entity and we want to update the real-time store with the latest one? We can do:
code
Step 1: For each entity in the backfill batch, find the event with the maximum event_time (and its feature value).
Step 2: For each entity, do the upsert as above.
Alternatively, we can write every event from the batch to the real-time store and let the upsert logic handle it? But that would be a lot of writes and might be inefficient. So better to compute the latest per entity and then write.
But note: the real-time store might have a value for that entity from the stream with an event_time that is within the backfill window but older than the backfill's latest? Then we want to update. So by writing the latest per entity from the backfill, we are updating to the latest in the backfill window.
However, what if the stream has already written an event with an event_time that is in the backfill window but is more recent than the backfill's latest? Then we don't want to overwrite. So we must compare.
So the batch job should:
code
For each entity in the backfill batch:
backfill_latest = max event_time in the backfill batch for that entity
backfill_value = value for backfill_latest
current = get from real-time store (entity_id, feature_name) -> (current_event_time, current_value)
if backfill_latest > current_event_time:
write (backfill_value, backfill_latest) to real-time store
else if backfill_latest == current_event_time and backfill_value != current_value:
write (backfill_value, backfill_latest) to real-time store
But note: the real-time store might have multiple features per entity? We are doing per feature? Actually, we are storing by (entity_id, feature_name). So we do this for each feature.
Alternatively, we can store in the real-time store by (entity_id) and then a map of feature_name to (value, event_time). But that might be more efficient for reading all features for an entity.
We'll leave the design of the real-time store as an implementation detail.
Consistency Checks:
We can run a consistency check job that:
code
How to implement the stream store? We can have the stream processing write to two sinks:
code
Sink1: real-time store (for online) with key=(entity_id, feature_name) and value=(value, event_time) and we upsert (only keep the latest event_time).
Sink2: stream store (for consistency) with key=(entity_id, event_time, feature_name) and value=value. We can set a TTL (e.g., 1 day) to avoid storing too much.
Then the consistency check job can read from the stream store for the last day and from the batch processed data for the same window.
But note: the batch processed data might be in the historical store (which is by event_time). So we can read from the historical store for the window and compare with the stream store.
Alternatively, we can run the batch processing on the raw data for the window and compare with the stream store. That way we are comparing the same input (the raw events) processed by two different jobs (batch and stream) but with the same logic? But we want to test the stream processing's output, so we should compare the stream store (which is the output of the stream processing) with the batch processing output for the same raw events.
So:
code
Consistency Check Job:
Input: time window [t_start, t_end]
Step 1: Read raw events for [t_start, t_end] from the data lake (or from the stream store's raw event storage?).
Step 2: Run the batch processing (using the same feature processing logic) on these raw events and produce a set of (entity_id, event_time, feature_name, value). This is the expected output.
Step 3: Read from the stream store for the same (entity_id, event_time, feature_name) in [t_start, t_end]. This is the actual output.
Step 4: Compare.
We can run this job daily for the previous day.
Cost Budgets:
code
We need to monitor the cost of:
- Stream processing cluster (e.g., Flink on EMR, or Kinesis Analytics)
- Batch processing cluster (e.g., Spark on EMR)
- Storage (real-time store, stream store, historical store, data lake)
- Network (data transfer between components)
We can set up cloud provider's budget alerts (e.g., AWS Budgets) and also monitor within our system by:
- Instrumenting each component to emit cost metrics (e.g., using CloudWatch, Prometheus).
- Define budgets per component (e.g., stream processing should not exceed $X per day).
- Set up alerts when a component is expected to exceed the budget (based on current usage trends).
We can also optimize:
- Use auto-scaling for stream and batch clusters.
- Use spot instances for batch.
- Use appropriate storage classes (e.g., S3 Intelligent Tiering).
- Compress data in the data lake.
We can also set up a dashboard to monitor costs.
Implementation Steps:
Set up the data sources (Kafka for stream, S3 for batch).
Implement the feature processing logic as a library (in Scala/Java for Spark/Flink, or Python if using other frameworks).
Stream processing job:
Source: Kafka
Process: apply feature logic
Sink1: real-time store (Redis) with upsert logic (key: entity_id:feature_name, value: {value, event_time})
Sink2: stream store (Cassandra or HBase) with key: (entity_id, event_time, feature_name) and TTL of 1 day.
Also, write raw events to the data lake (S3) for backfill and consistency.
Batch processing job:
Source: S3 (raw events) for a given time window.
Process: same feature logic.
Sink1: historical store (e.g., BigQuery, Redshift, or Parquet on S3) by (entity_id, event_time, feature_name).
Sink2: update real-time store for the latest events in the window (as described above).
Backfill:
To backfill a time window [t_start, t_end]:
code
Run the batch processing job on [t_start, t_end] and write to historical store.
Then, for the same window, update the real-time store by:
For each entity in the batch output for [t_start, t_end]:
Find the latest event_time for that entity in [t_start, t_end] and the corresponding feature value.
Compare with the current real-time store and update if the backfilled event_time is more recent or same but value different.
We can do this in the same batch job by reading the current real-time store and writing updates.
Consistency Check:
Run a job daily for the previous day:
code
Read raw events for day D from the data lake (or from the stream store's raw events?).
Run batch processing on day D and store in a temporary location (or in memory).
Read from stream store for day D.
Compare.
Cost Monitoring:
Set up cloud provider's budget and alerts.
Emit metrics from each component (e.g., number of records processed, compute time, storage used) and calculate cost.
Additional Considerations:
code
- Exactly-once processing: We need to ensure that the stream processing and batch processing are idempotent and can handle duplicates.
- Event-time vs processing-time: We use event-time for feature computation. The stream processing must handle late events (allowed lateness) and watermarks.
- State management: The stream processing might need to maintain state for windowed aggregations. We need to choose a state store (e.g., RocksDB) and handle state cleanup.
- Schema evolution: How to handle changes in feature definitions? We might need to version the feature logic.
- Metadata: We need a metadata catalog for features (descriptions, types, owners, etc.). We can use a separate database or a data catalog (like AWS Glue).
- Data quality: Beyond consistency, we might want to check for nulls, value ranges, etc.
This is a high-level design. The actual implementation will depend on the chosen technologies and specific requirements.
Let me know if you need more details on any specific part.
I'll design a Lambda-Kappa hybrid streaming ML feature store that combines the strengths of both architectures while addressing your requirements. This design uses Kappa's single codebase philosophy for consistency, while maintaining Lambda's separate path for backfill/batch workloads.
Would you like me to elaborate on any specific component or provide implementation details for a particular cloud provider (AWS/GCP/Azure)?
Takes a time window (e.g., last hour) and:
- Reads the events from the raw data lake for that window.
- Runs the batch processing (the same logic) on that window and writes to a temporary location (or in memory).
- Reads the real-time store for the same entities and time window (how? we have stored by event_time) and collects the features for the same events (by event_id? or by entity and event_time?).
Then compare the feature values for the same (entity_id, event_time, feature_name) between the two.
We can also compare the latest values per entity? But note: the real-time store might have been updated by events outside the window? We want to compare for events in the window.
So we need to be able to query the real-time store by event_time? If we store (entity_id, feature_name) -> (value, event_time), then we can query by entity_id and then filter by event_time? But that's inefficient.
Alternatively, we can store in the real-time store by (entity_id, event_time, feature_name) -> value? Then we can query by entity_id and event_time range.
Let's change the real-time store design:
Key: (entity_id, event_time, feature_name) -> value
Then for a given entity and event_time, we can get the feature value. But for online serving, we want the latest event_time for an entity and feature. So we have to query for the maximum event_time for that entity and feature. That might be inefficient.
Alternatively, we can have two stores:
- Real-time store (for online): key=(entity_id, feature_name) -> (value, event_time) [we can use a sorted set by event_time for each entity?]
- Historical store: by (entity_id, event_time, feature_name) -> value
Then for consistency check, we read from the historical store (which is the batch output) and from the real-time store by querying for each (entity_id, feature_name) and then filtering by event_time? But the real-time store only has the latest event_time per entity and feature. We might have lost the intermediate values? So we cannot compare for a specific event_time unless we store all event_times in the real-time store.
Therefore, for consistency checks, we need the real-time store to store all event_times? That would be too big.
Alternatively, we can have the stream processing write to a separate store that is structured like the historical store (by event_time) for the recent window (e.g., last 24 hours). Then we can compare that with the batch output for the same window.
Let's call it:
- Real-time store (for online): key=(entity_id, feature_name) -> (value, event_time) [only latest]
- Stream store (for consistency and debugging): key=(entity_id, event_time, feature_name) -> value, and we keep only for a short window (e.g., 1 day).
Then the consistency check job:
For a given time window [t_start, t_end]:
Read from the raw data lake for events in [t_start, t_end] (by event_time) and compute the batch features (using the batch processing logic) and store in a temporary map: (entity_id, event_time, feature_name) -> value.
Read from the stream store for the same (entity_id, event_time, feature_name) in [t_start, t_end].
Compare the two.
Then we can also compare the latest values in the real-time store (for the current moment) with the batch's latest for the same entities? But that's a different check.
We are concerned about the consistency of the computation, so we want to check that for the same event, the batch and stream processing produce the same feature.
So the stream store must store by event_time.