← Back to System Design, page 3

system-design-0057 · Medium mid

Design the home timeline for a Twitter or X style service with 250 million daily users and 500 million posts per day, including accounts with tens of millions of followers.

Precompute each active user's home timeline as a list of post IDs in an in-memory store by fanning out new posts to followers' lists, but skip fan-out for accounts with very many followers and merge their recent posts at read time. Reads (about 87,000 per second) then cost one list read plus a hydration call, while fan-out writes reach about 1.16 million per second on average. The timeline cache holds IDs only, about 1.6 TB for 800 IDs per user.

Full answer

Requirements

Functional:

  • Post short messages; follow accounts.
  • The home timeline shows recent posts from followed accounts, newest first (ranking can be added later).
  • A user timeline shows one account's own posts.

Non-functional:

  • Home timeline loads in under about 200 ms at p99.
  • A new post appears in followers' timelines within a few seconds.
  • Availability over strict consistency: a post showing up a few seconds late is acceptable.

Capacity estimates

Assumptions: 250M daily users, 500M posts per day, 200 followers on average, 30 timeline loads per user per day, 800 post IDs cached per timeline at 8 bytes each.

  • Posts: 500M / 86,400 = about 5,800 per second.
  • Fan-out writes: 5,800 x 200 = about 1.16 million timeline inserts per second.
  • Timeline reads: 250M x 30 / 86,400 = about 87,000 per second.
  • Timeline cache: 250M x 800 x 8 bytes = 1.6 TB, spread over a Redis cluster (with replicas, about 3.2 TB of memory).

The average hides the real problem: one post from an account with 50M followers would be 50M inserts on its own.

API

POST /v1/posts {"text": "...", "media_ids": [...]} GET /v1/timelines/home?cursor=.. -> [{"post_id", "author", "text", "counts"}...], next_cursor GET /v1/users/{id}/posts?cursor=.. POST /v1/users/{id}/follow

The cursor is the last post ID seen; post IDs are time-ordered (Snowflake style), so "older than this ID" works without offsets.

Data model

  • posts: post_id, author_id, text, media, created_at. Stored in a sharded store keyed by post_id; a per-author index for user timelines.
  • follows: follower_id to followee_id and the reverse, in a graph or wide-column store.
  • home_timeline:{user_id}: Redis list of post IDs, trimmed to 800.
  • celebrity flag on accounts above a follower threshold (for example 1M).

High-level design

mermaid
flowchart LR
  U[Client] --> W[Post service]
  W --> PS[(Posts store)]
  W -- "new post" --> Q[(Queue)]
  Q --> FO[Fan-out workers]
  FO --> G[(Follow graph)]
  FO --> TL[(Timeline cache)]
  U --> R[Timeline service]
  R --> TL
  R --> CEL[Celebrity recent posts]
  R --> H[Hydration: posts and users cache]

Fan-out and the celebrity problem

Normal accounts: on a new post, fan-out workers read the follower list in pages and push the post ID onto each active follower's list, trimming it to 800 entries.

Celebrities: no fan-out. At read time, the timeline service fetches the user's precomputed list, plus the latest posts from the handful of celebrities they follow (each celebrity's recent posts are cached and read by millions), merges by post ID and returns the top page.

Inactive users (not seen in 30 days) get no fan-out; their timeline is rebuilt from the follow graph when they return.

Bottlenecks and trade-offs

  • Fan-out on write versus read. Write fan-out makes reads cheap but costs huge write volume; read fan-out is cheap to write but needs a scatter-gather over many authors on each read. The hybrid uses each where it is cheap.
  • Threshold choice. Too low a celebrity threshold makes reads slow; too high makes fan-out spikes. Tune it from measurements.
  • Deletions must remove IDs from many lists; instead, filter deleted posts during hydration.
  • Cache loss. If a Redis shard is lost, timelines are rebuilt on demand from posts and the follow graph, so the cache is not the source of truth.
  • Hydration batches post and user lookups so one timeline page costs a few cache calls, not 20.

By level

Fresher: Explain fan-out on write in simple terms and why a timeline cache makes reads fast. A fresher should notice that very popular accounts cause a problem.

Mid: Do the fan-out math, design the hybrid with a threshold, and use ID-based cursors. Explain hydration and trimming.

Senior: Discuss threshold tuning, inactive users, deletions, cache rebuild, ranking and the cost model that drives each choice. Expect a clear comparison with fan-out on read.

Key points

  • Precompute home timelines as lists of post IDs
  • About 1.16 million fan-out inserts per second on average
  • Skip fan-out for celebrities and merge at read time
  • Skip inactive users and rebuild their timeline on return
  • Store IDs only and hydrate from caches

Common mistakes

  • Pure fan-out on write including celebrities
  • Pure fan-out on read for every request
  • Storing full post bodies in every timeline
  • Offset pagination on a changing feed

Follow-ups

  • How would you add ranking to the timeline?
  • How do you handle a user who follows 5,000 accounts?
  • How would you show a "new posts" banner?

Tips

  • Show the averages, then the celebrity spike
  • Explain the hybrid clearly
  • Use ID cursors for pagination

Evaluation rubric

  • Correct capacity math
  • Fan-out design with clear trade-offs
  • Celebrity handling and inactive users
  • Cache rebuild and hydration discussed