Skip to main content
HS
HiSETSuccess
LeetCode Prep

System Design

Comprehensive interview guide: scalability, databases, distributed systems, cloud architecture, and full case-study walkthroughs — plus adaptive quizzes.

Case study

Design a News Feed (Twitter/X)

Deliver personalized home timelines to 300M DAU with hybrid fan-out and celebrity handling.

fan-outtimelinecache

Requirements

  • Functional: post tweets (text, media); home feed of followed users; like/retweet
  • Feed ordering: reverse chronological (MVP); optional ranked feed
  • Non-functional: feed load p99 < 500ms; post tweet < 300ms
  • Scale: 300M DAU; avg 200 follows; 2 posts/day per active user
  • Celebrity users with 50M+ followers require special handling

Back-of-envelope Estimation

MetricCalculationResult
Posts/day300M × 0.5 active × 2300M posts/day
Write QPS300M / 86,400~3.5K/sec avg; ~15K peak
Feed reads/day300M × 5 loads1.5B reads/day
Read QPS1.5B / 86,400~17K/sec avg; ~50K peak
Fan-out writes (naive)15K × 200 followers3M writes/sec — infeasible

Why hybrid fan-out

Pure fan-out on write fails for celebrities. Pure fan-out on read is slow for users following 2000 accounts. Hybrid is the industry standard.

API Design

EndpointDescription
POST /v1/tweets{ text, mediaIds[] } → tweetId
GET /v1/feed?cursor=&limit=20Paginated home timeline
GET /v1/users/{id}/tweetsProfile timeline
POST /v1/tweets/{id}/likeIdempotent like

Data Model

StoreSchemaAccess pattern
tweetstweet_id, user_id, text, media_urls, created_atWrite on post; read by id
followsfollower_id, followee_id, created_atGraph lookup
user_timelineuser_id → sorted set (tweet_id, ts)Redis; top 800 tweet IDs
tweet_contenttweet_id → blobCassandra; wide rows by tweet_id

High-level Design

Post & feed flow
Post ──► Tweet Service ──► Tweet Store
              └──► Fan-out Service ──► queue ──► workers update follower timelines (Redis)

Feed ──► Timeline Service ──► merge(cached timelines + celebrity tweets on read)
              └──► Tweet Service (hydrate tweet bodies)

Deep Dive: Hybrid Fan-out

User typeWrite pathRead path
Normal (< 10K followers)Push tweet_id to each follower's Redis timelineRead precomputed timeline
Celebrity (> 10K followers)Skip fan-out; store in celebrity tweet listMerge at read time
Inactive followerSkip fan-out if not logged in 30 daysRebuild on next login

Bottlenecks at Scale

  • Celebrity post triggers millions of fan-out jobs — threshold gate prevents queue flood
  • Timeline hydration (800 IDs → full tweets) — batch mget; CDN for media
  • Hot tweet (viral) — single row hotspot; replicate in cache
  • Feed ranking ML — async precompute scores; serve from feature store

Failure Modes & Monitoring

SLOTarget
Feed p99 latency< 500ms
Fan-out lag< 30 sec for 99th percentile follower
Post success99.95%

Monitor fan-out queue depth, celebrity merge latency, timeline cache memory per user. Alert if fan-out workers fall behind post rate for > 2 min.