2dbi

Design Twitter

viaLeetCode

Problem Design a Twitter-like service: users post tweets, follow others, and read a home timeline of recent tweets from people they follow.

Functional requirements

  • Post tweet (text, media reference), follow/unfollow, home timeline (reverse-chronological or ranked), user timeline.

Non-functional requirements

  • Read-dominated: timeline reads ~100x tweet writes. Timeline p99 < 200 ms.
  • Scale to discuss: 200M DAU, ~5K tweets/sec average (spikes 10x), fan-out to millions of followers for celebrity accounts.
  • Availability over strict consistency — a slightly stale timeline is acceptable.

Key components

  • Tweet service + storage (write path), social-graph service (follows), timeline service with a fan-out strategy, Redis timeline caches (per-user list of tweet IDs), media store/CDN.

Deep dives / trade-offs

  • Fan-out on write (push tweet IDs into each follower's cached timeline; cheap reads, expensive for celebrities) vs fan-out on read (merge followees' recent tweets at request time; cheap writes, expensive reads) vs the hybrid: push for normal users, pull-and-merge for high-follower accounts.
  • Timeline cache sizing/eviction and rebuild for cold users.
  • Ordering and pagination (snowflake-style time-sortable IDs, cursor pagination).
Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account