Design Typeahead Search in LinkedIn (search by prefix in connections & companies)

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2024-04-16

Problem Statement

On LinkedIn, when a user starts typing in the search bar (e.g., "Micr..."), they should see instant suggestions for:


•Their connections (people they are directly or indirectly connected with).
•Companies they follow or are trending on LinkedIn.
•Other relevant entities (groups, schools, jobs).


This requires a Typeahead Search System that supports prefix-based queries, is low-latency, and scales to hundreds of millions of users and entities.


✅ Functional Requirements


1.Prefix Search
•Given a prefix (e.g., "Mic"), return top-k matching entities.
•Entities: Users (connections), Companies, Groups, Schools, Jobs.
2.Personalization
•Prioritize user’s 1st-degree & 2nd-degree connections over global results.
•Boost companies user follows or has interacted with.
3.Scoring & Ranking
•Rank results by relevance (connection degree, recency of interaction, global popularity).
4.Autocomplete Experience
•Results must appear as user types (keystroke latency < 100ms).
•Support typo tolerance (e.g., "Micorsoft" → Microsoft).
5.Scalability
•Support billions of entities (users, companies).
•Handle tens of thousands of QPS globally.
6.Multi-Entity Support
•Differentiate between people, companies, and groups in UI.


❌ Non-Functional Requirements


•Latency: <100ms end-to-end per keystroke.
•Availability: 99.99% (should not block typing experience).
•Consistency: Eventually consistent (new users/companies indexed within minutes).
•Multi-Tenancy: Work across global regions (US, EU, APAC).


🧱 System Components


1.Indexing Service
•Precompute prefix → entity mappings using Tries / FST (Finite State Transducers).
•Maintain inverted index for names.
•Support incremental updates (when user/company is added).
2.Distributed Cache / Shards
•Cache top prefixes (e.g., "Go", "Mic", "Amaz") for fast lookup.
•Shard by prefix hash for scalability.
3.Query Service
•Receives keystroke input, fetches candidate results from index.
•Applies personalization layer (boost connections, followed companies).
•Ranks using scoring model (popularity, recency, degree of connection).
4.Data Sources
•Users & connections (Graph DB or social graph service).
•Companies, groups, schools (Entity DB).
•Activity logs (for ranking, trending signals).
5.Ranking Layer
•Lightweight ML model for re-ranking top-N candidates.
•Features: connection degree, user interactions, popularity score.
6.Client-Side Optimization
•Debouncing keystrokes (send queries only after ~50ms pause).
•Prefetch results for common prefixes.


🔄 Example Flow


User types "Mic" →


1.Client sends query "Mic" to Typeahead Service.
2.Index Lookup finds candidates: Microsoft, Michael Smith, MicroStrategy, Micron.
3.Personalization Layer boosts:
•Michael Smith (1st-degree connection).
•Microsoft (company user follows).
4.Ranking Layer reorders based on score.
5.Top-K (say 5) results returned in <100ms.


⚖️ Trade-offs & Design Considerations


•Index Structure: Trie/FST (fast prefix lookup) vs inverted index (flexible ranking).
•Updates: Batch updates (every few mins) vs real-time (complex but fresher data).
•Personalization: Precompute top-k per user (faster, higher memory) vs on-the-fly scoring (slower, lower memory).
•Ranking: Simple heuristics vs ML-based.
•Sharding: By prefix hash vs entity ID.


🧪 Open Discussion Points


•Should results be strict prefix-only or allow fuzzy matches (typos)?
•How to handle multi-lingual names (accented characters, non-English alphabets)?
•How to efficiently merge global popularity results with user-personalized results?
•Should we support offline caching on client side for top queries?


👉 This problem tests low-latency distributed search systems, prefix indexing, caching, personalization, and ranking trade-offs.

Solution

Comments