Build a Reply-Probability Scorer: Feature Pipelines and Inference
Machine LearningModel answerLast updated
Design an ML system that estimates whether a user will comment on a post in a community forum, emphasizing feature pipelines, training-serving consistency, and low-latency model deployment.
You are building the ML component of a large forum-style platform in which posts live inside communities. For a given (user, post) pair, the system must output the probability that the user will leave a comment on that post.
Walk through the full design: how you frame the problem, build training data, choose features and a model. Spend most of your time on two areas:
- Feature serving: how each input the model needs is computed, stored, and retrieved at request time.
- Model serving: how the model is deployed and scored while staying inside the latency budget of the calling request.
Before designing, state the assumptions you would confirm with the interviewer, for example:
- Who consumes the score: a personalized home feed, a within-community ranking, or a notification selector? This determines candidate count and latency budget.
- What exactly is a positive label: only top-level comments, or any reply in the thread? Within what window after the post was shown?
- What is the latency budget for the ranking stage and how many candidates are scored per request?
- What infrastructure already exists (event stream, feature store, serving platform)?
Your answer should address label definition, bias and imbalance, feature groups and freshness, offline/online consistency, serving architecture, safe rollout, fallbacks, and monitoring.
Hints
Hint 1
Start by pinning down the label: which event, which time window after an impression, and which impressions count as negatives. Everything downstream depends on this.
Hint 2
Group features by entity (user, post, community, user-community, context) and by how fresh they must be (batch, streaming, request-time). Each freshness tier suggests a different pipeline and storage choice.
Hint 3
For serving, think in terms of a latency budget: one batched feature fetch for all candidates, one batched model call, a timeout, and a fallback. Also ask how you will guarantee the model and its feature schema are deployed together.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
1. Framing and assumptions
I would assume the score feeds a feed-ranking stage that scores roughly 200-1000 candidates per request with a 50-100 ms budget for scoring (features plus inference). The task is binary classification: P(comment | user, post, context), used as one signal alongside others such as click and dwell time.
Label. Positive: the user posts a comment on the post within a fixed window (e.g. 24 hours) after an impression. Negative: an impression with no comment in that window. Decisions to state explicitly:
- Do replies to other comments count? I would start with any comment in the thread, and optionally train a separate head for top-level comments.
- Delay the label join until the window closes, and handle late events.
- Use only impressions that were actually seen (viewport logging), not merely served.
Data issues.
- Class imbalance: comment rates are often well under 1%. Downsample negatives for training efficiency, then correct the output with a calibration step (or the prior-shift formula), since downstream ranking needs calibrated probabilities.
- Position bias: items shown higher get more exposure. Log position, include it as a training-only feature (set to a constant at inference), or use inverse propensity weighting.
- Selection bias: we only observe outcomes for shown posts. A small exploration slice helps.
2. Features
Group by entity and freshness:
| Group | Examples | Freshness | Pipeline |
|---|---|---|---|
| User | historical comment rate, active communities, activity embedding | hours to days | daily batch |
| Post | age, text/media embedding, author stats, topic | static plus changing | batch plus streaming |
| Post dynamics | comments in last 5/60 min, votes velocity | seconds to minutes | streaming aggregation |
| Community | size, comment rate, moderation strictness | daily | batch |
| User x community / author | user's past comments in this community, follows | minutes to days | batch plus streaming |
| Context | time of day, device, surface | request time | computed inline |
Cross features (user x community) are the strongest signal for this task; keep them in a keyed store.
3. Model
Start with gradient-boosted trees as a baseline (fast, robust, handles missing values). Move to a neural model with user and post embeddings plus the above dense features if offline gains justify the cost. Optimize log loss and evaluate with AUC/PR-AUC and calibration error (ECE), sliced by community size, new vs. old users, and post age.
4. Feature serving
Stores. An offline store (data warehouse, partitioned by time) for training, and an online key-value store (in-memory or low-latency NoSQL) for serving. Streaming aggregates are updated by a stream processor consuming the event log and written to the online store with timestamps.
Training-serving consistency is the main risk:
- Define each feature once (a shared feature definition or transformation library) and use it for both batch backfill and online computation.
- Build training sets with point-in-time-correct joins: each example sees feature values as of its impression time, never later (avoids leakage, especially for post-dynamics features like comment counts).
- Even better, log the features actually served at inference and train on those logs; this removes skew by construction for features that are expensive to reconstruct.
- Monitor skew: compare distributions of served vs. training features (PSI, null rates, means) and alert on drift.
Request-time path. One batched multi-get for all candidates' post features, one for the user's features, and one for user-x-post/community cross features. Co-locate the store with serving, use local caching for hot posts and community features (short TTL), and return defaults plus a "missing" indicator when a key is absent. Keep p99 of the feature fetch to roughly 10-20 ms.
Freshness trade-off. Streaming features cost more (state, backfill complexity). Only stream what moves the metric: comment velocity on a post matters; a user's lifetime comment count does not.
5. Model serving
- Batched scoring: score all candidates in one call (one forward pass over a matrix) rather than per-item calls. Trees on CPU score thousands of rows in a few ms; neural models may need GPU or quantization.
- Budget example: feature fetch 15 ms, assembly 5 ms, inference 20 ms, network/overhead 10 ms, leaving headroom.
- Versioning: package the model with its feature schema (names, types, transforms, defaults). A deploy that mismatches schema and features is a classic outage; reject at load time.
- Rollout: shadow traffic (score but do not use), then canary at 1-5%, then A/B at larger share, with automatic rollback on error rate, latency, or calibration drift.
- Fallbacks: strict timeouts; on feature-store failure use cached or default features; on model failure fall back to the previous model or a simple heuristic (e.g. community-level comment rate). Degrade, never block the feed.
- Scaling: stateless replicas with autoscaling on QPS; precompute and cache user-level parts (user embedding) per session.
6. Monitoring and guardrails
- Feature freshness and null rates, prediction distribution, calibration by slice.
- Online metrics: comments per session, but also guardrails such as report/downvote rate, comment quality (length, deletion), and diversity, so the model does not reward inflammatory or low-quality threads and does not concentrate on already-hot posts.
- Retrain on a schedule (daily or weekly) given fast-changing content, and monitor for feedback loops.
Common mistakes
- Vague label (no window, no impression definition).
- Leaking future information (post comment counts after the impression) into training.
- Ignoring calibration after negative downsampling.
- Fetching features per candidate with sequential calls.
- Deploying a new model without versioning its feature schema.
- Optimizing comment count with no quality guardrails.