Twitter / X

Social media feed

Design a service where people post short messages, follow other people, and read a home timeline of posts from the accounts they follow.

Overview

In this service, people post short messages. Each person follows some accounts, and their home timeline shows recent posts from those accounts, newest or most relevant first. That is the whole product for this interview, and it hides one hard question: when someone posts, who does the work of getting that post in front of their followers, and when?

The numbers make it hard. Reads dwarf writes, so reads must be cheap. But follower counts are extremely uneven: most accounts have a few hundred followers, and a handful have tens of millions. A design that is cheap for the typical account can be a disaster for the largest ones, and the interviewer will steer you there.

Questions to ask first

  • How many users and posts? — 200 million daily active users; about 500 million posts a day
  • How often do people read? — About 5 billion home-timeline builds a day
  • Chronological or ranked timeline? — Start chronological; ranking is a later stage on top
  • How fresh must the timeline be? — A new post may take a few seconds to reach followers; the author sees it at once
  • How uneven are follower counts? — Average 200 per post, a long tail up to 100 million
  • Media? — Stored in blob storage behind a CDN; out of scope here

What you will be able to do

  • Explain fan-out on write and fan-out on read, and what each costs, with numbers.
  • Handle accounts with huge follower counts with a hybrid timeline.
  • Design time-ordered IDs, the post store and the timeline cache.
  • Walk through posting and reading, including what happens during a traffic spike.
  • Describe how a ranked timeline is built on top of the same data.

Key concepts

  • Fan-out on write
  • Fan-out on read
  • Hybrid timelines
  • Celebrity accounts
  • Snowflake IDs
  • Timeline cache
  • Hydration
  • Social graph
  • Candidate ranking

Functional requirements

  • Post a message of up to 280 characters, optionally as a reply
  • Follow and unfollow accounts
  • Read a home timeline of posts from followed accounts, newest first, in pages
  • Read any account's own posts (profile timeline)
  • Like and repost; show counts on each post

Non-functional requirements

  • Home timeline pages load in under 200 ms at p99 (a target we chose)
  • Reading stays available during failures; a post may reach followers seconds late
  • An author sees their own new post immediately (read your own writes)
  • Absorb posting spikes of many times the daily average without dropping posts
  • Posts are durable once acknowledged

Interview questions

  • Push or pull: which would you use for the home timeline?
  • How do you handle accounts with tens of millions of followers?
  • How do you generate post IDs?
  • What happens, step by step, when someone posts?
  • How is a timeline page read and paginated?
  • What consistency does the timeline need?
  • How do you count likes on a post that goes viral?
  • How does a ranked timeline change the design?

Sign up free to read the full topic

More System Design topics

Start free on Cariara · Coding problems · System design studio