AI Hints
Home›System Design Tutorial›Case Study: Autocomplete and Key-Value
System Design · Lesson 110 of 120

Autocomplete at Scale

Fast suggestions.

⏱ 2 min read▶ Video at 2:13:45🎓 Free
Watch this lesson in the System Design courseWatch on YouTube ↗Full System Design course

Millions of people type into the search box, every letter a request. And what people search for changes every day. How do we keep it fast, and up to date?

Autocomplete at Scale
From the video: Autocomplete at Scale
  • Millions typing
  • Every letter a request
  • Trends change daily

Two separate paths

The design splits into two paths. The query path serves suggestions, from tries held in memory and cached at the edge. The data path collects every finished search into logs. A batch job counts them, every hour or day, builds fresh tries, and swaps them in. Readers never wait for updates.

  1. Search logs
  2. Hourly counting
  3. Build new tries
  4. Swap in
  5. Serve from memory
Two separate paths
From the video: Two separate paths

Speed tricks

A few more tricks keep it fast. The browser caches recent suggestions, so backspacing is free. The app waits a few milliseconds after each keystroke, so fast typists send fewer requests. Short prefixes, like one or two letters, are cached heavily, because everyone types them. And the tries are sharded by first letters.

Browser cachebackspace is free
Debouncefewer requests
Cache short prefixeseveryone types them
Shardby first letters

Summary

Autocomplete at scale serves suggestions from in memory tries, and rebuilds them from search logs in the background. Next, designing a key value store yourself.

Key takeaways
  • Serve from memory
  • Rebuild from logs
  • Cache and debounce
Next up: A Key-Value Store — design it yourself