Skip to main content
BLUEPRINT #02Storage & Search

Design a Search Autocomplete System

Target AWS Architecture:DynamoDBS3ElastiCacheKinesis
Referenced Architecture Primitives (4)
Click any primitive to study its algorithmic deep dive
10-Stage Structure:1. Requirements→2. Sizing→3. Topology→4. Data Model→5. AWS Topology→6. Deep-Dive→7. Failures→8. SRE Playbooks

1. Problem Statement & Scope Clarification

System Mission

Design a real-time, globally distributed search autocomplete (typeahead) system (similar to Google Search and Amazon product typeahead) capable of returning the Top-5 most relevant, high-frequency query completions as a user types into a search box, achieving sub-10 millisecond latency at massive planetary scale.

Functional Requirements

  1. Real-Time Prefix Completion: Given a search prefix (e.g., "sys"), return the Top-5 highest-ranked completed search phrases within 10ms.
  2. Dynamic Relevance & Frequency Ranking: Rank suggestions based on historical query frequency, recency, location, and user personalization.
  3. Trending & Breaking News Ingestion: Surface sudden viral search spikes within 5 minutes without waiting for nightly batch re-indexes.
  4. Content Filtering & Safety: Filter out profanity, hate speech, and sensitive personal information using a high-speed blacklist filter.
  5. Multi-Lingual & Unicode Support: Support accent-insensitive, case-insensitive, and multi-byte UTF-8 string prefix matching.

Non-Functional Requirements (SLAs & SLOs)

  • Ultra-Low Latency: P50<3.0Β msP50 < 3.0\text{ ms}, P99<10.0Β msP99 < 10.0\text{ ms} globally. Autocomplete must render before the user types the next character (β‰ˆ150Β ms\approx 150\text{ ms} human typing speed).
  • High Availability: 99.999%99.999\% uptime .
  • Scalability: Support >100,000Β QPS> 100,000\text{ QPS} at peak.
  • Data Freshness: Batch models refreshed daily; trending query velocity merged within 5 minutes.

2. Capacity & Scale Estimation (Back-of-the-Envelope Math)

Ingest & Query Volume

  • Daily Searches: 1,000,000,000Β queries/day1,000,000,000\text{ queries/day} (1 Billion searches/day).
  • Average Keystrokes per Search: 4 autocomplete queries per completed search.
  • Total Daily Autocomplete Requests: 109Β searchesΓ—4Β keystrokes=4.0Γ—109Β requests/day10^9\text{ searches} \times 4\text{ keystrokes} = \mathbf{4.0 \times 10^9\text{ requests/day}}
  • Average : AverageΒ QPS=4Γ—10986,400Β secβ‰ˆ46,296Β QPS\text{Average QPS} = \frac{4 \times 10^9}{86,400\text{ sec}} \approx \mathbf{46,296\text{ QPS}}
  • Peak (2.2Γ—2.2\times peak multiplier): PeakΒ QPSβ‰ˆ100,000Β QPS\text{Peak QPS} \approx \mathbf{100,000\text{ QPS}}

Trie Memory Footprint Estimation

  • Total Unique Search Queries Retained: 100,000,000100,000,000 (100MΒ uniqueΒ queries100\text{M unique queries}).
  • Average Query Length: 20 characters (20Β bytes20\text{ bytes}).
  • Optimized Radix Tree / FST (Finite State Transducer) Node Layout:
    • Precomputing Top-5 suggestions at every node eliminates tree traversal during runtime.
    • 5 suggestions Γ—\times 20 bytes =100Β bytes= 100\text{ bytes} per node.
    • Pointer and structural overhead β‰ˆ40Β bytes\approx 40\text{ bytes}.
    • Total per prefix entry β‰ˆ140Β bytes\approx 140\text{ bytes}.
  • Raw Memory Footprint: [Trie](/components/13-trie-data-structure-and-inverted-index)Β Memory=100,000,000Β entriesΓ—140Β bytesβ‰ˆ14.0Β GBΒ RAM\text{[Trie](/components/13-trie-data-structure-and-inverted-index) Memory} = 100,000,000\text{ entries} \times 140\text{ bytes} \approx \mathbf{14.0\text{ GB RAM}}
  • Multiplexing across an with 3Γ—3\times replication requires only β‰ˆ42Β GBΒ RAM\approx 42\text{ GB RAM}, fitting comfortably on a modest multi-node cluster.

3. Dual-Path Architecture & AWS Component Mapping

The architecture splits into an Ultra-Low Latency Online Query Path and an Asynchronous Big Data Analytics Pipeline.

Interactive Architecture Diagram
Synthesizing vector architecture diagram...

4. API Interface Design & Wire Protocol

Typeahead Query REST/HTTP API

http
GET /v1/search/autocomplete?q=sys&limit=5&country=US&lang=en
Host: autocomplete.production.aws.internal
Accept: application/json

Response: 200 OK
Cache-Control: public, max-age=60, stale-while-revalidate=30
Content-Type: application/json

{
  "prefix": "sys",
  "suggestions": [
    {
      "query": "system design interview",
      "score": 98500,
      "type": "HISTORICAL_POPULAR"
    },
    {
      "query": "system design roadmap",
      "score": 84200,
      "type": "HISTORICAL_POPULAR"
    },
    {
      "query": "system down outage today",
      "score": 79100,
      "type": "REALTIME_TRENDING"
    },
    {
      "query": "system 32 error fix",
      "score": 64300,
      "type": "HISTORICAL_POPULAR"
    },
    {
      "query": "system architecture patterns",
      "score": 52100,
      "type": "HISTORICAL_POPULAR"
    }
  ],
  "server_timing_ms": 1.42
}

5. Storage Engine & Trie Serialization Schema

1. Radix Trie Node Structure with Precomputed Top-KK

Storing the Top-KK (Top 5) completions directly on every intermediate prefix node converts the query time complexity from O(Prefix+Tree Traversals+Klog⁑K)O(\text{Prefix} + \text{Tree Traversals} + K \log K) down to a pure O(1)O(1) memory lookup:

text
Trie Root
β”œβ”€β”€ "s" [Top-5: "spotify", "slack", "system design", "steam", "salesforce"]
β”‚   └── "y" [Top-5: "system design", "system down", "synonym", "syntax", "sync"]
β”‚       └── "s" [Top-5: "system design", "system down", "sysco", "sysadmin", "system32"]

2. Redis Key Layout & Binary Serialization Format

To optimize memory cache lines, precomputed suggestions are serialized using FlatBuffers and stored under compressed keys:

  • Key: trie:en:us:<prefix_sha1_or_string> (e.g. trie:en:us:sys)
  • Value: Binary FlatBuffer byte array containing 5 compressed query ID pointers and integer relevance scores.
  • : 86,400 seconds (Refreshed daily via snapshot deployment).

Part 2: Production Deep-Dive Locked1 Coin = 24 Hours

Unlock Complete Architecture & Production Runbooks

Your Balance:40 Coins

You have explored the free architectural preview (~43%). Spend 1 Coin to unlock the remaining 6 production deep-dive sections for a full 24 hours.

Sections Included in This 24-Hour Pass:
6. Detailed Request Flow & Resolution Workflows
7. Autocomplete Architecture Trade-Off Matrix
8. Failure Modes, Resiliency & Critical Edge Cases
9. Production Pitfalls & Anti-Patterns (The "Gotchas")
10. Production Runbook & Observability Guide
11. Interview Strategy & System Design Rubric
Keeps page unlocked for exactly 24 hoursSpend coins to fund LLM & compute infrastructure