System design case study C06 · Fan-out · Redis

Design a Social News Feed (Twitter/X-style)

Show each user a fresh, ranked timeline of posts from accounts they follow, in under 200 ms, even when one of those accounts has 100 million followers.

What you'll be able to explain
  • Compare fan-out-on-write and fan-out-on-read
  • Use a hybrid model for celebrity accounts
  • Store timelines efficiently in memory
  • Add ranking without slowing the read path
  • Paginate a feed stably

Scope

Functional

  • Post short text (+ media). Follow/unfollow.
  • Home timeline: posts from followed accounts, ranked, paginated.
  • User timeline: one user's own posts.

Non-functional

  • Home timeline p99 < 200 ms. New posts visible to followers within ~5 s.
  • 300M DAU, read-heavy.

Size

  • Posts: 300M × 0.5/day → 150M/day ≈ 1,700 posts/s (peak ~5k).
  • Timeline reads: 300M × 20/day → 6B/day ≈ 70k/s (peak ~200k).
  • Average 200 followers per poster → fan-out of 1,700 × 200 = 340k timeline inserts/s.
  • Some accounts have 50M+ followers: one post would mean 50M inserts.

APIs and data

POST /v1/posts                       {text, media_ids}
GET  /v1/timeline/home?cursor=&limit=20
GET  /v1/users/{id}/posts?cursor=
POST /v1/users/{id}/follow
  • Posts (sharded by post_id, Snowflake IDs give time order).
  • Follow graph: followers(user_id) and following(user_id) tables, sharded by user.
  • Timeline cache: Redis list/sorted set per user of the latest ~800 post IDs.

Fan-out models

Fan-out on write (push) Fan-out on read (pull)
On post Insert post ID into every follower's timeline Just store the post
On read Read one precomputed list: fast Merge recent posts of everyone you follow: slow
Cost Huge for celebrities; wasted on inactive users Expensive reads for users following thousands

Choice: hybrid. Push for normal accounts; pull for celebrities (e.g. > 1M followers). At read time, merge the precomputed timeline with recent posts from the few celebrities the user follows.

CLIENTAuthorSERVICEPost serviceDATABASEPosts storeQUEUE / LOGKafkaPostCreatedWORKERFan-out workersDATABASEFollow graphCACHETimeline cacheRedis, per userCLIENTReaderSERVICETimeline serviceCACHECelebrity postsrecent, cachednormal authors onlyfollowerspush post_idprecomputedmerge celebs
Hybrid fan-out: normal posts are pushed to timelines; celebrity posts are pulled at read time.

The 30-second summary

Say it in the interview

"Hybrid fan-out: posts from normal accounts are pushed asynchronously into per-user Redis timelines of post IDs; celebrity posts are pulled and merged at read time. Reads fetch IDs, hydrate from a post cache, and optionally rank the top few hundred candidates within a strict latency budget. Inactive users' timelines are rebuilt on demand."

Follow-up questions the interviewer may ask

  1. How do you handle a celebrity with 50M followers posting?
  2. How do you rank instead of showing reverse-chronological?
  3. A user unfollows someone. Must their posts vanish immediately?
  4. How do you paginate a feed that changes while scrolling?

Model answers to each of these are in the book.