Blog 11 — Twitter: Timelines at Scale

C

Qubits of DPK

March 21, 2026

Core Case Studies
Core Concept: Fanout problem, Push model vs Pull model vs Hybrid model, Feed generation at scale
Why SDE-2 Critical: Most commonly asked system design question in MAANG — interviewers always go deep on fanout
Status: Draft notes ready

Quick Revision

  • Problem: Feeds are expensive either on write or on read.
  • Core pattern: Hybrid fanout: push for normal users, pull for celebrities.
  • Interview one-liner: Timeline design is really a trade between write amplification and read amplification.

️ The Core Problem: Fanout

javascript
QUBITS OF DPK
1Elon Musk posts a tweet (100M followers)
2
3Push model (fan-out on write):
4  For each of 100M followers → insert tweet into their feed cache
5  100M writes per tweet → system melts 💥
6
7Pull model (fan-out on read):
8  When Alice opens timeline:
9    For each person Alice follows:
10      SELECT latest tweets
11    Merge and sort
12  If Alice follows 500 people → 500 DB queries on timeline open 💥
13
14Hybrid model (what Twitter actually uses):
15  Regular users → Push (pre-compute feed)
16  Celebrities (>X followers)Pull (compute at read time)

Core Concepts

Push Model (Fan-out on Write)

javascript
QUBITS OF DPK
1User A tweets → insert tweet_id into feed cache of ALL followers
2
3Pros:
4Timeline read is instant (pre-computed)
5Simple read path
6
7Cons:
8Celebrity with 100M followers = 100M writes per tweet
9Wasted work for inactive followers
10Storage cost for every follower's cache

Pull Model (Fan-out on Read)

javascript
QUBITS OF DPK
1User opens timeline → fetch tweets from everyone they follow
2
3Pros:
4No write amplification
5Always fresh, no stale data
6
7Cons:
8High read latency (merge N feeds at request time)
9DB hammered on every timeline open
10Hard to rank and personalize

Hybrid Model (Twitter's Actual Solution)

javascript
QUBITS OF DPK
1Classify users:
2  Regular user (< threshold followers)PUSH
3    Tweet inserted into follower's feed cache immediately
4
5  Celebrity (> threshold followers)PULL
6    Tweet NOT pre-fanned out
7    At read time: merge pre-computed feed + fetch celebrity tweets
8
9Timeline assembly:
10  Alice opens timeline
11  Step 1: Read pre-computed feed from Redis (fast, O(1))
12  Step 2: Fetch tweets from celebrities Alice follows (small list)
13  Step 3: Merge + rank by recency/relevance
14  Step 4: Return top N tweets

Timeline Cache Architecture

javascript
QUBITS OF DPK
1Redis stores per-user feed:
2  Key: timeline:user_alice
3  Value: sorted set of [tweet_id, timestamp]
4  Size: last 800 tweet IDs
5
6When Bob (regular user) tweets:
7  Fanout service reads Bob's follower list (sharded DB)
8  For each follower → ZADD timeline:follower tweet_id timestamp
9  TTL on inactive users → cache cleared if user inactive 30 days

Scale Achieved

5 Interview Questions This Blog Unlocks

Q1. Design Twitter's timeline / Instagram feed

Answer: Hybrid fanout. Regular users → push tweet IDs to followers' Redis sorted sets on write. Celebrities → pull their tweets at read time and merge. Timeline read: fetch pre-computed feed from Redis + merge celebrity tweets + rank. Cache last 800 tweets per user.

Q2. What is the fanout problem and why is it hard?

Answer: When a user with millions of followers posts, each follower's feed needs updating. Pure push = millions of writes per post. Pure pull = millions of reads per timeline load. Neither scales alone. The fanout problem is fundamentally about write amplification vs read amplification — you must choose or hybrid.

Q3. Why is Redis sorted set a good data structure for timeline storage?

Answer: Sorted set stores tweet IDs ordered by timestamp score. ZADD is O(log N). ZRANGE (fetch top N tweets) is O(log N + N). Automatic deduplication. Natural ranking by timestamp. Memory-efficient (only stores IDs, not tweet content). Perfect for feed use case.

Q4. What happens when a celebrity with 100M followers tweets on Twitter?

Answer: Tweet is NOT fanned out to 100M feeds immediately. Instead, tweet stored in celebrity's own tweet store. When any of their followers opens timeline, celebrity's latest tweets fetched in real-time and merged with pre-computed feed. Threshold typically around 1M followers.

Q5. How do you handle inactive users in the fanout system?

Answer: Don't fanout to users who haven't been active in N days. Cache entry TTL of 30 days — inactive user's feed evicted. When inactive user returns, regenerate feed by pulling from followees. This optimization alone reduces fanout writes by 60%+ since most accounts are inactive.

Key Engineering Lessons