Courses / System Design Interview / Chapter 37
Module 8 · Design Problems III: Links, Crawling and Collaboration · Chapter 37 of 49
Design Typeahead Suggestions
Builds on Back-of-the-Envelope Estimates, Content Delivery Networks and Distributed Caches. Next up: Design Collaborative Editing like Google Docs.
Part 1 · Requirements and the latency budget
What the search box must do
Typeahead shows a short list of likely searches while the user is still typing, so a few keystrokes are enough to finish a query. This chapter designs the suggestion service behind a search box on a large site.
The functional requirements are small. Given a prefix such as cr, return the five most useful completions, best first. Useful mostly means popular, with recent popularity counting for more than old. The service should drop suggestions the site does not want to show, and it may personalise them a little. It does not run the search itself: the user picks a suggestion or presses enter, and the search service takes over.
The non-functional requirements are where this problem differs from most. A suggestion is only useful if it appears before the user has typed the next letter, so latency is the first requirement. Our own target is 100 ms from keypress to suggestions on screen, which we chose for this design and is not a published figure. Availability matters less than it seems: if suggestions fail, the box still works, so the service should fail open and show nothing instead of an error. Freshness is relaxed. A new trending query may take minutes or an hour to appear, and that is acceptable. Last, the service must handle a request on nearly every keystroke from crores of users.