KatInterview

Build a Real-Time Search Autocomplete System

System DesignModel answer
MediumModel answerSystem Designautocompletelow-latencydistributed-systems

Last updated

Design an autocomplete service that delivers personalized query suggestions with sub-50ms latency, handling millions of concurrent users and live trend updates.

You're tasked with architecting a search autocomplete system that powers the query suggestion dropdown for a large-scale search platform. As users type each character, the system must return ranked suggestions instantly.

Core Requirements

Your design must support:

  • Suggestion delivery: Return the 10 most relevant completions for any input prefix
  • Live popularity updates: Incorporate recent search activity so trending topics surface within minutes
  • Consistent ordering: When multiple suggestions share identical scores, break ties alphabetically by normalized query text
  • Scale: Handle 100 million daily active users generating approximately 150,000 queries per second at peak
  • Performance: Achieve P99 response latency below 50 milliseconds measured at the server

Design Scope

Address the following components in your solution:

  1. Storage architecture: Select appropriate data structures and backing stores for the suggestion corpus. Justify your choice between trie-based indexing, key-value lookups, inverted indexes, or hybrid approaches.

  2. Partitioning: Explain how you'll distribute the dataset across machines to avoid coordination overhead during reads while keeping the system balanced.

  3. Multi-tier caching: Identify what should be cached at each layer (edge/CDN, application server, client) with appropriate invalidation policies.

  4. Ranking signal computation: Describe how to blend long-term popularity with short-term trending signals, and how to update scores as new search activity arrives without reprocessing the entire corpus.

  5. Quality and safety: Outline defenses against spam, coordinated manipulation, and harmful content appearing in suggestions.

Assumptions

  • Your corpus contains roughly 100 million servable queries after filtering
  • Prefixes are capped at 20 characters
  • Eventual consistency is acceptable; trending queries don't need to appear instantly everywhere
  • The system must remain available during datacenter or node failures

Hints

Hint 1

Precomputing top results per prefix at write time eliminates expensive ranking during reads, trading storage for latency.

Hint 2

Exponential decay for trending signals can be implemented efficiently by storing scores in log-space with timestamps, avoiding the need to rewrite every entry on each tick.

Hint 3

Short prefixes (1-3 characters) represent a tiny fraction of the keyspace but handle most traffic; replicate these aggressively across all serving layers.