Explore / Backend system design

Design a Social Media Timeline (Twitter / X)

Overview

Design the core of Twitter (now X): users post short messages, follow other users, and open a home timeline showing recent posts from the accounts they follow, newest or best first. The same question appears as "design the Facebook news feed" or "design the Instagram feed", and it is one of the most frequently asked system design questions at large social companies. This is the backend version; the frontend feed has its own question.

The whole interview turns on one decision: do you build each user's timeline when a post is written, or when the timeline is read? Reads outnumber writes by roughly 25 to 1 with our numbers (Twitter reported about 50 to 1 in 2013), which argues for doing the work at write time. But one post from an account with 100 million followers would then mean 100 million writes. A strong answer explains both approaches with numbers, then designs the hybrid that real systems use. Write your answer in the Write tab and sketch the architecture in the Diagram tab.

Requirements

Functional

  • Post a message (text up to a few hundred characters, optional media).
  • Follow and unfollow accounts.
  • Home timeline: recent posts from followed accounts, paginated with infinite scroll.
  • User timeline: one account's own posts.
  • Like and repost, with counts shown on each post.
  • Optional: ranked "For you" timeline with recommended posts.

Non-functional

  • Fast reads: the home timeline loads in under about 200 ms at p99.
  • Fresh enough: a new post appears in followers' timelines within a few seconds; eventual consistency is acceptable.
  • Highly available: reading the timeline matters more than perfect freshness.
  • Scale: hundreds of millions of daily users, hundreds of millions of posts a day, accounts with over 100 million followers.

Estimates

Assume 200 million daily active users, 400 million posts per day, each user opening the home timeline about 50 times a day, an average of 200 followers per account, and a home timeline cache of the 800 most recent post ids per active user. Work out:

  • Average post writes per second, and timeline reads per second.
  • Fan-out writes per second if every post is pushed to every follower's timeline.
  • What happens when an account with 100 million followers posts.
  • Memory to cache home timelines as lists of 8-byte ids.

Hints

  • Should the timeline be computed on every read, or stored ready-made?
  • What if a celebrity's post had to be copied into 100 million lists?
  • What exactly do you store in a cached timeline: whole posts, or ids?
  • How do you generate unique, roughly time-ordered post ids across hundreds of servers without a central counter?
  • Do inactive users who haven't opened the app in a year need a precomputed timeline?

Follow-ups

  • How do you remove a deleted post from millions of cached timelines?
  • How would you add a ranked "For you" feed with posts from accounts the user doesn't follow?
  • A user follows a new account. When do that account's old posts appear?
  • How would you build search over all posts?
  • A world-cup final produces 10× normal post volume for 10 minutes. What breaks first?

Write your answer step by step and compare it with the reference solution on GrindPrep.