ML System Design

Design: Query Autocomplete

Suggesting search completions as users type, predicting their intended full query from partial keystrokes.

🔴 advanced6 min readsystem-designretrieval
Designing a Query Autocomplete System predicts and suggests complete search queries as a user types each character. The system must return suggestions within 100ms of each keystroke, handle typos and partial prefixes, personalize suggestions based on user history, and rank candidates by expected relevance.

The Problem

Design a system that suggests 5 to 10 search query completions as a user types. Suggestions must appear within 100ms of each keystroke. The system handles 50,000 queries per second and must work across web and mobile clients.

High-Level Architecture

  User types: "how to m"
              │
              ▼
  ┌───────────────────────────────┐
  │ STEP 1: PREFIX MATCHING       │  (~2ms)
  │ Trie lookup or prefix index   │
  │ Output: ~100 candidate queries│
  └───────────────────────────────┘
              │
              ▼
  ┌───────────────────────────────┐
  │ STEP 2: RANKING               │  (~5ms)
  │ Score by popularity + recency │
  │ + personalization              │
  │ Output: Top 10 suggestions    │
  └───────────────────────────────┘
              │
              ▼
  ┌───────────────────────────────┐
  │ STEP 3: FILTERING             │  (~1ms)
  │ Remove offensive / harmful    │
  │ Apply diversity constraints   │
  │ Output: Final 5-8 suggestions │
  └───────────────────────────────┘

Step 1: Prefix Matching

Store a corpus of historical search queries in a data structure optimized for prefix lookup:

Option A: Trie (Prefix Tree)

Option B: Sorted Index with Binary Search

Option C: Elasticsearch Completion Suggester

At scale, the query corpus might contain billions of unique queries. Keep only the top 10 million most frequent queries in the active index and refresh daily.

Step 2: Ranking

Once you have 100 candidate completions matching the prefix, rank them:

  1. Global Popularity: How many times has this query been searched by all users in the last 30 days? Popular queries rank higher.
  2. Recency / Trending: Queries trending in the last hour get a boost. This surfaces current events ("election results", "world cup score").
  3. Personalization: If the user recently searched for "how to make pasta", suggest "how to make pasta sauce" higher than "how to make paper airplanes".
  4. Query Quality: Longer, more specific queries are often more useful than very short generic ones.

A simple scoring formula:

$$\text{Score} = w_1 \cdot \log(\text{Frequency}) + w_2 \cdot \text{RecencyBoost} + w_3 \cdot \text{PersonalBoost}$$

Step 3: Filtering

Before returning suggestions to the user:

Data Pipeline

Continuously update the suggestion corpus:

  1. Log all completed searches (queries that users actually executed, not abandoned).
  2. Aggregate query frequencies daily using a batch pipeline (Spark).
  3. Compute trending queries using a streaming pipeline (Flink) that detects sudden frequency spikes.
  4. Rebuild the prefix index periodically with updated frequency counts.

Say this out loud

Query autocomplete matches typed prefixes against a corpus of historical queries stored in a trie or sorted index. Candidate completions are ranked by popularity, recency, and personalization. Results are filtered for offensive content and deduplicated before displaying 5 to 8 suggestions within 100ms of each keystroke.

Followups to expect

  1. How do you handle typos in partial queries? Use fuzzy matching (edit distance 1 to 2) during prefix lookup, or correct the prefix using a spelling correction model before trie traversal.
  2. How do you prevent autocomplete from amplifying misinformation? Suppress suggestions related to unverified claims, conspiracy theories, or content flagged by fact-checkers using a content policy blocklist.

Check yourself

Question 1 of 3

What data structure enables sub-millisecond prefix lookup for autocomplete suggestions?

More in ML System Design

See all →
A Framework for Any ML Design Round5 minFraming a Business Problem as ML5 minOnline vs Offline Evaluation5 min