KatInterview

Design a Prefix Autocomplete Service with Live Trend Signals

System DesignModel answer
HardModel answerSystem Designautocompletedistributed-systemscaching

Last updated

Design a distributed autocomplete backend that returns the top 10 popularity-ranked query completions for any typed prefix in under 30ms at global scale.

Context

A large social content platform lets users search for topics, ideas, and creators. As a user types in the search bar, the product fires a request on nearly every keystroke and renders a dropdown of up to 10 suggested completions. The team wants you to design the backend service powering those suggestions.

The system must feel instant, stay accurate during viral moments (a trending topic should surface within a few minutes), and never become a single point of failure for the broader search experience.

Functional Requirements

  • Given a typed prefix string, return the top 10 query completions ranked by a popularity score.
  • Scoring must reflect both long-term historical popularity and short-term trending activity (spikes visible within 2–5 minutes).
  • The system must be able to suppress or filter completions that violate content policy (banned, adult, or abusive terms).

Non-Functional Requirements

  • Latency: p99 end-to-end read latency under 30ms globally.
  • Availability: 99.99% uptime; autocomplete degrading must not break core search.
  • Scale: 1 billion registered users, ~500 million daily active searchers.
  • Freshness: A newly viral query should appear in completions within 2–5 minutes.

Traffic and Scale Estimates

  • Daily active users: 500M, averaging 4 searches per day.
  • Autocomplete QPS: Each search generates roughly 5 autocomplete requests after client-side debouncing (~15 keystrokes, grouped). That's ~10 billion autocomplete requests per day — about 115,000 QPS on average and 300,000 QPS at peak.
  • Vocabulary size: ~100 million unique query strings tracked globally.

What to Cover

Your answer should address all four of the following areas:

  1. Data model and serving engine — Compare serving from a live trie versus a precomputed prefix-to-top-list key-value store. Reason through memory footprint, read/write trade-offs, and why one is more practical at this scale.

  2. Freshness pipeline — Describe how you would blend a stable batch-computed baseline (e.g., a daily job) with a real-time streaming signal (e.g., a 5-minute sliding window) to produce a single score per query. Include the data flow from raw search events to updated completions.

  3. Sharding and hot-prefix handling — Single-letter and two-letter prefixes ("h", "ho", "s") receive a wildly disproportionate share of traffic. How do you prevent those keys from overloading individual nodes?

  4. Replication and update propagation — How do you push batch snapshots and streaming delta updates to all replicas without causing latency spikes or serving stale or inconsistent lists?

Include rough numbers where they help justify design choices.

Hints

Hint 1

Start with a back-of-envelope calculation: how large is the full prefix keyspace, how many bytes per entry, and does the whole table fit in RAM? That number drives the rest of the architecture.

Hint 2

Serving a trie dynamically on every read sounds appealing but falls apart for short prefixes — think about what the subtree under 'd' looks like. A precomputed flat key-value table swaps write amplification (one query update touches many prefix keys) for predictable O(1) reads. At 5K writes/s that trade is very favorable.

Hint 3

Short prefixes (length ≤ 3) are maybe 1% of the key space but carry 40–50% of traffic. Treating them as a special tier — fully replicated into every node's local memory plus cached at the CDN edge — removes most of the hot-key problem before it reaches your origin shards.