Designing a Low-Latency Feature Platform for Ranking Models
System DesignModel answerLast updated
Design a platform that serves fresh features to a ranking model in milliseconds and also builds leak-free historical training sets, keeping the two paths consistent.
Your team runs ranking models for an ad or content-recommendation product. Models need many input signals (user history, item statistics, cross signals such as user × advertiser), and those signals come from both streaming events and daily batch jobs. You are asked to design a shared feature platform that the whole organization can use.
The platform must serve two very different consumers:
- Online inference: when a request arrives, the ranker fetches features for one user and many candidate items within a tight latency budget.
- Offline training: data scientists build historical training sets where every example sees only the feature values that would have been available at the moment of the original decision.
Walk through your design and cover:
- Abstractions and APIs: what concepts and calls does the platform expose to feature authors and model owners?
- Data flow: how are features ingested, computed, stored, and served?
- Consistency: how do you keep online and offline values aligned and avoid training/serving skew?
- Freshness and corrections: how do you handle fast-moving streaming features, late-arriving events, and historical backfills?
- Operations: how do you scale it, and how do you monitor data quality and reliability?
State your own assumptions about traffic, latency, entity counts, and freshness targets, and justify the main trade-offs you make.
Hints
Hint 1
Start by stating numbers (requests per second, candidates per request, latency budget, number of entities, event rate). They drive the choice of online store and the sizing.
Hint 2
The biggest risk is training/serving skew. Think about three layers of defense: one feature definition used by both paths, a single computation that writes to both stores, and logging the exact vectors that were served.
Hint 3
For point-in-time correctness, a join on event time alone is not enough. Ask when each value actually became available, and think about append-only history plus an as-of join.
Hint 4
For late events, separate what the online path can fix within a bounded lateness window from what only offline recomputation or label-side handling can fix.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
What a strong answer emphasizes
The main failure mode in production ML is training/serving skew. So treat the platform as a correctness system that must also be fast. Build the answer around three ideas:
- One feature definition, two materializations (online and offline).
- Event time everywhere, with a separate notion of when a value became available.
- Logging what was actually served.
1. Requirements and assumptions
- Ad ranking: about 500K requests/s, about 100 candidates each.
- Feature fetch budget: p99 under 10 ms out of roughly 50 ms end to end.
- Entities: about 1B users/devices, 10M ads, 1M publishers, plus cross keys.
- About 300 features per request. User vectors are 1-2 KB, so about 2 TB, roughly 6 TB with 3x replication.
- Freshness: real-time counters within seconds; batch features (30-day CTR, embeddings) daily.
- About 2M events/s. Conversions can arrive hours to days late.
- Serving must degrade gracefully (defaults, stale values) rather than fail a request. Versioning, lineage, and ownership are required.
2. Abstractions and APIs
- Entity: a join key (
user_id,ad_id,(user_id, advertiser_id)). - Source: batch table or stream, every record carrying
event_ts. - Feature view: a versioned declarative spec with entity, source, transformation (SQL/UDF), window, TTL, owner, and online/offline flags.
- Feature service: the pinned set of feature views a model consumes.
register_feature_view(spec) # validated in CI, versioned
get_online_features(service, keys) # batched multi-get -> values + ts
get_historical_features(service, entity_df) # entity_df = keys + event_ts
push(view, rows) # direct streaming writes
materialize(view, start, end) # batch -> online
backfill(view, start, end) # recompute history
The online call must be a batched multi-get because one request touches about 100 items.
3. Architecture
Kafka events -> Flink -> Online store (Aerospike/Redis/Bigtable)
\-> Offline store (Iceberg/Delta, append-only)
Lake/warehouse -> Spark -> Offline store, and materialize -> Online store
Registry: definitions, versions, schemas, lineage
Serving API (stateless, local cache) <- rankers
Served vectors -> Kafka -> "served features" table
- Online store: key is
(view, version, entity_id), value is packed features plusevent_ts. Shard by entity, replicate across zones. SSD-optimized KV is a good fit for TB scale with sub-ms reads. - Offline store: append-only, date-partitioned rows
(entity_id, event_ts, created_ts, values). Never overwrite, because immutability enables time travel and reproducible training sets.
4. Point-in-time correctness
For a training example (entity, label_ts), take the latest feature row satisfying:
event_ts <= label_ts AND created_ts <= label_ts
AND label_ts - event_ts <= TTL
The created_ts (availability time) condition is the subtle one. A daily feature for day D may land at 03:00 on D+1. Joining on event_ts alone would let training see it at 23:00 on D, which leaks the future.
At scale, use an as-of (sort-merge) join in Spark, bucket both sides by entity to limit shuffle, and prune partitions by the label time range.
5. Online/offline consistency
Three layers, weakest to strongest:
- Single definition compiled twice into a Flink job and a Spark job. Removes most skew, but engine differences (null handling, UDF semantics, window edges) remain.
- Compute once, write twice: the stream job writes each update to both the online store and the offline log, so history equals what serving saw. Batch is used for backfills.
- Feature logging: the serving layer logs the exact vector per
request_id; training joins labels to these logs. This is the gold standard and also catches serving-path bugs. Its limit is that brand-new features have no logs, so they still need point-in-time backfills.
Skew monitor: sample served vectors, recompute offline for the same key and timestamp, and alert on per-feature mismatch rate.
6. Streaming, late events, backfills
- Windowed aggregates: keyed state in Flink; build sliding windows from tumbling sub-buckets (a 1h window is 60 one-minute buckets) for cheap incremental updates. Alternatively store buckets online and finish aggregation at read time, trading read cost for exact freshness.
- Late events: event-time processing with watermarks and bounded allowed lateness (say 10 minutes), re-emitting updated aggregates. Anything later goes to a side output for offline correction. Multi-day-late conversions are handled on the label side (delayed-feedback windows, attribution jobs), not in online features.
- Truth: offline recomputation is authoritative. Online may be slightly under-counted, which is acceptable if training uses logged or as-of features reflecting the same lateness.
- Backfills: run the same definition in batch over history and write under a new version, never mutating old rows, so earlier training sets stay reproducible. When materializing to online, use conditional writes (only if
incoming.event_tsis newer) so a backfill cannot overwrite fresher streaming values.
7. Scale and reliability
- Shard and replicate the online store; use hot-key local caches for popular items; batch and pipeline lookups; co-locate serving with the store per region.
- Pack features per entity into one value to reduce round trips and per-key overhead.
- Fallbacks: per-feature defaults, serve stale values within TTL, hedged requests to cut tail latency, and a timeout budget so one slow view cannot sink the request.
- Isolate tenants with quotas and separate clusters for critical models.
8. Monitoring
- Freshness: lag between
event_tsand online write time per view. - Quality: null rate, range, cardinality, and distribution drift (PSI/KS) versus a training baseline.
- Skew: online vs. offline recomputation mismatch rate.
- Serving: p50/p99 latency, error rate, hit rate, default-fill rate.
- Pipeline: stream consumer lag, watermark delay, late-event volume, failed materializations.
- Alerts should be tied to feature owners via the registry, and breaking schema changes should be blocked in CI.
Trade-offs and common mistakes
- Answering "Redis plus a data lake" with no story for skew or point-in-time joins.
- Joining on event time only and leaking late-landing batch features.
- Mutating offline history during backfills, which breaks reproducibility.
- Letting backfills overwrite newer online values.
- Ignoring label delay for conversions.
- Chasing second-level freshness for features that do not benefit, which adds cost and complexity.
- No graceful degradation path when the store is slow.