Design search-suggestion autocomplete
A search autocomplete/typeahead returns ranked suggestions for a prefix in tens of milliseconds. The design splits an offline index-building pipeline (a trie with precomputed top-K) from an ultra-fast online lookup path.
Web Apps, 8 components, about 35 min
1. Scope
In scope
- • Suggest completions for a prefix
- • Rank by popularity
- • Update suggestions from query logs
- • Sub-50ms responses
Out of scope
- • Spell correction (mention)
- • Full search results
- • NLU/semantic suggestions
2. Functional requirements
Must have
- • Return top suggestions for a prefix
- • Rank by frequency
- • Very low latency
- • Reflect trending queries over time
Nice to have
- • Personalization
- • Typo tolerance
- • Multi-language
3. Non-functional requirements
4. Back-of-envelope estimation
Assumptions
- • 100K suggestion QPS (fires per keystroke)
- • 10M distinct queries indexed
- • Top-10 per prefix
- • Rebuild hourly
Derived numbers
5. API design
Top-K completions for the prefix
6. High-level design
What each box in the reference diagram is responsible for:
7. Data model
8. Deep dives
Trie with precomputed top-K
A naive trie requires traversing the whole subtree under a prefix to find the most popular completions — too slow per keystroke. Instead, precompute and store the top-K completions at each node during the offline build. A lookup then just walks to the prefix node and returns its cached list — O(prefix length).
Offline build, online serve
Separate the heavy index construction from serving. A Spark job aggregates query logs hourly into frequency counts, builds the trie with top-K, and snapshots it; serving nodes load the snapshot into memory. This keeps the hot path read-only and fast, and makes rebuilds atomic.
CDN for short prefixes
The first 1-2 characters have enormous fan-in (everyone typing "a"). Cache those responses at the CDN, offloading ~80% of traffic before it reaches your servers.
Freshness via micro-batches
Hourly rebuilds lag trends. Layer small delta merges (recent query counts) on top of the base trie so newly popular queries surface within minutes without a full rebuild.
9. Bottlenecks
- Per-keystroke QPS → CDN + in-memory trie
- Index staleness → delta merges over hourly base
- Memory for large tries → compression, sharding by first char
10. Trade-offs
Trie with top-K per node or prefix table
Choose: Precomputed prefix → top-10 table (or a trie storing top-K at each node), built offline and loaded into memory.
Instead of: Compute top-K at query time by walking the subtree: smaller index, but latency grows with subtree size.
A keystroke budget of ~50 ms leaves no room for a subtree walk; storing answers trades memory for O(prefix length) lookups.
Freshness of suggestions
Choose: Hourly or daily batch rebuild, plus a small real-time layer for trending queries merged at serve time.
Instead of: Update counts online in the serving trie: fresher, but contended writes and complex consistency.
Popularity changes slowly except for spikes; a separate trending layer covers spikes without making the main index mutable.
Where to cache
Choose: Browser cache plus CDN for 1–2 character prefixes, which are few and very hot.
Instead of: Serve everything from the origin: simpler invalidation, much more load.
There are only about 4,000 one- and two-character prefixes; caching them absorbs a large share of traffic.
11. Failure modes
Offensive or dangerous suggestion goes live
Reputational and legal harm within minutes.
Mitigation: Blocklist filter at build time and at serve time, with a hot-reloadable denylist that takes effect in seconds.
Bad index build (for example empty input)
Suggestions vanish everywhere after deploy.
Mitigation: Validate the build (size, coverage of top prefixes versus previous build) before swap; keep the previous version for instant rollback.
Keystroke storm overloads serving
Latency rises and suggestions arrive after the user has typed past them.
Mitigation: Client debounce (~100 ms), cancel in-flight requests, and drop responses for stale prefixes.
12. What interviewers probe
Answer each one yourself before opening it.
How big is the index for 10M queries?Show a strong answer
Average query ~20 chars gives ~200M prefixes before sharing; with prefix sharing and top-10 ids per node, low tens of GB. Shard by first characters if one node is not enough.
How do you personalize without breaking caching?Show a strong answer
Serve the global list from cache, then re-rank client-side or with a small personal layer (recent searches) merged in; never make the cached object per-user.
How are counts aggregated from logs?Show a strong answer
Stream query logs to a batch job that counts with time decay (for example exponential, 1-week half-life), filters, then builds prefix tables.
How do you handle typos?Show a strong answer
Fuzzy matching with edit distance 1 on prefixes longer than ~4 chars, using a spelling-correction pass or a BK-tree, ranked below exact-prefix matches.
Sign in to track this guide in your curriculum progress.