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:

  • It’s cheap and easy: You need no specialized machine learning engineering skills to use this in production; whoever built the training dataset (e.g., data scientists) can build the serving-layer dataset, while engineers deploying the model only need a single request to fetch multiple features at once. 
  • There will be no “logic-induced” training-serving skew if training and serving reuse the same feature computation logic from the data lake but there may still be skew due to delays or failed ETL runs.
  • Retrieval latency is very low because it’s a simple key lookup, returning a single row.

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:

  • They are the closest we can get to real online features when we need to calculate aggregations at inference time
  • They are only calculated when they are actually used (as opposed to pre-computed features which are materialized for the whole customer base, on the off chance they’re needed)

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:

SELECT

  customer_identifier,

  count(DISTINCT event_timestamp) AS app_opened__last_2h__count

FROM clickstream_table

WHERE type = ‘app_opened’

  AND customer_identifier = ‘{{customer_identifier}}’

  AND event_timestamp >= AGO(‘PT2H’)

GROUP BY customer_identifier

Some points on making this workable at our scale:

  • No joins: We don’t usually do any joins at serving time (only group-by aggregations on the same table). If we need to have more data in the table, we usually enrich it in the Flink layer instead (so that we need no costly joins at query time)
  • Aggressive TTL: Due to our scale, we apply a time to live (TTL) to events in Pinot, so that the size of the database doesn’t grow too much.
    • Therefore we only fetch very short-term data (e.g., last few days) from Pinot. If we need longer-term features (e.g., aggregations over the last year), we pre-compute them from batch data, as it’s easier, faster, and cheaper to retrieve them from a delayed snapshot computed daily.
  • Avoid schema changes: Schema changes are a problem. One solution is to have string fields with JSON content, as the JSON structure can evolve without changing the table schema (changes must still remain backward compatible for existing consumers).

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:

  • Feldera pipelines: We have recently been experimenting with a tool called Feldera. Feldera pipelines support incremental view maintenance, which means that aggregation-based features (sum, count, avg, etc.) are updated in real time as new events come in without recomputing the full aggregation from scratch each time).

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: Although most examples in this series involve tabular features, we have also been using sequential features in several use cases, as far back as 2023.

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.

  • Batch features are simple and cheap and shouldn’t be overlooked, even when that means the real-time model will be using slightly outdated information.
  • Deciding whether to pre-compute features or to calculate them on-demand is one of the key trade-offs when thinking about feature stores. Different use cases call for different approaches. A good feature store should support both.
  • Unifying the training and serving feature definition paths is hard and, though it doesn’t completely solve training-serving skew, it does mitigate it substantially.

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