SYSTEM DESIGN SKETCHBOOK / PRACTICE SHEET / NO. 08
Design search autocomplete
As someone types into a search box, a short list of suggested queries appears under it and updates with every keystroke. Good suggestions save typing and steer people toward queries that return useful results; slow or stale ones are ignored, and offensive ones make the news.
Requirements
- Return up to 10 suggested completions for the text typed so far, ordered by likelihood.
- Suggestions come from what other people have searched, weighted toward recent popularity.
- A query that suddenly becomes popular (breaking news) starts appearing as a suggestion quickly.
- Blocked terms (offensive, illegal, personal data) never appear as suggestions.
Constraints and assumptions
- 5 billion searches a day; with typing debounce the client sends about 5 suggestion requests per search.
- Server-side p99 under 30 ms per suggestion request; the whole keystroke-to-render budget is about 100 ms.
- About 1 billion distinct queries in the last 30 days, but only queries typed by at least 50 distinct users may be suggested.
- A newly trending query should be suggestible within 15 minutes; a newly blocked term must disappear within 5 minutes.
- One language and market for the first design.
Out of scope for the first sketch
Spelling correction and fuzzy matching · The search results page itself · Multiple languages and scripts
Draw your design
Rough math and notes
SYSTEM DESIGN SKETCHBOOK / FOLLOW-UPS
Design search autocomplete
Finish your first sketch before answering these. Cross out freely.
- Walk through the 100 ms from keystroke to rendered list. Where does the time go, and how can the client avoid sending some requests at all?
- Your main suggestion index is rebuilt once a day. How does a query that took off 10 minutes ago get into suggestions?
- Someone reports an offensive suggestion. How do you make it disappear everywhere within five minutes?
- Product wants each person's own recent searches mixed into their suggestions. Where does that data live and where is it merged?
Compare afterwards: worked approaches and reading at sketchbook /questions/search-autocomplete/review