Design a News Feed (Twitter / Instagram / Facebook)
Post creation and home timelines for hundreds of millions of users — fan-out on write vs fan-out on read, the hybrid approach for celebrities, timeline caches in Redis, media handling, ranking pipelines, pagination with cursors, counters for likes, and the hot-key problems of viral content.
1. Requirements
Functional
- Post text + media; follow users.
- Home timeline: posts from followees (ranked or chronological), infinite scroll.
- Like, comment, share; counts shown.
- User timeline (a user's own posts).
Non-functional
- 300M DAU; timeline load p99 < 300 ms.
- Highly available; eventual consistency OK (a post may appear a few seconds late).
- Read-heavy: ~100 timeline reads per post written.
2. Estimation
| Quantity | Assumption | Result |
|---|---|---|
| Posts | 300M DAU × 1 post/day avg | ~3.5k/s (peak ~15k/s) |
| Timeline reads | 300M × 20 refreshes/day | ~70k/s (peak ~300k/s) |
| Avg followers | 200 | fan-out ~700k timeline inserts/s |
| Post metadata | 1 KB | ~300 GB/day |
| Media | 20% posts with 1 MB image | ~60 TB/day → object storage + CDN |
| Timeline cache | 300M × 800 IDs × 16 B | ~4 TB RAM across a Redis fleet (active users only → less) |
3. API
POST /v1/posts {"text": "...", "media_ids": ["m_1"]}
GET /v1/feed?cursor=<opaque>&limit=20 → {"items": [...], "next_cursor": "..."}
POST /v1/users/{id}/follow
POST /v1/posts/{id}/likes (idempotent per user)
GET /v1/users/{id}/posts?cursor=...
4. Data model
| Data | Store | Key |
|---|---|---|
| Users, profiles | sharded SQL / KV | user_id |
| Social graph (follows) | sharded KV/graph store (both directions) | follower → followees, followee → followers |
| Posts | Cassandra / sharded SQL | post_id (Snowflake, time-sortable); user posts by (author_id, post_id) |
| Timelines | Redis lists/sorted sets | timeline:{user_id} |
| Media | object storage + CDN | media_id |
| Likes | KV (post_id, user_id) + counters |
5. High-level design

Post flow: upload media first (pre-signed URL) → POST /posts writes the post (with media IDs) to the posts DB → publishes PostCreated → fan-out workers page through the author's followers (graph service) in batches and LPUSH+LTRIM the post ID into each active follower's timeline. Celebrity authors (e.g., > 1M followers) are flagged; their posts are not fanned out.
Read flow: feed service reads timeline:{user} page → fetches recent posts of followed celebrities (cached per celebrity) → merges → ranking → hydrates posts, authors, counts and "liked by me" via multi-get from caches → returns with a cursor.
6. Deep dives
Fan-out cost control
CELEBRITY_THRESHOLD = 1_000_000
def on_post_created(post):
author = users.get(post.author_id)
if author.follower_count >= CELEBRITY_THRESHOLD:
celebrity_posts.add(author.id, post.id) # pulled at read time
return
for batch in graph.followers(author.id, batch_size=5_000):
active = [f for f in batch if activity.is_active(f, days=30)]
with redis.pipeline() as p:
for follower in active:
key = f"timeline:{follower}"
p.lpush(key, post.id)
p.ltrim(key, 0, 799)
p.execute()
Celebrity / hot content
- Celebrity posts cached once and fanned in at read time.
- Viral post objects replicated in multiple cache nodes (hot-key replication) or served from a local in-process cache with a short TTL.
- Counters aggregated asynchronously (see questions).
Media
Uploaded directly to object storage, processed asynchronously (resize into multiple renditions, strip EXIF, content moderation), served via CDN with immutable URLs.
Deletes, edits and privacy
Deleting a post removes the post object (hydration then drops dangling IDs) — no need to remove IDs from millions of timelines. Blocks/mutes are filtered at read time. Private accounts: fan-out only to approved followers.
7. Trade-offs and bottlenecks
| Concern | Approach |
|---|---|
| Write amplification | hybrid fan-out, skip inactive users |
| Timeline cache loss | rebuild on demand from graph + posts (pull path) |
| Consistency | eventual; author sees own post immediately (insert into own timeline synchronously) |
| Ranking latency | precompute candidate features; two-stage ranking; cache ranked page for the session |
| Hot shards | shard timelines by user ID; replicate hot post objects |
Interview questions
Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.