Bird
Raised Fist0

Design a Typeahead Suggestion System - Trie at Scale, Ranking, Personalization, Cache

HLDmediumInterview freq: high
Choose your preparation mode4 modes available
▶
Request Trace
Step 1/8
API Gateway

Receive prefix query

User types prefix 'app', API Gateway accepts query

Protocol:WebSocket
BeforeUser has typed prefix 'app' on client device
AfterAPI Gateway received prefix query 'app' for processing
Redis Cache

Check cache for prefix suggestions

Lookup 'app' in Redis Cache for cached suggestions

Protocol:Redis GET
BeforeAPI Gateway has prefix query 'app' to resolve
AfterCache miss for 'app'; no cached suggestions found
Ranking Service

Request candidate suggestions

Ranking Service queries Trie Index Shards for prefix matches

Protocol:gRPC
BeforeCache miss for prefix 'app' confirmed
AfterRanking Service received candidate suggestions from Trie shards
Trie Index Shards

Lookup prefix in distributed trie

Trie shards return all suggestions starting with 'app'

Protocol:gRPC
BeforeRanking Service requested prefix matches for 'app'
AfterTrie shards returned candidate suggestions for 'app'
Personalization DB

Fetch user preferences

Ranking Service queries Personalization DB for user history

Protocol:NoSQL Query
BeforeRanking Service has candidate suggestions for 'app'
AfterUser personalization data retrieved for ranking
Ranking Service

Rank suggestions

Combine trie results and personalization data to rank suggestions

Protocol:Internal processing
BeforeRanking Service has candidates and personalization data
AfterRanking Service produced ranked, personalized suggestions
Redis Cache

Cache ranked suggestions

Store ranked suggestions for prefix 'app' in Redis Cache

Protocol:Redis SET
BeforeRanking Service has ranked suggestions ready
AfterRedis Cache updated with new suggestions for 'app'
API Gateway

Return suggestions to user

Send ranked suggestions back to client over WebSocket

Protocol:WebSocket
BeforeRedis Cache updated with fresh suggestions
AfterUser receives ranked, personalized suggestions for 'app'
🏗️
Architecture Diagram
Architecture Diagram
┌───────────────┐
│  API Gateway  │
└───────┬───────┘
        │ WebSocket
        ▼
┌───────────────┐
│ Redis Cache   │
└───────┬───────┘
        │ Redis GET
        ▼
┌───────────────────┐
│ Ranking Service   │
└───────┬───────────┘
        │ gRPC
        ▼
┌───────────────────┐
│ Trie Index Shards │
└───────┬───────────┘
        │ Kafka
        ▼
┌───────────────────┐
│ Suggestion DB     │
│ (Elasticsearch)   │
└───────┬───────────┘
        │ NoSQL Query
        ▼
┌───────────────────┐
│ Personalization DB │
└───────┬───────────┘
        │ Kafka
        ▼
┌───────────────────┐
│ Indexing Workers  │
└───────────────────┘

This architecture uses an API Gateway to receive user prefix queries, which first checks a Redis Cache for low-latency hits. On cache misses, the Ranking Service queries distributed Trie Index Shards to retrieve candidate suggestions. The Ranking Service then fetches user personalization data from the Personalization DB to produce a ranked, personalized list. These results are cached back in Redis for future queries. Kafka Cluster and Indexing Workers maintain trie shards and databases up-to-date asynchronously. This design balances fast cache hits with scalable, personalized ranking and ensures fresh data via event-driven indexing.

Primary Focus

✓

Practice

(1/5)
1. A web service needs to distribute incoming TCP connections evenly across backend servers without inspecting HTTP headers. Which load balancer type is most appropriate?
easy
A. Layer 7 load balancer that routes based on URL paths
B. Application gateway that terminates SSL and inspects HTTP content
C. DNS-based load balancing with round-robin IP addresses
D. Layer 4 load balancer that forwards packets based on IP and port

