System DesignSep 29, 2026⏱️ 3 min read
Design a Search Autocomplete System: Trie Top-K Caching, Sharding & Analytics

Design a Search Autocomplete System: Trie Top-K Caching, Sharding & Analytics

Every keystroke a user types triggers an autocomplete query (GET /v1/search?q=sys). To feel instantaneous without jitter, responses must arrive in under 100ms across ~24,000+ peak QPS (assuming 10M DAU × 10 searches × 20 keystrokes $\approx 2\text{B queries/day}$).
Running SQL SELECT ... WHERE query LIKE 'sys%' ORDER BY frequency DESC LIMIT 5 destroys databases under that load. Instead, production typeahead runs on in-memory Trie Caches built asynchronously.
Search Autocomplete Trie Architecture

Search Autocomplete Trie Architecture


1. The Core Data Structure: Trie with Cached Top-K

A standard Trie (Prefix Tree) locates a prefix of length $p$ in $O(p)$ time, independent of whether you store 10,000 or 100 million phrases.
However, traversing every descendant node under "s" to sort millions of suffixes on every keystroke is still $O(n)$ too slow.
The Optimization: Store the Top $K$ (e.g., Top 5) most popular queries directly at every Trie node during offline compilation!
Root
 └── "s" [top 5: "system design", "spotify", "stripe", "sql", "swift"]
      └── "sy" [top 5: "system design", "system design interview", "symfony", ...]
           └── "sys" [top 5: "system design", "system design interview", ...]
With pre-cached Top-K arrays at each node (plus capping prefix depth at max_length = 50), lookup time drops to $O(1)$ constant time!

2. Two Separate Pipelines: Gathering vs. Serving

Never mutate the live serving Trie synchronously on every search click—writes would contend with read locks and invalidate caches.

Path 1: Data Gathering Service (Asynchronous Offline Pipeline)

  1. Analytics Logs: Append search queries + timestamps to Kafka / S3 logs ("system design", ts).
  2. Aggregators: MapReduce / Spark / Flink jobs compute weekly (or hourly for trending news) query frequencies.
  3. Trie Builder & DB: Constructs the serialized Trie snapshot and stores it in a Trie DB (e.g., Cassandra/Document Store key-value snapshot per prefix).

Path 2: Query Serving Service (Read-Only Speed)

  1. Browser Cache + CDN: Cache static high-frequency prefixes like "a", "p", "py" right in browser memory (Cache-Control: max-age=3600) and CDN edge.
  2. Debouncing: Clients debounce keystrokes by ~100–150ms to cut redundant network calls in half.
  3. Trie Cache: Fleet of in-memory servers load the latest read-only Trie snapshot and swap pointers atomically when updated.

3. Scaling Beyond Single-Machine RAM: Prefix Sharding

If multi-language global dictionaries exceed one server's memory:
  • Naive Range Sharding (a-m on Shard 1, n-z on Shard 2) causes massive imbalance ("s" and "c" receive vastly more traffic than "x" or "z").
  • Shard Map Manager: Analyze historical prefix distribution and pack balanced prefix buckets onto shards (e.g., Shard 1 handles "s", Shard 2 handles "u-z"), routing requests cleanly at the API gateway.
  • Safety Filter Layer: Maintain a fast profanity/blocklist filter at the serving gateway so blocked suggestions disappear instantly without waiting for weekly Trie rebuilds.

Share this article

Comments