Skip to content
Commitwise
Menu

Design search autocomplete (typeahead) for a large site.

System designSenior

In shortPrecompute the top suggestions per prefix offline from query logs, serve them from an in-memory structure (a trie or a prefix-to-suggestions map) replicated across nodes and cached at the edge, and keep the client fast with debouncing, cancellation and local caching.

Model answer

Requirements: suggestions within ~100 ms of a keystroke, top 5–10 per prefix, ranked by popularity (and freshness), billions of queries per day, freshness of hours is acceptable for most terms.

Offline pipeline: query logs → aggregation job (hourly or daily, Spark or Flink for streaming freshness) → counts per query with time decay → for each prefix, the top-K completions → a compact serving artifact. Filter offensive or low-quality terms here.

Serving: load the artifact into memory on typeahead nodes: a trie with top-K cached at each node, or simply a hash map from prefix (up to ~20 characters) to its suggestions, which is fast and easy to shard. Shard by prefix range or by hash of the first characters, replicate each shard, and swap artifacts atomically on each build.

Caching: responses for short prefixes are identical for everyone, so cache them at the CDN with short TTLs; most traffic hits the first one to three characters.

Client: debounce keystrokes (~100–150 ms), cancel in-flight requests when the prefix changes, cache recent responses locally, and render optimistically from cached longer prefixes.

Extensions: personalization by blending global suggestions with the user’s history on the client or a separate service; trending terms from a streaming path with a small fresh index merged at query time; locale-specific artifacts.

What they're looking for

Latency budget (under ~100 ms end to end), separating the offline aggregation pipeline from the online serving path, prefix data structures and their memory size, ranking and freshness, sharding, caching at CDN and client, and client-side behavior.

Common mistakes

  • Running LIKE 'prefix%' queries on the main database per keystroke.
  • Computing rankings at query time.
  • Ignoring personalization, filtering of offensive terms and multi-language input.

More system design questions

Esc

Type to search the whole site.