Question #58 of 60•System Architectures•Medium• ~3 min read

How do you design an elastic search-as-you-type autocomplete system (Tries vs. Inverted Indexes)?

Executive Takeaway

Build a real-time search autocomplete system with sub-30ms latency: Prefix Tries with top-K cached suggestions, frequency tracking, inverted index tokenization, and distributed Trie sharding.

Architectural Breakdown & AnalysisFAANG Aligned

Search autocomplete (query auto-suggestion) powers Google, Amazon, and YouTube search bars. As a user types each character (e.g., "sys"), the system must return the top 5 to 10 most relevant, trending suggestions within sub-30 milliseconds before the user types the next letter.\n\n### 1. Data Structure Choice: The Prefix Trie\nA standard relational database LIKE 'sys%' query scans millions of rows and collapses under high concurrency. The optimal data structure is a Prefix Trie (Prefix Tree):\n* Each node in the Trie represents a character.\n* A path from the root to a node spells out a search prefix.\n* Leaf nodes store the full search query and its historical search frequency score.\n\n### 2. The Critical Optimization: Cached Top-K at Each Node\nTraversing down the Trie to find prefix "sys" and then searching all descending subtrees to sort by frequency is $O(\text{all sub-queries})$, which is too slow.\n* Production Solution: Store the Top-K (e.g., Top 5) highest-scoring queries directly inside each Trie node during offline indexing.\n* Query Complexity: Finding suggestions drops to $O(L)$ where $L$ is the length of the typed prefix (typically 3 to 15 characters), completely independent of total database size!\n\n### 3. Distributed Sharding & Scaling the Trie\nWith hundreds of millions of search terms, a Trie exceeds single-machine RAM (20 GB to 100 GB):\n* Sharding by First Letter (Naive): Sharding by a-z produces massive hotspots on popular letters like s or c.\n* Sharding by Hash of Prefix: Partition based on hash(prefix) % N to balance load evenly across worker nodes.\n* Aggressive Edge Caching: 80% of searches follow the Pareto distribution (the same top 20% queries). Cache top autocomplete payloads in CDN edge nodes (Cloudflare/CloudFront) with a 1-hour TTL.\n\n### Key Architectural Comparison\n| Autocomplete Engine | Latency | Memory Footprint | Typo Tolerance | Update Overhead |

:---:---:---:---:---
Inverted Index (Elasticsearch)20ms - 50msHigh (Lucene segments)Excellent (Fuzzy Levenshtein matching)Dynamic near real-time updates
Relational SQL (`LIKE`)> 200msMinimalNoneInstant

| Client-Side Trie (Mobile/Local) | < 1ms | Low (Cached top 5,000 terms locally) | Simple | Fetched on app launch |\n

Previous QuestionHow does the Raft consensus algorithm guarantee Leader Election and Log Replication?Next Question How do you eliminate the Thundering Herd / Cache Stampede problem under massive concurrent load?

Continue Learning on Being SDE

Explore deep-dive system architecture modules, low-level object-oriented designs, and interview cheat sheets.