← Back to System Design, page 4

system-design-0079 · Medium mid

Add personalisation to a search autocomplete service: suggestions must reflect each user's own recent searches and interests while still returning in under 50 ms.

Keep the global top-k suggestions per prefix precomputed (as in a standard typeahead), and add a small per-user layer: the user's recent queries and topic interests stored in a fast key-value store. At request time, fetch the global candidates for the prefix and the user's matching history in parallel, merge them with a lightweight ranking function, and return the top 10. Personal history for 50 million users is about 150 GB, small enough for an in-memory cluster.

Full answer

Requirements

Functional:

  • As a user types, return up to 10 suggestions that include their own recent matching searches and items related to their interests, plus popular global suggestions.
  • Users can delete their history or turn personalisation off.

Non-functional:

  • p99 latency under 50 ms per keystroke request.
  • New searches appear in that user's suggestions within seconds.
  • Privacy: one user's history never appears in another user's suggestions.

Capacity estimates

Assumptions: 50M daily users, 10 searches per user per day, 5 suggestion requests per search (after debouncing), peak factor 3, 100 recent queries kept per user at about 30 bytes each.

  • Requests: 50M x 10 x 5 / 86,400 = about 28,900 per second; about 86,800 per second at peak.
  • Personal history: 50M x 100 x 30 bytes = 150 GB, which fits in a replicated in-memory store.
  • The global prefix index is already sized by the base typeahead design and is shared by all users.

API

GET /v1/suggest?q=mach&limit=10 (user from session token) -> [{"text": "machine learning course", "source": "history"}, {"text": "machine design", "source": "global"}] DELETE /v1/me/search-history PUT /v1/me/settings {"personalised_suggestions": false}

Data model

  • Global: prefix to top 50 candidate completions with scores (trie snapshot or prefix table), as in the base design.
  • user_history:{user_id}: Redis sorted set of recent queries scored by last-used time, capped at 100.
  • user_interests:{user_id}: small vector or list of topic weights (for example "python": 0.8, "system design": 0.6), updated by a batch job.
  • Candidate topic labels for global completions (computed offline).

High-level design

mermaid
flowchart LR
  U[Client] --> S[Suggest service]
  S --> G[(Global prefix index)]
  S --> H[(User history store)]
  S --> I[(User interests)]
  S --> RK[Merge and rank]
  U -- "search submitted" --> LOG[(Search log)]
  LOG --> HU[History updater] --> H
  LOG --> BATCH[Daily interest job] --> I

Ranking at request time

  1. In parallel: get the top 50 global candidates for the prefix; get the user's history entries that start with the prefix (a scan of at most 100 short strings).
  2. Score each candidate: score = a x global_popularity + b x recency_of_user_use + c x interest_match(topic), with weights tuned offline from click data.
  3. Remove duplicates, apply safety filters, return the top 10, usually with at most 3 or 4 history items so popular results still appear.

If the personal stores are slow or unavailable, return global results only; personalisation must never break the base feature.

Bottlenecks and trade-offs

  • Latency budget. Parallel fetches with tight timeouts (for example 10 ms for the personal store) and a fallback keep the p99 within 50 ms.
  • Freshness versus cost. History updates are near real time (append to a sorted set); interest vectors update daily in batch because they change slowly.
  • Caching. Global results per prefix cache well; personalised results do not, which is why the personal layer is kept tiny and fast.
  • Privacy. Delete requests must remove history from the store and from the training data path; personal entries must be shown only to their owner and only when signed in.
  • Filter bubbles. Too much personal weight hides useful new suggestions; cap personal items per response.

By level

Fresher: Explain that the service mixes popular suggestions with the user's own recent searches. A fresher should describe storing a short list of recent searches per user.

Mid: Design parallel fetches, a scoring formula and capped history, do the capacity math and set a fallback. Explain why personal results do not cache well.

Senior: Discuss latency budgets, evaluation with click data, privacy and deletion, signed-out personalisation and filter bubbles. Expect a clear failure mode plan.

Key points

  • Keep the global top-k per prefix and add a small per-user layer
  • Fetch global candidates and user history in parallel
  • Lightweight linear ranking with tuned weights
  • Fall back to global results if personal stores are slow
  • About 150 GB of personal history fits in memory

Common mistakes

  • Building a separate trie per user
  • Calling a heavy ranking model on every keystroke
  • Letting personalisation failures break autocomplete
  • Leaking one user's history to others through shared caches

Follow-ups

  • How would you personalise for signed-out users?
  • How do you evaluate whether personalisation helps?
  • How would you handle history deletion requests?

Tips

  • Reuse the base typeahead design and add a layer
  • Show the latency budget split
  • Mention privacy controls early

Evaluation rubric

  • Clear layering of global and personal data
  • Capacity math
  • Ranking and fallback within the latency budget
  • Privacy and freshness trade-offs