Every keystroke is a request, which makes this one of the busiest endpoints a company runs. The trick is to do the expensive ranking offline and make serving a plain memory lookup.
Requirements. Return the top 5 completions for a prefix, ranked by popularity, with trending searches within about an hour and blocked terms never shown; answer in under 100 ms end to end, about 20 ms of it on the server.
Requests
23,000 per second2B keystrokes a day, peak about 50,000
Prefix index
about 40 GB1B prefixes × 5 IDs × 8 B, so shard it
Server budget
about 20 msof 100 ms end to end
The design. Ranking happens offline. Search logs feed a batch job that counts the last few weeks and a streaming job that counts the last hour; a builder merges the scores into a trie that stores the top 5 completions at every node, and serving nodes swap it in. Serving walks down one node per typed character and returns the stored list. Debounce in the client, cache short prefixes at the CDN, and shard by prefix range balanced by traffic.
Interview prompt
Design the suggestions that appear under a search box as the user types.
Requirements and estimates
Functional requirements
Return the top 5 completions for a prefix, ranked by popularity.
Trending searches appear within about an hour.
Offensive and blocked terms never appear.
Non-functional requirements
Under 100 ms end to end, so the server budget is about 20 ms.
Highly available; if suggestions fail, search still works.
Freshness of minutes to hours is fine.
Estimates
Requests 10M DAU × 10 searches × 20 keystrokes = 2B per day, ≈ 23,000/s
Peak ≈ 2× ≈ 50,000/sPrefix index ≈ 1B prefixes × 5 IDs × 8 B ≈ 40 GB plus keys, so shard it
Storing the top five at every node is what makes serving fast: a lookup walks down as many nodes as the prefix has characters and returns the stored list, with no need to search the subtree below.
Whiteboard
What you sketch in the first five minutes: the trie that makes a lookup cheap, then the pipeline that builds it away from the request.
1. The top 5 at every node
Say: never search the subtree while the user waits: store the answer at the node.
Walk one node per character: t, then tw.
Each node already holds its top 5 completions.
Return that list, with no search below the node.
Next question: a harmful term starts trending. Serving filters on a block list right away, and the next build leaves it out.
2. Build offline, swap in
Say: the expensive ranking happens away from the request.
Search logs flow into a batch count of weeks and a stream count of the last hour.
A builder merges the scores and writes a new trie to object storage.
Serving nodes load it and swap it in atomically.
Next question: how fresh is it? Trending terms appear within the hour, from the stream count.
Where suggestions live
Approach
Serving
Updating
Fits when
Trie in memory, top 5 per nodeCommon pick
Walk the prefix, return the list
Rebuilt offline and swapped in
The classic interview answer
Prefix → top 5 in a key value store
One GET per keystroke
Batch write every prefix
Simple to shard and cache, more storage
Search engine completion suggester
In memory automaton with fuzzy matching
Near real time indexing
You already run Elasticsearch or OpenSearch and need typo tolerance
The data pipeline
Search logs flow into a stream. A batch job counts queries over the last few weeks with older counts weighted less; a streaming job counts the last hour to catch trending terms. The scores are merged, a builder produces a new trie, writes it to object storage, and serving nodes load it and swap it in atomically. Reads never wait for writes.
Cutting the load
Debounce in the client: send only after a short pause in typing.
Cache responses in the browser, and at the CDN for short prefixes, which are the hottest and identical for everyone.
Shard by prefix range using a shard map balanced by traffic, because far more queries start with “s” than with “x”.
What the interviewer probes next
A harmful term starts trending. How do you remove it right now?
Filter results against a blocklist at serving time, before returning them. The next rebuild removes the term from the trie itself.
How would you personalize suggestions?
Blend the global top results with the user’s own recent searches, kept on the device or in a small per user store, and merge them in the client or at the edge.
How do you handle typos?
Fuzzy matching by edit distance. It is much more expensive, so run it only when the exact prefix returns few results; completion suggesters in search engines support it directly.
What about other languages?
Build one trie per language or region, normalize Unicode, and for languages typed through an input method, such as Chinese pinyin, index the typed form as well as the final characters.