Lesson 2 · Minutes 10-20

Search Autocomplete: First Design

Problem

Ask what structure answers "everything starting with these letters", and a trie is the honest reply, so that is what goes on the board at minute ten. One server holds it in memory. A daily job feeds it. On a small site that is correct and fast, and by minute twenty you will have walked one keystroke through it, followed one day's counts into it, and said how many queries it takes to break it.

The 3 seconds from Scope are already in your notes, and they are no reason to skip this step. Precomputing from the first breath leaves the interviewer nothing to push on. Their four pushes wait in the quiz at the foot of this page, and the next lesson answers them in that order.

A Trie and a Daily Job

The browser sends each prefix to one suggest server, which holds a trie in memory. Every node stands for one more letter, so the path from the root to a node spells a prefix. A node where a query ends also carries that query's popularity count.

A lookup has two parts. It walks the letters of the prefix down to a node, and then it searches everything beneath that node and keeps the five highest counts. Beside this runs the only other moving part: once a day a job adds up the search log into query counts, and the server rebuilds its trie from them.

The first design
Browsersends every letter Suggest serverone trie in memory Search logfinished searches Count jobonce a day prefix, five back rebuild readasync

One Keystroke, One Day's Counts

Someone has typed two letters, a and p. The browser sends a GET with that prefix. The server steps from the root to a, then to p, visits every node below it, keeps the five highest counts it has seen, and returns those five queries as strings. The browser draws the list, and the next letter starts the same trip again.

Now the other half. Someone finishes a search, and the search system writes one line to the log. Once a day the count job reads those lines and adds them up by query over the agreed 30 days. The server takes the counts, builds its trie again, and answers from that. A keystroke never writes anything, and a count never reaches a reader until the next rebuild.

Why It Holds on a Small Site

It returns five completions and it counts searches, which is everything the functional requirements ask for. It is also fast at this size. Assume 100,000 distinct queries. At 10 nodes a query that is about a million nodes. One in 15 under a common letter is about 70,000, and at about 100 nanoseconds each that takes about 7 milliseconds, inside the server's 30.

A trie is the natural shape for "everything starting with this", and one process has nothing to coordinate with. How a trie works inside, and how a radix tree merges its long single-child chains, is Computer Science Deep Dive's lesson on tries and radix trees. Make this case out loud ahead of any limit, because it is the reason the design is on the board at all.

One part is unfinished on purpose, and the interviewer should know that you know. Mention it in passing: "The browser sends every letter in this sketch, and what else it could do is a question I'm holding back."

Where the Search Gives Out

Four limits go on the record now, and none of them gets fixed yet. The search is the first. At 50 million queries one letter costs about 3 seconds. Assume each further letter narrows the search by the same factor of 15. Then two letters cost about 200 milliseconds, and three cost about 15 milliseconds. Popularity gives the search no order to stop early on, because the top five can sit anywhere beneath the node.

The rebuild is the second: it touches 500 million nodes at 100 nanoseconds each, about 50 seconds at best, with the trie unavailable while it runs. The third is the server itself, which takes 60,000 keystrokes a second at peak against one machine's 100,000, and which is the one failure. The fourth is that every letter is a network request.

The search is the limit to raise yourself, in about ten seconds, with both sizes in it: "A common first letter costs about 7 milliseconds at a hundred thousand queries, which is fine. At 50 million the same letter costs about 3 seconds, so one-letter lookups are what I'd watch, and the search under the prefix is what goes first."

Interview Pitfalls
  • Opening with a precomputed index and a pipeline. Both are right in the end, and with no first design the interviewer has nothing to ask about.
  • Drawing a database for the lookups. With 30 milliseconds to spend, the interviewer asks what the trip is for.
  • Saying "the trie is fast" without separating the walk, which is short, from the search beneath it. The interviewer hears that you have not priced either.
  • Never saying how the counts get in, which leaves half the system undrawn.
What Interviewers Listen For
  • A keystroke followed to its answer, and a day's counts followed into the trie.
  • The reason a trie is the right start, given before where it breaks.
  • The number of distinct queries at which one letter stops fitting, given unprompted.

Knowledge Check

"Someone types the letter a. How much of your trie does that one keystroke touch, and can you afford it?"

  • "Keep the search, but stop as soon as it has found five completions under the prefix."
  • "Put a cache of recent prefixes in front of the trie, and search only on a miss."
  • "Run the search on many threads, one per branch of the subtree under the prefix."
  • "Too much, so store each prefix's top five on its node and never search at all."

"Tonight's rebuild is running and the letters keep coming. Which index answers them?"

  • "Lock the trie for the rebuild and show no suggestions until the build has finished."
  • "Yesterday's index keeps serving, while the new one is built apart and swapped in at once."
  • "Update the live index in place from the log, so that there is no rebuild to wait for."
  • "Run the rebuild in the quietest hour of the night and accept the gap while it runs."

"Now it's ten times the searches, ten times the distinct queries. What gives out first on that one server, and what do you draw instead?"

  • "Give each letter of the alphabet its own server, so that traffic splits by letter."
  • "Add a shared cache tier in front of the index to absorb the extra keystroke traffic."
  • "Split by prefix for the memory, copy for the rate, and keep short prefixes local."
  • "Keep the index on disk in one store, and cache only the popular prefixes in memory."

"Every letter anyone types lands on your servers. Before it gets that far, what can the client do?"

  • "Hold every letter back until the user stops typing, then send one request for all."
  • "Download the entire index to the device and answer each keystroke with no network."
  • "Compress each of the answers harder, so that the requests take less of the network."
  • "Wait a beat, reuse answers it already holds, and let the edge keep short prefixes."

You got correct