Design a typeahead (search autocomplete) system
MediumConceptSocial Platforms
Answer
Requirements: return the top five or so suggestions for a typed prefix within about 100 ms, ranked by popularity, at a very high query volume.
- Data structure: a trie in which each node stores its precomputed top-k completions, so a lookup is just a walk down the prefix, independent of how many queries match it.
- Data pipeline: query logs are aggregated offline (for example, hourly or daily) into frequency counts, and a new trie is built and swapped in; serving nodes do not update it on every keystroke.
- Serving: shard the trie by prefix and replicate it; cache popular prefixes in Redis and in the browser; clients debounce keystrokes to cut request volume.
- Trade-off: offline rebuilding keeps lookups fast and simple but makes suggestions slightly stale; trending terms need a separate real-time path.
More in Social Platforms
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | Design a photo-sharing service like Instagram | Concept | Medium | Read the answer to Design a photo-sharing service like Instagram | |||
| 2 | Design a typeahead (search autocomplete) system | Concept | Medium | Read the answer to Design a typeahead (search autocomplete) system | |||
| 3 | Design a news feed system | Concept | Hard | Read the answer to Design a news feed system | |||
| 4 | Design a chat application like WhatsApp | Concept | Hard | Read the answer to Design a chat application like WhatsApp |