Design a system like Gmail Smart Compose

    Smart Compose predicts the next words as you type an email, with a hard latency budget per keystroke: under 50 ms round trip, with the model itself under 20 ms. It's a sequence-prediction model serving problem dominated by latency optimization and caching.

    AI & Machine Learning, 5 components, about 30 min

    1. Scope

    In scope

    • • Inline next-phrase prediction
    • • Ultra-low latency serving
    • • Context (subject, thread)
    • • Caching common completions

    Out of scope

    • • Full email generation
    • • Grammar correction
    • • Training the model

    2. Functional requirements

    Must have

    • • Suggest the next words from a prefix
    • • Respond within ~50 ms round trip
    • • Use email context
    • • Accept/dismiss gracefully

    Nice to have

    • • Personalized style
    • • Multi-language
    • • Confidence threshold to suppress weak suggestions

    3. Non-functional requirements

    Latency< 50 ms round trip— model < 20 ms
    QPSVery high (per keystroke)
    QualityHigh precision (don't annoy)
    PrivacySensitive email content

    4. Back-of-envelope estimation

    Assumptions

    • • Fires on most keystrokes
    • • Quantized (INT8) model on TPU/GPU
    • • Beam search top candidates
    • • Cache hot prefixes

    Derived numbers

    Inference latency< 20 ms— Quantized + accelerator
    Cache hit rateHigh for common phrases
    Model sizeSmall/distilled for latency

    5. API design

    POST/api/complete

    { prefix, context } → suggested continuation

    6. High-level design

    What each box in the reference diagram is responsible for:

    Completion Service — Coordinates cache → context → model.
    Transformer (TPU) — Seq2seq model, quantized for low latency.
    Email Context — Subject/thread features (Spanner/Bigtable).
    Common Prefixes Cache — Redis cache of frequent completions.

    7. Data model

    Redis: prefix+context_hash → completion— Hot phrases

    8. Deep dives

    The latency budget

    Suggestions must appear faster than the user types the next character (under 50 ms round trip, ~20 ms of it for the model). This drives everything: a small/distilled model, INT8 quantization, accelerator inference (TPU/GPU), and short beam search. Anything slower and the suggestion arrives after the user has already typed past it.

    Caching common completions

    A huge fraction of email openings are formulaic ("Thanks for", "Let me know if", "Looking forward to"). Cache completions for common prefix+context signatures in Redis and serve them without invoking the model — slashing both latency and cost.

    Context features

    Quality depends on context: the subject line, prior messages in the thread, recipient, and time of day all condition the prediction. These features are fetched quickly (or precomputed per session) and fed to the model alongside the typed prefix.

    Precision over recall

    A wrong or awkward suggestion is worse than none — it distracts. Suppress low-confidence predictions below a threshold so the feature only fires when it's likely right, keeping user trust.

    9. Bottlenecks

    • Per-keystroke QPS → cache + small model
    • Tail latency → quantization, accelerators, request hedging
    • Context fetch → precompute per session

    10. Trade-offs

    Model size

    Choose: Small, distilled language model optimized for latency, quantized.

    Instead of: A large model: better suggestions, too slow per keystroke.

    A suggestion that arrives after the user typed past it has zero value.

    When to show a suggestion

    Choose: Only above a high confidence threshold, optimizing acceptance rate.

    Instead of: Show often: more acceptances in absolute terms, but annoying.

    Wrong suggestions cost attention; precision matters more than coverage.

    Personalization

    Choose: Lightweight personal signals (user's common phrases) on top of a shared model.

    Instead of: Per-user fine-tuned models: costly and privacy-sensitive.

    Most benefit comes from a good general model plus small personal context.

    11. Failure modes

    Model leaks memorized private text

    Privacy incident.

    Mitigation: Train with differential privacy or strong filtering of rare sequences; test for memorization with canaries.

    Biased or inappropriate completions

    Offensive suggestions.

    Mitigation: Filters on outputs and on sensitive contexts; suppress gendered pronoun predictions.

    Latency regression

    Suggestions stop appearing in time.

    Mitigation: Latency SLO with automatic fallback to no suggestion.

    12. What interviewers probe

    Answer each one yourself before opening it.

    Where does inference run, client or server?Show a strong answer

    Server for quality and model size; on-device when latency and privacy matter more and the model is small enough.

    How do you measure success?Show a strong answer

    Acceptance rate, characters saved per message, and latency.

    How do you avoid recomputing for every keystroke?Show a strong answer

    Cache the encoder state for the email context and decode incrementally from the current prefix.

    How do you collect training data ethically?Show a strong answer

    Aggregate and filter, avoid training on sensitive content, respect user settings, and apply privacy techniques.

    Sign in to track this guide in your curriculum progress.