Chapter Nine · Problem Three

Search Autocomplete

A search box with about 200 milliseconds to answer each letter, and everything that budget forces over 45 minutes.

4 lessons · 45 minutes
The Question

"As a user types in the search box, show the five most popular completions before the next letter is typed."

Most questions leave the deadline for you to ask about. This one states it in four words: before the next letter. Someone is typing, the gap between two keys is short, and every key is a new request whose answer has to land inside that gap. Nearly everything hard about the problem comes from taking those four words literally.

The skill on test is letting a budget make your decisions. The budget rules out computing anything while the user waits, which leaves answers prepared ahead and held in the layers Chapter 2's Caching at Scale draws, with the traps of When the Cache Hurts and the guarantees of The Edge: CDNs as a Black Box. The prepared answers are split the way Chapter 3's Range, Hash and Directory says, and Hot Partitions explains why the busiest of them have to be stopped before any partition sees them. The log that feeds it all is the one in Chapter 6's Queues, Logs and Pub-Sub.

The level runs from mid to senior. A mid-level candidate reaches precomputation once a search has been priced, and a senior candidate separates the two paths without being asked. Three problems come with it: a budget per keystroke that leaves no room to compute at query time, a slow path from query logs to counts to a rebuilt index that stays apart from the fast path and goes live in one atomic swap, and popularity so uneven that one-letter prefixes are the hottest keys in the system.

Start a 45-minute timer and draw your own version first, with a number next to each box. The clock gives ten minutes each to Scope, First Design and Scaling It, then fifteen to Deep Dives and Curveballs, and the four lessons follow it. Your sketch will part ways with these pages somewhere, and that is the place to read most closely.

The 45 minutes of this problem, lesson by lesson
Scopeminutes 0-10
→
First Designminutes 10-20
→
Scaling Itminutes 20-30
→
Deep Divesminutes 30-45

Lessons in This Chapter