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 isRead it fromWhat 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:

StoreHow the window is readFits when
PostgreSQLone aggregation query per call, by key and time, in the databasethe events are already there, and the per-key row counts are moderate
PostgreSQL with aggregate_pushdown = FALSErows by key and time, aggregated in the enginethe database is the bottleneck
MySQL, MariaDBone aggregation query per call by key, without a time window; with a window, rows by key aggregated in the enginethe events are already there
DynamoDBrows by key, filtered and aggregated in the enginethe events are already there, and each key has few rows
Redis sorted setthe key's members as an array of rows (INDEXED ON), filtered and aggregated in the enginelatency 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 ZADD and trims old members with ZREMRANGEBYSCORE, 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 needUseBuilt in
each event scored or enriched, and storedan event hook Scoring each event
a decision per payment, over the payment's recent historyan 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 thenan event hook writing the index; serving reads the window A rolling window
totals per key and per minute or hourthe index, and a scheduled hook per bucket Buckets per key
a 7-day or 30-day total at request timehour buckets, plus the index for the edges Long horizons
a 90-day or longer total at request timeday 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 activitya debounced scheduled hook A heavy profile
a snapshot of every key, refreshed on a schedule, from sources featuremeshd readsa 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