Designing Search Autocomplete / Typeahead

Difficulty: Intermediate Topics: Trie, Prefix Matching, Ranking, Caching, Real-time Trending Asked at: Google, Amazon, Microsoft, LinkedIn, Uber, Flipkart · Prerequisites: Caching, Database Indexing, and Scalability


1. Understanding the Problem

Search autocomplete predicts what a user is about to type and suggests completions in real-time as they press each key. It powers the dropdown under every search bar — Google, Amazon product search, YouTube, LinkedIn people search. The core challenge: return the top-k most relevant suggestions for any prefix in under 100ms, while continuously learning from billions of new queries to keep suggestions fresh and trending-aware.

Real examples: Google Search Suggestions, Amazon product typeahead, YouTube search, LinkedIn search, Spotify song search.


2. Naive First Cut

flowchart LR
    USER["User types prefix"]
    API["API Server"]
    DB[("SQL DB<br/>all queries + counts")]

    USER --> API
    API -->|"SELECT WHERE query LIKE 'pre%'<br/>ORDER BY count DESC LIMIT 10"| DB

Store every query with its count in a SQL table. On each keystroke, run a LIKE prefix query ordered by count.

Why this breaks:

  • LIKE ‘prefix%’ on billions of rows is too slow even with B-tree indexes (full scan for short prefixes like “a”)
    💡 An index helps when it narrows the search to a few rows. A one-letter prefix matches a huge fraction of the table, so the database still has to read and sort all of them - the index saves nothing on exactly the keystroke that happens first.
  • A keystroke fires every 50-100ms - SQL can’t keep up at millions of concurrent users
  • No real-time trending - counts only update in batch, so today’s viral topic won’t surface for hours
  • Single DB becomes the bottleneck - no horizontal scaling for read-heavy prefix lookups
  • No personalization - everyone sees the same suggestions regardless of context
  • Network round-trip to DB on every keystroke adds unacceptable latency

The rest of the doc evolves this into a Trie-based service with in-memory prefix lookups, async count aggregation, and a caching layer that serves most requests without hitting the data tier.


3. Prior Art We’re Drawing From

  • Google Autocomplete - Uses a combination of query popularity, freshness, and user context. Suggestions update in real-time for trending queries using a streaming pipeline separate from the batch popularity index. (Google Blog)
    💡 A batch job runs on a schedule over a block of collected data; a streaming job processes each event as it arrives. This design ends up needing both - batch for all-time popularity, streaming for what started five minutes ago.
  • LinkedIn Typeahead - Built a distributed Trie service called “Galene” that serves prefix-based entity search (people, companies, jobs) with sub-50ms P99.
    💡 Galene is LinkedIn’s in-house search and typeahead serving system - named here as evidence that a purpose-built prefix service is a real production choice, not an exotic one. Uses a two-level architecture: coarse-grained sharding by prefix + fine-grained in-memory Tries per shard. (LinkedIn Engineering)
  • Facebook Unicorn (Social Graph Search) - Typeahead over a social graph combines prefix matching with social proximity scoring (friends-of-friends rank higher). The ranking signal isn’t just popularity but personalized affinity. (Facebook Engineering)
  • Elasticsearch Completion Suggester - Uses FST (Finite State Transducer) data structure internally for prefix lookups with weighted suggestions, serving sub-5ms responses from an in-memory structure. Common choice for product search typeahead.
    💡 An FST stores a whole dictionary as a compact graph by sharing both the common prefixes and the common endings of words. It is a memory optimisation over a plain trie, which matters a great deal once Deep Dive 1 starts counting gigabytes.

4. Functional Requirements

Core (Top 3)

  1. Return top-k suggestions for a prefix - as the user types each character, return the 10 most relevant completions in under 100ms
  2. Rank by popularity and freshness - suggestions reflect both historical popularity and real-time trending queries
  3. Update suggestions with new queries - when users search for something new (a breaking event, a new product), it should appear in suggestions within minutes, not hours

Below the Line

  • Personalized suggestions (based on user history)
  • Spell correction / fuzzy matching
  • Multi-language support
  • Offensive content filtering
  • Category-aware suggestions (products vs pages vs people)

5. Non-Functional Requirements

Core

  • Low latency: P99 < 100ms - the latency 99% of requests beat (users expect instant response on each keystroke)
    💡 Quoting the 99th percentile rather than an average is deliberate: autocomplete fires on every keystroke, so a user typing ten characters meets the tail roughly once per word.
  • High availability: 99.99% - autocomplete is on the critical search path
  • Scale: 100K+ prefix lookups per second; 10B+ queries/day feeding the popularity model
  • Freshness: Trending queries surface within 5-15 minutes