News Feed System Design
A Twitter / Instagram-style home timeline
By Ankur NayakReviewed
Show each user a fresh, ranked feed of posts from the accounts they follow, in a few hundred milliseconds. The core decision is fan-out: precompute timelines when someone posts, or assemble them when someone reads, and how to handle celebrities.
Open in Studio· edit a copy- Users
- 300M daily active users
- Throughput
- ~300K feed reads/s
- Storage
- Petabytes incl. media
- Posts per day
- ~500M
- Median followers
- ~200
- Feed latency target
- < 200 ms p99
Requirements
Functional
- Publish a post with text and media
- Home feed of posts from followed accounts, newest or ranked
- Follow and unfollow accounts
- Infinite scroll with pagination
Non-functional
- Feed loads in under 200 ms at p99
- New posts appear in followers' feeds within seconds (eventual consistency is fine)
- Highly available; reads must survive partial failures
- Handle accounts with tens of millions of followers
In depth
The one decision that matters
A news feed looks like a simple query: "recent posts by everyone I follow, newest first." At small scale it is exactly that. At hundreds of millions of users, running that query on every app open means joining against hundreds of followed accounts per read, thousands of times a second. The whole design is about when that work happens: when someone posts, or when someone reads. This is the fan-out trade-off.
Push: pay at write time
With fan-out on write, a post triggers a background job that copies the post's ID into a cached timeline for every follower. Reading a feed becomes one lookup of a short list of IDs. Twitter engineers described this model at QCon in 2013: timelines held in Redis, filled by fan-out as tweets arrive. Since reads vastly outnumber writes, moving the cost to write time is a good deal.
The celebrity problem
Push breaks for accounts with tens of millions of followers: one post becomes tens of millions of cache writes, and followers see it late. The standard fix is a hybrid. Ordinary accounts are pushed. Very large accounts are skipped at write time, and when a user opens their feed, the service pulls recent posts from the few big accounts they follow and merges them with the precomputed list.
Keep timelines small
Timelines store only post IDs, capped at a few hundred per user, and only for users who have been active recently. The feed service then hydrates a page of IDs into full posts with one batched lookup against post and user caches. Because posts aren't copied, edits, deletes and like counts are always current.
Ranking
Modern feeds rank candidates by predicted interest rather than strict recency. Ranking sits after the merge step: the timeline provides candidates, a model orders them, and hydration fills in the details. A chronological mode simply skips the ranker.
What interviewers look for
- Push vs. pull vs. hybrid, with numbers
- The celebrity problem and a threshold
- What lives in the cache (IDs) and how hydration works
- Asynchronous fan-out through a queue
- Cursor-based pagination and eventual consistency
Components
Mobile / Web App
Client. Posts content and scrolls the home feed.
API Gateway
API gateway. Authenticates users, rate-limits writes and routes to the post and feed services.
Building block: API Gateway, Load Balancer
Post Service
Service. Validates and stores new posts, then enqueues fan-out.
Building block: Unique ID Generation
Post Store
Sharded key-valueNoSQL database. Every post by ID, sharded by post ID; a per-author index lists each user's recent posts.
Building block: Database Sharding, Replication
Social Graph
Graph storeGraph database. Who follows whom, queried in both directions; heavily cached.
Why: Follower lists are read on every fan-out; a graph store with a cache layer (like Facebook's TAO) serves them cheaply.
Fan-out Queue
KafkaQueue. Decouples posting from timeline updates so authors get an instant response.
Building block: Message Queue & Event Log
Fan-out Workers
Worker. Page through the author's followers and push the post ID into each follower's timeline.
Why: Doing the work at write time makes the far more frequent reads a single cache lookup.
Building block: Fan-out on Write vs. Read
Timeline Cache
Redis listsCache. For each active user, the latest ~800 post IDs from accounts they follow.
Building block: Cache, Consistent Hashing
Feed Service
Service. Builds a page of the feed: reads the precomputed timeline, merges celebrity posts, ranks and hydrates.
Building block: Fan-out on Write vs. Read
Read Timeline
Service. Fetches the user's precomputed list of post IDs.
Pull Celebrity Posts
Service. Fetches recent posts from the few huge accounts the user follows (no fan-out for them).
Merge & Dedupe
Service. Combines both lists by ID/time and removes duplicates and blocked content.
Ranker
ML model. Orders candidates by predicted interest (optional: chronological mode skips it).
Hydrate
Post & user cachesCache. Turns IDs into full posts with author names, counts and media URLs.
Media CDN
CDN. Images and video are uploaded to object storage and served from the edge.
Building block: CDN, Object Storage
Ranking Model
inferredML model. Scores candidate posts using engagement signals; trained offline.
Notifications
Service. Mentions and replies trigger notifications through a separate pipeline.
Request flows
A user publishes a post
- The author submits a post; media was already uploaded to object storage.
- The post service validates it and assigns a time-ordered ID.
- The post is stored durably.
- A fan-out job is queued and the author gets an instant success response.
- A fan-out worker picks up the job.
- It pages through the author's followers (skipped for celebrity accounts).
- It prepends the post ID to each active follower's cached timeline.
A user opens their feed
- The app requests the first page of the home feed.
- The feed service handles it.
- One read returns the user's precomputed list of post IDs.
- It merges in recent posts from followed celebrities and hydrates IDs into full posts.
- The ranked page returns; images load from the CDN.
Failure scenarios
What if: timeline cache is lost?
Feeds fall back to fan-out on read: the feed service fetches recent posts from every followed account and merges them. It works but is slow and loads the post store heavily until timelines are rebuilt.
User impact: Slower feeds and possibly fewer posts per page for a while.
- Replicate timeline cache shards
- Rebuild timelines lazily on first read
- Rate-limit expensive fallback reads
What if: fan-out workers fall behind?
Posts are safely stored and queued, but they reach followers' timelines late. Nothing is lost; the queue drains once workers catch up.
User impact: New posts appear in feeds minutes late.
- Autoscale workers on queue lag
- Prioritise fan-out to recently active users
Trade-offs
Fan-out on write vs. on read
Hybrid: push for ordinary accounts, pull for accounts above a follower threshold.
- Pros
- Most feeds are a single cache read
- Write amplification stays bounded
- Cons
- Two code paths plus a merge step
- Timelines are eventually consistent
Alternatives: Pure pull (simple, slow reads); Pure push (fast reads, celebrity problem)
Store IDs vs. full posts in timelines
Store only post IDs and hydrate from post and user caches.
- Pros
- Edits and deletes apply everywhere instantly
- Small timelines fit in memory
- Cons
- An extra batched lookup per page
Chronological vs. ranked
Ranked by default, with a chronological option.
- Pros
- Higher engagement
- Old but relevant posts surface
- Cons
- Needs an ML pipeline and feature store
- Less predictable for users
Frequently asked questions
What is fan-out on write in a news feed?
When a user posts, a background job inserts the post's ID into a precomputed timeline for each of their followers. Reading a feed is then a fast lookup of one list, at the cost of many writes for each post.
How do news feeds handle celebrities with millions of followers?
They don't fan out those posts. Each follower's precomputed timeline covers ordinary accounts, and at read time the feed service fetches recent posts from the handful of very large accounts the user follows and merges them in.
Why store post IDs instead of full posts in the timeline cache?
IDs are small, so timelines for hundreds of millions of users fit in memory, and edits, deletes and like counts are read fresh from the post cache instead of being copied into millions of timelines.
How is the feed paginated?
With a cursor: the client sends the ID of the last post it saw, and the service returns the next posts older than that ID. Offsets break when new posts arrive at the top while the user is scrolling.
Building blocks used
Sources
- System Design Primer: Design the Twitter timeline and search · GitHub
- Timelines at Scale · InfoQ (QCon)
- TAO: Facebook's Distributed Data Store for the Social Graph · USENIX ATC