Design a scalable ML model serving system

    An ML model-serving system answers prediction requests in real time with versioned models, low-latency feature lookups, and safe rollout (canary/A-B). The interview is about the serving infra around the model, not the model itself.

    AI & Machine Learning, 8 components, about 40 min

    1. Scope

    In scope

    • • Serve real-time predictions
    • • Model versioning + rollout
    • • Online feature retrieval
    • • Monitoring/drift

    Out of scope

    • • Model training (separate system)
    • • Feature engineering deep-dive
    • • AutoML

    2. Functional requirements

    Must have

    • • Low-latency predictions
    • • Deploy new model versions safely
    • • Fetch features for inference
    • • Roll back a bad model

    Nice to have

    • • A/B testing
    • • Shadow deployment
    • • Batch + online serving

    3. Non-functional requirements

    Latency< 100 ms p99
    Availability99.99%
    ThroughputAutoscaled to QPS
    FreshnessFeatures near-real-time

    4. Back-of-envelope estimation

    Assumptions

    • • 50K predictions/sec
    • • Feature vector ~1 KB
    • • Model artifacts 100s of MB
    • • GPU for heavy models

    Derived numbers

    Inference QPS~50K/s
    Feature store reads~50K/s— Redis sub-ms
    GPU podsAutoscaled by load

    5. API design

    POST/api/predict

    { features | entityId } → { prediction, modelVersion }

    POST/admin/deploy

    Roll out a model version with traffic weight

    6. High-level design

    What each box in the reference diagram is responsible for:

    Model Gateway — Routes by weight (canary/A-B); sticky if needed.
    Inference Service — Loads the model, runs prediction; CPU or GPU pods.
    Feature Store — Redis/Feast online features for sub-ms retrieval.
    Model Registry — Versioned artifacts (S3/MLflow); pulled on deploy.
    Feature DB — Source of truth synced into the online store.
    Drift Monitor — Tracks prediction distribution + latency (Prometheus).

    7. Data model

    Feature Store: entity_id → feature vector— Online, low-latency
    Registry: model_name:version → artifact + metadata

    8. Deep dives

    Online feature store

    Predictions need features (user history, counts) at request time. Computing them inline is too slow, so a feature store precomputes and serves them from a low-latency store (Redis/Feast). The same feature definitions are used offline (training) and online (serving) to avoid train/serve skew.

    Safe rollout: canary & shadow

    Never flip 100% of traffic to a new model. Canary: route a small % (e.g., 10%) to v2 and compare metrics before ramping. Shadow: send a copy of traffic to v2 without using its output, to validate latency/quality risk-free. The gateway does weight-based routing and instant rollback.

    Versioning & rollback

    A model registry stores immutable versioned artifacts. Inference pods pull a specific version; deploys are config changes (which version + what weight), making rollback a fast config revert rather than a rebuild.

    Monitoring drift

    Models degrade as the world shifts. Monitor input feature distributions and prediction distributions against training baselines; alert on drift. Also track latency/error SLOs per model version.

    9. Bottlenecks

    • GPU cost/contention → batching, autoscaling, right-sizing
    • Feature lookup latency → online store + caching
    • Bad model in prod → canary + instant rollback

    10. Trade-offs

    Online or batch predictions

    Choose: Online for requests that need fresh context; precompute in batch whenever the input set is known ahead of time.

    Instead of: Everything online: simplest mental model, most expensive and latency-sensitive.

    A nightly batch of predictions served from a key-value store costs a fraction of real-time GPU serving.

    CPU or GPU serving

    Choose: CPU for small tree and linear models; GPU with dynamic batching for deep models.

    Instead of: GPU for everything: idle expensive hardware for models that run in microseconds on CPU.

    GPU pays off only when batching keeps it busy.

    Rollout of a new model

    Choose: Shadow traffic first, then a canary percentage with automatic rollback on metric regression.

    Instead of: Swap all traffic at once: fastest, and the riskiest.

    Offline metrics do not guarantee online behaviour.

    11. Failure modes

    Training/serving skew

    Model accurate offline, poor online.

    Mitigation: One feature definition for both paths (a feature store) and logging served features for training.

    Feature store slow or down

    Requests time out.

    Mitigation: Per-feature defaults and a fallback model that needs fewer features.

    Silent drift

    Quality degrades for weeks unnoticed.

    Mitigation: Monitor input distributions (PSI) and prediction distributions; alert and trigger retraining.

    12. What interviewers probe

    Answer each one yourself before opening it.

    How does dynamic batching trade latency for throughput?Show a strong answer

    The server waits up to a few milliseconds to group requests into one GPU call; set max batch size and max wait so p99 stays inside the budget.

    How many GPUs for 50K predictions/s?Show a strong answer

    Benchmark one GPU at the target batch size (say 2,000/s at p99 under budget), divide: ~25 GPUs, plus 30–50% headroom and N+1 per zone.

    How do you version models and features together?Show a strong answer

    A model registry entry pins the feature set version; serving refuses to load a model whose features are not available.

    What do you log per prediction?Show a strong answer

    Request id, model version, input features (or a hash and sampled copies), output and latency, joined later with labels.

    Sign in to track this guide in your curriculum progress.