Common Designschapter 5 of 8 · 3 lessons
Search Autocomplete
Return top-k ranked suggestions for a prefix in real time (< 100ms).
Somebody types p, then i, then z. Between each keystroke you have about 100 milliseconds to show them five suggestions, and once the network takes its share your server has roughly 10.
Ten milliseconds rules out asking a database. It rules out ranking anything. Whatever you show has to be sitting in memory, already sorted, already chosen.
Then count the traffic. Autocomplete fires per keystroke rather than per search, so a product doing 10 million searches a day makes 60 million autocomplete requests, which is more than any other endpoint you run.
The trie gets all the attention in interviews. The constraint that actually shapes the design is those 10 milliseconds.
Lessons
3 in this chapter- Requirements and EstimationEvery keystroke is a request, so autocomplete traffic is a multiple of search traffic.3 min
- The Trie and the Serving PathPrecompute the top 5 at every trie node, and a query becomes a single pointer walk in memory.3 min
- The Offline Pipeline and Trending QueriesA batch job builds the trie from logs; a streaming layer keeps it honest during breaking news.3 min