most read
Software Engineering
Why We Killed Our End-to-End Test Suite Sep 24
Product
Product Managers: what they do and why we need them Feb 15
Software Engineering
The value of canonicity Oct 30
Careers
We are building diverse teams with the most creative and innovative professionals for each position we open.



Written by: Felipe Almeida, with contributions from Luiz Felix, Vívian Miranda, Lucas Nolasco, and Caroline Custodio.
This is part 3 of a 3-part series on Feature Stores for Real-time ML. View part 1 and part 2.
Quick recap and introduction
In part 1 of this series we described what feature stores are and why they make sense once a data-driven organization reaches a certain level of maturity, especially when using ML-enabled real-time systems. In part 2, we listed lessons we learned from applying feature store concepts to real-world problems at Nubank.
In the next sections we will go over some concrete architectural patterns we have implemented. Some of them are what we could call a complete feature store (with common feature definitions for training and serving paths, for example) while others are focused on solving a particular problem only.
Check our job opportunies
Pre-computed features from batch data, materialized to a key-value serving layer
In this variant we produce a fully-denormalized dataset (with all the features for each entity combined into a single row) as part of our nightly ETL/ELT jobs and make it available in a “serving layer” (e.g. DynamoDB), keyed by the ID, to be used at inference time. This can be seen in Figure 1:
Figure 1: In this strategy, an extra step is added to the nightly ETL/ELT jobs: produce a dataset, keyed by entity ID(e.g. customer ID, account ID) where each column is a feature value for that entity. Then you push that snapshot to a low-latency key-value (KV) store such as DynamoDB or Redis. At serving time, the real-time ML model will retrieve asingle row from that store, since it’s fully denormalized. This is fast and easy.
This is usually the first feature store “pattern” many companies converge on, often without knowing it is a feature store. It’s based on the lambda architecture (minus the speed layer).
This is a simple use case but it’s quite powerful:
The major disadvantage is that the feature values served at inference time will be stale, as they are a snapshot from the last time the ETL/ELT jobs ran (not a representation of the world as it is now). So you need to account for when each snapshot would actually have become available when building the training dataset, to avoid data leakage (snapshots don’t become available instantaneously; they may take hours and sometimes fail).
As with all “shift-left” feature implementation strategies, this may become expensive because you pre-compute features for all entities (e.g., customers, accounts) even though only a minority thereof will need to be scored in a given day.
On-demand tabular features via Flink and Pinot
On-demand features are calculated at serving time, rather than pre-computed ahead of time. This is useful when models need the most up-to-date information and pre-computing features for entities that may never use them would be wasteful.
They can increase latency if we don’t take precautions, but there are clear advantages:
One of the strategies we use for on-demand features is a dual-write strategy whereby transactional microservices write events to both the offline (Parquet files in S3) and the online path (an Apache Flink app and then Pinot), as we can see in Figure 2 below.
Figure 2: Every behavioral event is routed to the offline path and to the online path. This helps reduce training-serving skew in this case, but the aggregation logic is still different in the offline path (usually Spark jobs) and the online path (SQL queries on Pinot).
In the write path, the dual-write strategy above is intended to send the same events to both the offline and online stores. This is important to reduce training-serving skew (but not eliminate it, because there may still be delays and missing events). Also, the batch aggregation logic (used in training) and the queries used at serving-time are different artifacts. For this reason, skew monitoring is still needed.
The read path consumes the data produced by the write path (Figure 2). It has two branches: one for training and one for serving.
The training-time logic is usually written as batch processing jobs, as it involves heavy joins and aggregations over long time horizons. The serving-time logic is a SQL query executed in real time on a real-time online analytical processing (OLAP) database such as Apache Pinot, as can be seen in Figure 3 below. This is part of our “Avalanche” stack, about which we wrote in this blog post.
Figure 3: At training time, we build the training dataset from a spine (a dataset with entity IDs and as-of timestamps) and the event store, via a Spark job. At serving time, we execute the aggregation in real time, with a regular SQL query, on a Pinot database backend.
The SQL query sent to Pinot is an aggregation query. Here’s one example of the query we’d use to retrieve the number of times a given customer has opened their app in the last 2 hours:
Some points on making this workable at our scale:
A secondary advantage of this approach is that any other team can use the events a team saves: the whole feature logic is contained in the query, so multiple teams can share the Pinot database and run different queries on it.
This is not a fully fledged feature store; we only use this setup to support the online feature-serving path. The training code is on another path altogether, and we need to keep monitoring training-serving skew because of that.
Using a two-stage Flink-based materialization and serving architecture
In this strategy we have behavioral events flow into two Flink apps, one after the other (Flink app #1 and Flink app #2, as can be seen in Figure 4 below). This is useful when models need up-to-date features, but calculating them at serving time would add too much latency:
Figure 4: In this example, every new “purchase-requested” event is streamed through Flink app #1 and a running count of the last 24 hours for every customer is kept in Flink app #2 and then upserted to DynamoDB. Whenever any real-time model needs this information, it only needs to call DynamoDB passing a customer’s ID and the feature-set name to retrieve the latest materialized count for that customer.
Flink app #1 listens to behavioral events (CDC logs from transactional databases and raw Kafka events), merges data from multiple shards into a single stream, filters out unwanted events and normalizes names and schemas. No state is kept in this first app.
Flink app #2 listens to events from the first app and keeps some state, holding all events for every customer, with a TTL of 24 hours. When a new event comes enters app #2, all related features are recalculated and a new row is upserted into a DynamoDB table (keyed by an entity ID such as a customer ID).
This strategy relies on pre-computation of features, so its serving-time latency is very low (since it’s a simple lookup). Similar to the first example in this article, the trade-off is that the majority of features will be calculated for customers that will never need them.
As in the previous example, this setup only covers the serving path, so it’s not a fully fledged feature store. The training code runs on Spark, so there may be logic-induced skew, which must be monitored.
Looking forward
In addition to constantly enhancing our setup and learning new lessons, there are at least another two avenues we are exploring currently:
Also, Feldera may allow us to unify feature logic for training and serving, reducing training-serving skew issues and bringing Nubank closer to a fully-fledged, companywide real-time feature store.
Sequential features unlock a lot of value, particularly in neural nets, but they also introduce extra complexity and new failure modes; figuring out how to integrate these into our feature platforms in real time is an ongoing challenge.
Conclusion
In this post we walked through some architectural patterns we use at Nubank to make features available to real-time ML models.
This wraps our 3-part series on feature stores for real-time ML at Nubank. Make sure to read parts 1 and 2 and stay tuned for other posts on this topic, since it’s something we are actively working on!
Check our job opportunies