Skip to content

Case study: Typeahead / autocomplete

Design a sub-50 ms search typeahead system: in-memory radix tries with precomputed top-k completions per node, front-tier prefix caching, and asynchronous stream aggregation.

35 min9 steps

By the end you can
  • Estimate keystroke QPS, compiled trie memory footprint, and query-log sampling rates
  • Explain why caching top-k completions at every trie node turns subtree scans into O(L) lookups
  • Separate the sub-50 ms read path from asynchronous query-log aggregation and trie snapshot swaps
  • Predict system behaviour during breaking-news hotspots, cache restarts, and aggregator pauses

Pin down what you are building

Top 5-10 completions

  • Rank popularity/recency; log completed queries; block unsafe phrases

Server p99 <50ms

  • Round trip <100ms; eventual popularity; fast safety updates

Why is the latency budget for typeahead much tighter than for a full search results page?

Answer this step to continue.

What this lesson simplifies

  • The runnable cell simulates 3,000 requests/s over 15,000 prefixes with a 100 ms deadline.
  • Trie lookups are modelled as O(1) key-value reads on materialised prefix keys (`pfx:{prefixKey}`).
  • Client-side keystroke debouncing and personalised re-ranking are discussed in the text.

Sources

  1. AK. Singh (Facebook Engineering). The Life of a Typeahead Query, 2010.
  2. D. Tunkelang. Prefix-Based Autocomplete at Scale, 2019.

Reviewed by Content lead on . Revision 1.