Step 1: Requirements and estimates
Functional requirements:
- After each keystroke, show up to 10 suggestions that start with what was typed.
- Rank them by how often people search for them, with recent searches counting more.
- Filter out offensive or unwanted suggestions.
Non-functional requirements:
- Fast: suggestions should appear in under 100 ms, or they’re useless; the user has typed the next letter.
- Available: if suggestions fail, search must still work; it’s a helpful extra, not a required step.
- Fresh enough: popular terms can update every few hours; a breaking news term should appear within minutes.
A useful observation: most keystrokes are short prefixes (“s”, “sw”, “swi”), and the suggestions for “sw” are the same for everyone (unless personalized). That makes caching very effective.
Clarify: suggestion count, ranking signals (frequency, recency, click-through, personalization), languages and scripts, spelling tolerance, content policy, and whether suggestions are queries or entities (products, people). Non-functional: p99 under ~100 ms including the network (so the server has tens of milliseconds), very high read QPS, writes only through the aggregation pipeline, eventual consistency in hours for the base index and minutes for trending terms. Typeahead is a read-optimized, precomputed system: the online path should be a hash lookup plus serialization; all ranking happens offline.
Searches: 1 billion / day, ~4 typeahead requests per search (debounced)
Typeahead requests: 4e9 / 86,400 s ≈ 46,000 / s average, ~150,000 / s peak
Response: 10 suggestions × ~30 B ≈ 300 B → ~45 MB / s at peak
Index size:
distinct queries kept: 100 million (after dropping rare ones)
prefixes (≤ 20 chars): ~1 billion prefix entries before dedup, far fewer distinct
top-10 per prefix: ~10 × 8 B ids + scores → precompute only prefixes with traffic
→ tens of GB: shard across nodes, keep each shard in memory
Cache: prefixes of length 1–3 cover a large share of requests → CDN and in-process caches- 150,000 requests per second at peak is far too many for a database query each, but easy for in-memory lookups spread over a few servers.
- The index is tens of gigabytes, so it’s split (sharded) across servers, each keeping its part in memory.
- Short prefixes are a big share of traffic and identical for all users, so caching them near users removes most of the load.
- Top 5–10 per prefix, ranked offline by frequency with time decay (and CTR, personalization optional); content filtering.
- p99 ~100 ms end to end; online path = lookup + serialize; graceful failure (search still works).
- ~150k req/s peak; index tens of GB, sharded in memory; short prefixes dominate and are cacheable at the edge.
- Freshness: hours for the base index, minutes for trending via a streaming path.
Check this part
Why must suggestions appear in about 100 ms?
Where does ranking happen in a typeahead system?