Solution

  1. Step 1: Identify load balancer function

    Layer 4 load balancers operate at the transport layer, forwarding TCP/UDP packets based on IP and port without inspecting application data.
  2. Step 2: Eliminate options requiring application-level inspection

    Layer 7 load balancers and application gateways inspect HTTP headers or content, which is unnecessary and adds overhead here.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Layer 4 load balancer forwards by IP/port without payload inspection [check]
Hint: Layer 4 balances TCP/UDP by IP/port, no HTTP check [OK]
Common Mistakes:
2. If the cache warming service crashes after partially warming keys but before marking completion, what is the immediate failure observed during launch?
medium
A. The cache contains stale data from previous runs causing incorrect responses
B. The cache warming service automatically retries from the start, causing duplicate warming and wasted resources
C. The system falls back to a read-through cache mode with no performance impact
D. Clients experience cache misses for keys not warmed, causing increased latency and possible DB overload

Solution

  1. Step 1: Trace failure impact of partial warming crash

    Without completion mark, warming restarts causing duplicate warming and resource waste.
  2. Step 2: Eliminate other options

    Stale data unlikely if warming overwrites; cache misses occur but duplicate warming is immediate failure; fallback read-through causes latency, not immediate failure.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Crash without completion causes duplicate warming retries [check]
Hint: Crash without completion mark causes duplicate warming retries [check]
Common Mistakes:
3. If the Kafka broker cluster loses ZooKeeper coordination, what exactly breaks in the message delivery process?
medium
A. Messages are lost immediately as brokers cannot write to logs
B. Producers can still write, but partition leadership cannot be elected, causing delivery stalls
C. Consumers receive duplicate messages due to offset corruption
D. The entire cluster shuts down automatically to prevent data loss

Solution

  1. Step 1: Role of ZooKeeper in Kafka

    ZooKeeper manages partition leadership and consumer offsets; losing it affects coordination but not immediate message loss.
  2. Step 2: Failure impact on delivery

    Without ZooKeeper, offset metadata can become inconsistent, causing consumers to re-read messages and receive duplicates (C); producers may still write (B is partially true but leadership election stalls eventually); cluster does not shut down automatically (D).
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    ZooKeeper loss -> offset inconsistency -> duplicate consumer messages [check]
Hint: ZooKeeper loss corrupts offsets causing duplicates, not immediate loss [check]
Common Mistakes:
4. For a vector database supporting real-time updates and low-latency ANN queries, which architecture best balances update speed and query performance?
medium
A. Use a single monolithic HNSW index rebuilt entirely on every update
B. Maintain an immutable HNSW index for bulk data plus a small dynamic index for recent inserts
C. Use a brute-force linear scan for all queries to avoid index complexity
D. Store vectors in a relational DB and compute ANN on-demand

Solution

  1. Step 1: Evaluate update and query trade-offs

    Rebuilding entire HNSW on every update (Use a single monolithic HNSW index rebuilt entirely on every update) is too slow for real-time.
  2. Step 2: Consider hybrid index approach

    Maintaining an immutable bulk index plus a small dynamic index (Maintain an immutable HNSW index for bulk data plus a small dynamic index for recent inserts) allows fast queries and incremental updates.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Hybrid immutable + dynamic HNSW balances update speed and query latency [check]
Hint: Hybrid immutable plus dynamic index enables real-time updates [check]
Common Mistakes:
5. At 10x scale, which Pastebin component is most likely to fail first and why?
hard
A. The relational database, because complex joins for analytics become a bottleneck
B. The analytics event ingestion pipeline, because Kafka partitions saturate and cause backpressure
C. The Expiration Worker, because scanning all snippets for TTL expiry becomes too slow
D. The snippet storage layer, because key-value stores cannot shard keys efficiently at scale

Solution

  1. Step 1: Analyze scaling bottlenecks

    Expiration Worker scans all snippets to delete expired ones; at 10x scale, scan latency grows linearly.
  2. Step 2: Compare other components

    Relational DB (A) is not primary storage; key-value stores (C) shard well; Kafka (D) scales with partitioning but expiration scanning is a linear bottleneck.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Expiration scanning is linear and slows down first at scale [check]
Hint: Expiration scanning scales linearly, fails first at 10x [check]
Common Mistakes:
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard