Post Search (Facebook / Twitter Search)
Requirements: search posts by keywords, sorted by recency or popularity (likes); new posts searchable within ~1 minute; billions of posts, high query volume, posts get liked constantly.
write: posts -> Kafka -> indexing workers: tokenize -> append post_id to each term's posting list
(sharded by term; one list ordered by time, one by like bucket)
likes: like events -> counter svc -> re-index a post only when its count crosses a bucket (1, 10, 100, 1k …)
query: "taylor swift" -> fetch both terms' lists -> intersect -> top N -> fetch exact like counts -> re-rank
popular queries cached (TTL ~ 1 min)
Deep dives
- Inverted index (term → post ids) partitioned by term; keep postings sorted by time so "most recent" is a prefix read; very hot terms are cached and replicated.
- Like-count churn: re-indexing on every like would be a write storm — index coarse popularity buckets and fetch exact counts only for the top candidates at query time.
- Freshness vs cost: an in-memory real-time index for recent posts plus a larger on-disk index for older ones; queries merge both.
- Scale: billions of posts × ~10 terms each — compress posting lists (delta + varint encoding); move cold index segments to cheaper storage.
- Privacy filtering: the result set must respect visibility (friends-only posts) → filter at query time against the viewer's graph, or index visibility attributes.