ayushsalampuriya.xyz Revise

System Design · Design Question

Design Twitter Search

Search recent tweets at scale: ingest, inverted index, ranking, and fan-in query serving.

1. Requirements

Scope: text search on tweets; not the whole Twitter product Ask: only last 7–30 days early? archive later?

2. API

GET /v1/search?q=world%20cup&type=latest|top&cursor= → { results: [{ tweetId, user, text, ts, score }], nextCursor }

3. Data model

Source of truth: tweet store (already exists in Twitter design) Search index (inverted): term → postings list of (tweet_id, tf, ts, ...) Hashtags / mentions as special terms User filter: secondary index or posting metadata

4. High-level design

Tweet Write Path → Kafka | Indexer workers → Lucene/ES shards (inverted index) | optionally early "realtime" segment + later merge Query Path: Client → Search API → Query Planner → fan-in to relevant shards → merge top-K → hydrate tweet objects from cache/DB

5. Inverted index refresher

tweet1: "cats are great" tweet2: "cats and dogs" cats → (t1, t2) great → (t1) dogs → (t2) AND query: intersect postings; OR: union

6. Freshness strategy

7. Ranking

latest: sort by ts DESC after text match top: BM25 / learned ranker + engagement + recency decay

Retrieve a candidate set (e.g. few thousand) then re-rank; do not score the entire corpus deeply.

8. Sharding

9. Scaling & ops

10. Edge cases

11. Query planning examples

q = "cats filter:media since:2024-01-01" 1) tokenize "cats" 2) constrain postings to docs with media flag 3) time bound → only recent shards 4) retrieve → re-rank → hydrate q = "from:alice dogs" use author index / filter early to cut postings

12. Abuse and quality

Quick revision

  • Async index from tweet stream; do not block write path.
  • Inverted index + realtime and batch segments for freshness.
  • Query fan-in across shards; merge top-K; hydrate tweets.
  • Latest vs Top ranking modes.
  • Candidate retrieve then re-rank.
  • Tombstones for deletes; lag monitoring on indexers.
  • Time sharding helps bound “recent search.”