Catalog My learning Blog
System Design Roadmap: 100-Day Course for Engineers · Phase 10 · Lesson 96 · 5 min read

System Design: Design Twitter News Feed

On this page
  1. The problem: design a Twitter-like news feed
  2. News feed requirements and estimates
  3. Data model: users, follows and posts
  4. The home timeline: fan-out on write vs fan-out on read
  5. Likes, search, notifications and deletes
  6. Reliability and fan-out lag
  7. What a strong news feed design includes
  8. Check yourself

The problem: design a Twitter-like news feed

Users post short messages, follow others, and see a home timeline of recent posts from people they follow. Posts can include images. Also support likes, a user's own profile timeline, search and notifications. 50 million daily active users. Design it.

News feed requirements and estimates

Non-functional: the home timeline loads in under 300 ms; a new post appears in followers' timelines within seconds; reads must stay up even if posting degrades.

Code
Posts          20M per day          ≈ 230/s average, ~700/s peak
Timeline reads 50M × 20 loads/day   ≈ 11,500/s average, ~35,000/s peak
Text storage   20M × 1 KB           ≈ 20 GB/day; media far larger, in object storage

Reads outnumber writes about 50 to 1. Optimize the read path.

But count the work behind each write too. With fan-out on write, one post becomes one timeline insert per follower:

Code
Fan-out writes   700 posts/s at peak × 200 followers on average  ≈ 140,000 timeline inserts/s
Timeline memory  active users × 800 ids × ~60 bytes              ≈ 48 KB per user
                 20M active users                                ≈ 1 TB of Redis

So the "cheap" write side is actually the heaviest write load in the system, and timelines need a large Redis cluster. Both numbers are why the design below caps timeline length and skips inactive users.

Data model: users, follows and posts

  • Users and follows (follower_id, followee_id), with indexes for both directions: "who do I follow" and "who follows me".
  • Posts: id, author, text, media URLs, created time. Ids are generated as time-sortable 64-bit values (Twitter's Snowflake scheme combines a timestamp, a worker id and a sequence), so sorting by id sorts by time without a central counter.
  • Posts partitioned by author id, since most queries are "posts by these authors, newest first".
  • Media in object storage behind a CDN (Day 35).

The home timeline: fan-out on write vs fan-out on read

Fan-out on write for normal accounts: a new post is published as an event; fan-out workers insert its id into each follower's timeline list, a Redis sorted set capped at about 800 ids. Reading a timeline is one range read plus a batched lookup of post contents from a post cache. This is close to what Twitter described running in 2012, including the cap of about 800 entries per timeline (Krikorian, QCon 2012).

Fan-out on read for accounts above a follower threshold: their posts aren't pushed. When a user loads the timeline, the service merges recent posts from the few big accounts they follow into the precomputed list. This hybrid keeps one celebrity post from triggering tens of millions of writes.

Inactive users get no fan-out; their timeline is rebuilt when they return.

A Twitter-like feed: posts stored once, fanned out by workers into per-user timeline caches, with celebrity posts merged at read time.
A Twitter-like feed: posts stored once, fanned out by workers into per-user timeline caches, with celebrity posts merged at read time.

Likes, search, notifications and deletes

  • Likes and counts: increment in Redis, flush to the database in batches (Day 32). Exact counts can lag a few seconds.
  • Profile timeline: a range query on the author's partition, cached.
  • Search: posts streamed through Kafka into a search index (Elasticsearch or OpenSearch), searchable within seconds.
  • Notifications: consumers of like, follow and reply events, delivering push notifications through a queue with rate limits per user.
  • Edits and deletes: update the post record and its cache; timelines keep ids and filter deleted posts at read time (Day 40).

Reliability and fan-out lag

  • Timeline reads are served from caches and replicas and lean AP; a missing post for a few seconds is acceptable.
  • The posting path writes the post durably first, then fans out asynchronously; a fan-out backlog delays timelines, it doesn't lose posts.
  • Hot posts get stampede protection and local caching (Day 39).
  • Watch fan-out lag, the time from a post being stored to it appearing in followers' timelines. It's the SLI behind "within seconds", and it rises first when fan-out workers fall behind (Day 73).

What a strong news feed design includes

  • Read-heavy numbers driving a read-optimized design.
  • The fan-out choice and the hybrid for large accounts, stated before being asked.
  • Time-sortable ids and partitioning by author.
  • Asynchronous everything after the post is stored: fan-out, search, notifications, counts.
  • How deletes and edits work without rewriting millions of timelines.

Check yourself

Ids like Snowflake ids or UUIDv7 embed a timestamp in their high bits, so sorting posts by id also sorts them by creation time, which keeps timeline queries and pagination simple. They can also be generated on many servers independently, without a central counter becoming a bottleneck.

Fan-out workers and their queue. At about 140,000 timeline inserts per second, workers fall behind first; check queue age and scale workers or raise the celebrity threshold.

In the hybrid design, the celebrity's post is stored once and not fanned out to 40 million timelines, which would take too long and cost too much. When followers load their timelines, the service merges recent posts from the celebrities they follow into their precomputed timeline at read time.

The author gets a fast, durable confirmation, and fan-out delays or failures only slow timeline delivery without losing the post.