Search Autocomplete: Deep Dives and Curveballs
Nothing on the board is wrong at minute thirty, and the last fifteen minutes go to what could still fail quietly: a table you left undecided, a daily job that can put a bad list in every box, and a swap that could empty the edge on a schedule. After that the interviewer changes the requirements four times, and the fast path has to survive each one.
What to Precompute
This is the choice you left open as table or trie. One option is a trie, the tree of letters, with a list on each node. The other is a plain table from prefix to five answers. The table wins here, because a lookup is one read and a table partitions by hash with nothing more to decide. A trie split across machines would first have to settle where each subtree lives.
The trie does not disappear. It stays in the builder, where each node's list is merged from its children's lists, far cheaper than searching beneath every node. The trigger back to a walkable tree is typo tolerance, which explores neighbouring letters and cannot do that in a table of exact prefixes. Scope ruled typos out, so if the interviewer brings them back, this decision moves first.
Then top five or top ten. Five more IDs add another 20 GB, which takes 46 GB to 66, past the half of a machine the swap relies on. At ten times the size the partitions' 450 GB becomes 650 GB, and at 50 GB a server that takes about 10 partitions to about 13. What they buy is a removed term leaving a list still full. Stay at five unless removals are routine for this product, because a list that runs one short for a day, and says so, harms nobody.
The Slow Path
Three rules keep the counts honest. Count finished searches, not keystrokes, or half-typed words become popular. Cap one client's count for one query in one day, or one script writes a suggestion. Weight recent days, so that a 30-day window still leans toward this week.
Then going live, since a roll with no check in it carries a bad index to everyone. Compare the new index with the old on a sample of prefixes. Load it on one server first, the same move as rolling a change through one cell at a time in Chapter 6's Failure Across Many Services. Keep the previous version. A failed build does nothing by itself, because yesterday's index keeps serving, so the alarm watches the index's age and not the job's exit.
Uneven Prefixes and the Swap
Each layer takes its share of an uneven load. The edge takes the shortest prefixes. The app server's table takes what the edge misses. The partitions take the long tail, which at ten times the size is nearly all of the entries and about a third of the letters typed.
The trap is the swap. Suppose the edge's key carries the version. Then every swap misses everywhere at once, the cold tier of Chapter 2's When the Cache Hurts, by appointment. At ten times the size, that is 200,000 a second more on the origin, which is already carrying 200,000. Its load doubles until about 18,000 keys have refilled at each location. Assume 100 locations, and that is under 2 million fetches, about 10 seconds, every day, on an app tier run at half its limit, so it spends all the headroom. That lesson prefers a version in the key to a flush, and for one rare event it is right. Run daily across every key, it is a flush with a calendar entry.
So the key does not carry the version. Entries age out on a jittered expiry, one with a little randomness in it so that they do not end together, and different places show different days' lists for a few hours, which the agreed lag allows. One more cost: a rollback does not reach the edge, so a bad list stays there until it expires unless you purge the short prefixes too, and browsers keep theirs. Say it as the trade it is: "I'd keep the version out of the edge's key, or every daily swap is a cold start I scheduled myself. A few hours of two lists is fine, because we already agreed on a day."
Rebuild and swap produces a whole read-only index on the side, so readers never wait, and every suggestion is as old as the last build. Use it when a day's lag is acceptable.
Update in place changes lists as searches arrive, so they are current, and every search writes along its path, on the entries readers use most. Use it only for a small set of fresh terms, beside the main index.
Curveballs
"A trending query must appear within minutes." Leave the daily index alone and add a small overlay. A stream job counts the last few minutes of searches and writes the risers, the queries climbing fastest, to a small table by prefix, and a lookup merges the two. It costs a second read on the path, a purge of a riser's prefixes at the edge as it climbs, browsers that keep the old list until theirs expires, and a target for anyone pushing a term up. Windows over a stream are Chapter 21's.
"A term must come down now, for legal reasons." Ask the deadline first, because the rest is worked back from it. A block list checked at serving time stops the origin at once. A stored top ten fills the gap, and with five stored the list runs one short until the next build. A list cut short this way has to say so, because the browser reads fewer than five as all there is and would stop asking. The term has as many prefixes as letters, so purge those, ordering the edge to drop them, and work back from the longest expiry, since a purge reaches each location on the vendor's schedule and browsers cannot be recalled. Then add it to the block list the builder applies, or tomorrow's build brings it back.
"Suggestions should be personal." Do not precompute per user. Split the answer the way Chapter 2's lesson on the edge splits a page: the shared five stay cacheable, and the user's recent searches come from the device and merge on screen, spending none of the budget. A call to the origin per keystroke undoes the edge, and it has to justify that.
"Several languages." Build one index per language, with the language in the key. Text runs up to 4 bytes a character in UTF-8, so the bytes grow. The short-prefix sum changes too: a script with thousands of characters has far more than 18,000 short prefixes, whatever the traffic. And some scripts are composed before a letter is final, so what counts as a keystroke is a question to ask.
How Each Level Answers
A mid-level candidate reaches a trie and gets to a top five on the node once the search under one letter is priced. They move the build offline when asked what serves during it, and they add browser caching with a hint.
A senior candidate starts from the budget, subtracts the network, and rules out computing at query time as soon as the subtree is priced, saying which box will replace it. They keep the two paths apart unprompted, size memory and rate separately, and find on their own that short prefixes are half the traffic.
A staff candidate asks who can push a term into the box and who owns the block list, because a suggestion is something the company appears to say. They treat index rollout as the riskiest routine change in the system, price a keystroke in egress, and ask whether the far reader or the takedown deadline sets the edge's expiry.
- Rebuilding the system to answer the trending curveball, when a small overlay is the change. The interviewer watches a working design get thrown away.
- Answering the takedown with "remove it from the index", while the next swap is hours away and the edge still holds it.
- Promising personal suggestions without noticing that every cache on the path has gone.
- The staleness of each layer stated before the takedown question arrives.
- The fast path protected from every curveball, with what each one adds to it.
- The deadline asked for, then worked back from the longest expiry.
Knowledge Check
"A trending query must appear within minutes." What is the smallest change that does it?
- Run the whole build every few minutes in place of once a day
- Keep the daily index, and merge in a small table of risers
- Update the live lists in place from a stream of searches
- Cut the edge's expiry down to seconds for every prefix
"A term must come down now, for legal reasons." What does the removal have to reach?
- The next index file, rebuilt and swapped as soon as it is ready
- The edge alone, where one purge of the term removes every copy
- The search log, so that no later line can ever count the term
- The serving tier, the edge for each prefix, and the builder's list
"Suggestions should be personal." Why does a personal answer on every keystroke cost so much?
- An answer that differs per user cannot sit at the edge or any shared cache
- Each user's own full index would have to be downloaded to the browser
- A user's ID cannot be read from a request within the 30 milliseconds
- The search log would have to be written once more for each letter typed
Why should the edge's cache key leave out the index version?
- The edge cannot store a key that has an index number in it
- Without it, all places always show the same day's list
- With it, each daily swap misses everywhere and doubles the origin's load
- With it, going back to yesterday's index is no longer possible
You got correct