{}DSA Atlas

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.

Advanced5 interview questions

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

QuantityAssumptionResult
Posts300M DAU × 1 post/day avg~3.5k/s (peak ~15k/s)
Timeline reads300M × 20 refreshes/day~70k/s (peak ~300k/s)
Avg followers200fan-out ~700k timeline inserts/s
Post metadata1 KB~300 GB/day
Media20% posts with 1 MB image~60 TB/day → object storage + CDN
Timeline cache300M × 800 IDs × 16 B~4 TB RAM across a Redis fleet (active users only → less)

3. API

HTTP
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

DataStoreKey
Users, profilessharded SQL / KVuser_id
Social graph (follows)sharded KV/graph store (both directions)follower → followees, followee → followers
PostsCassandra / sharded SQLpost_id (Snowflake, time-sortable); user posts by (author_id, post_id)
TimelinesRedis lists/sorted setstimeline:{user_id}
Mediaobject storage + CDNmedia_id
LikesKV (post_id, user_id) + counters

5. High-level design

Twitter timeline architecture with fan-out service, timeline cache, user graph service, tweet info service, search, notification and object store
A reference Twitter design with fan-out to timeline caches. Diagram from The System Design Primer by Donne Martin, licensed CC BY 4.0.
write path author post service posts DB Kafka fan-out workersskip celebs/inactive graph service timeline cache(Redis, IDs only) read path reader feed service celebrity posts(pull + merge) ranking service post + user cache(hydrate)
Precompute timelines for most users; merge celebrity posts at read time.

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

Python
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

ConcernApproach
Write amplificationhybrid fan-out, skip inactive users
Timeline cache lossrebuild on demand from graph + posts (pull path)
Consistencyeventual; author sees own post immediately (insert into own timeline synchronously)
Ranking latencyprecompute candidate features; two-stage ranking; cache ranked page for the session
Hot shardsshard timelines by user ID; replicate hot post objects

Interview questions

0 / 5 marked confident

Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.