Choosing a pattern
This guide helps you decide how a feature is computed for serving: read at request time, precomputed by a hook, or refreshed by a materialized view.
Before you start, have the feature written and running in batch, know the latency budget of the endpoint that calls it, and know where its data lives today. The four steps of serving are explained in Real-time serving .
1. Start at request time
Serve the feature as it is, from the stores that already hold its data, with a prepared statement . Port a batch feature shows the change of mappings. Then measure the statement against production-like sources ( Bounded requests ).
Stay at request time when the call fits the budget. Move to step 3 only when it does not: a heavy aggregate is a reason to measure, not a reason to precompute.
2. Pick the store a lookup reads
| The data is | Read it from | What a call costs |
|---|---|---|
| a cached profile, a flag, a precomputed value | Redis | one key read per key |
| rows in an operational database | a database source | one query per call, by key |
| only behind an API: a scoring service, an external model, a REST-only system | an HTTP source | one network round trip per request, plus the service's own time |
An HTTP source is the most flexible and the slowest of the three: it serializes each request and waits for another service. Prefer a database or Redis for data you can reach there.
History of a key
A feature over a key's recent history (a 30-day count, the last ten events) can be read at request time from several stores:
| Store | How the window is read | Fits when |
|---|---|---|
| PostgreSQL | one aggregation query per call, by key and time, in the database | the events are already there, and the per-key row counts are moderate |
PostgreSQL with aggregate_pushdown = FALSE | rows by key and time, aggregated in the engine | the database is the bottleneck |
| MySQL, MariaDB | one aggregation query per call by key, without a time window; with a window, rows by key aggregated in the engine | the events are already there |
| DynamoDB | rows by key, filtered and aggregated in the engine | the events are already there, and each key has few rows |
| Redis sorted set | the key's members as an array of rows (INDEXED ON), filtered and aggregated in the engine | latency matters most, and a writer maintains one sorted set per key, trimmed to the window |
- Keep aggregate pushdown on unless you measure otherwise. Turn it off when the database is the bottleneck and the engine has CPU to spare, or when a key has few rows and the aggregate is costly for the database to plan ( Where aggregations run ).
- A Redis history needs a writer. Either your own code adds each event with
ZADDand trims old members withZREMRANGEBYSCORE, or an event hook does both ( A rolling window ). The layout is in Histories in sorted sets .
Aggregating a key's recent history builds the PostgreSQL case.
3. When request time is too slow, pick a precompute pattern
| You need | Use | Built in |
|---|---|---|
| each event scored or enriched, and stored | an event hook | Scoring each event |
| a decision per payment, over the payment's recent history | an event hook writing the index, and a hook on its statement | A decision per payment |
| a count or sum over the last minutes, at request time, over the events indexed by then | an event hook writing the index; serving reads the window | A rolling window |
| totals per key and per minute or hour | the index, and a scheduled hook per bucket | Buckets per key |
| a 7-day or 30-day total at request time | hour buckets, plus the index for the edges | Long horizons |
| a 90-day or longer total at request time | day buckets, hour buckets for the edge days, the index for the hours not bucketed yet | Size a long horizon |
| a heavy per-key profile, refreshed after activity | a debounced scheduled hook | A heavy profile |
| a snapshot of every key, refreshed on a schedule, from sources featuremeshd reads | a materialized view | A materialized view |
Three rules separate the close cases:
- Window or buckets. A window read at request time costs a range read per request, in proportion to the events in it. For a few hundred events per key and window, it is the simplest choice. For a 7-day or 30-day window over a busy key, read precomputed buckets, and only the edges from the index.
- Hook or view. Every view run computes its whole keyset. For values refreshed per key after activity, use a scheduled hook.
- Hooks or your pipeline. A decision that must follow the card's order of events strictly, such as an authorization, or that must wait for late payments, belongs to the pipeline that owns that order. Call the statement from it ( Feed hooks from your own pipeline ).
4. For a profile refreshed after activity, pick the policy
A scheduled hook keyed by merchant computes a heavy profile once per burst ( A heavy profile ). Its POLICY decides which due time an entry keeps ( WITH ):
'latest'for keys whose bursts end. It computes less, once per burst.'earliest'when some keys never pause. Under'latest', a merchant that never stops paying is never profiled: each payment pushes its entry past the next drain.'earliest'keeps the due time of the first payment of a wave.
Only a payment refreshes a profile. A profile goes stale when its window moves with time alone (a 30-day window losing old payments), when another source it reads changes, or when its definition changes, until the merchant pays again. A merchant that stops paying keeps its last profile. Serving reads the stored profile with its request_time and datakey, so how old a profile may be, and from which definition, is the reader's rule.
Where every key must follow time or other sources, refresh them all on a schedule with a materialized view , or read the time-dependent part at request time.
Check the choice
- Request time: the prepared statement's latency under production-like load fits the budget.
- Precompute: what the pattern can miss is acceptable for the feature. Late, corrected and missing events lists it per pattern.
Common mistakes
- Precomputing before measuring. A prepared statement over an indexed table answers an aggregate in one query. Precompute adds a stream, stores to run and values that can lag.
- A view for per-key freshness. A view recomputes every key at each run. A merchant that paid a minute ago waits for the next run.
- A profile that must follow the clock. A debounced hook refreshes only on activity. Read the time-dependent part at request time.
Related
- Limits of hooks and views : what they do not do, with the workaround.
- Port a batch feature : serve a feature you already compute in batch.
- Train on what serving saw : the training data for whichever pattern you chose.
- Late, corrected and missing events : when a precomputed value is final, and how to send corrections.
- Keep money exact :
DECIMALamounts from the record to the sink. - Size
RETENTION,EXPIREand memory : what the chosen pattern needs from operations.