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
Core Concepts
Push Model (Fan-out on Write)
javascript
QUBITS OF DPK
Pull Model (Fan-out on Read)
javascript
QUBITS OF DPK
Hybrid Model (Twitter's Actual Solution)
javascript
QUBITS OF DPK
Timeline Cache Architecture
javascript
QUBITS OF DPK
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.