Designing a Scalable Social Timeline & Hybrid Fan-Out Engine
A production-grade news feed must serve cached timelines in sub-100ms while ingesting tens of thousands of new posts every second. Learn how to solve the celebrity fan-out bottleneck using hybrid push-pull architectures, Redis sorted sets, and asynchronous queue dispatching.
Capacity Math: Read Heavy 100:1 Asymmetry
Timeline consumption outpaces timeline generation by orders of magnitude. The system must optimize ruthlessly for the read path while preventing fan-out write amplification from overwhelming worker message brokers.
Read & Write QPS Calculations
- 1. Read Throughput: 300M active users open their feed an average of 10 times daily = 3B total timeline fetches. Baseline average: 3B / 86,400s = 34,700 QPS. Peak load multiplier (10x during viral breaking news) = 350,000 QPS.
- 2. Write Throughput: 300M posts created daily = 3,470 write QPS average. Peak ingress reaches 35,000 QPS.
- 3. Fan-Out Amplification: Average user has 200 followers. Standard fan-out requires 3,470 * 200 = 694,000 queue writes/s. Celebrity users with 20M followers would require 20M writes for a single tweet, crashing standard worker queues.
Timeline Cache Memory Footprint
-
1.
Per-User Cache Entry: Rather than caching full post blobs, timelines store only 800 recent
post_ids (64-bit integer, 8 bytes) scored by epoch millisecond timestamp in a Redis Sorted Set (ZSET). - 2. Storage Per Timeline: 800 posts * (8 bytes ID + 8 bytes score + 24 bytes overhead) ≈ 32 KB per user timeline.
- 3. Active Working Set: 300M DAU * 20% high-frequency active cohort = 60M cached active timelines. 60M * 32 KB = 1.92 TB RAM. A modest 16-node Redis cluster with 128 GB RAM per node comfortably holds the hot timeline state.
Push (Fan-Out on Write) vs. Pull (Fan-In on Read)
Neither pure push nor pure pull scales across asymmetric follower distributions. The production solution is a hybrid architecture that dynamically splits user models based on follower thresholds.
| Strategy | Read Complexity | Write Complexity | Failure Mode | Production Suitability |
|---|---|---|---|---|
| Fan-Out on Write (Push) | O(1) Redis ZREVRANGE | O(N) where N = followers | Celebrity write stalls workers; million-follower users trigger backpressure. | Ordinary users only (< 25k followers) |
| Fan-In on Read (Pull) | O(M log M) multi-merge | O(1) single DB insert | High p99 latency; database read amplification degrades cluster under traffic spikes. | Celebrities only (> 25k followers) |
| Hybrid Push-Pull (Recommended) | O(1) + O(K) celebrity merge | O(N) capped at 25k | Requires aggregator proxy layer to merge timeline cache with celebrity feeds. | Standard for Twitter / Instagram / Threads |
The Celebrity Ingestion Algorithm
When an ordinary user (e.g. 300 followers) posts, async worker queues fan out the post ID directly into each follower's Redis timeline. When a celebrity user (> 25,000 followers) posts, the write fan-out is skipped entirely. Instead, the post is saved only to the celebrity's public post list. When a follower requests their feed, the Aggregator Service pulls their cached timeline and merges the recent posts of any celebrities they follow on the fly using a multi-way k-merge in memory.
1. Fetch base timeline: cached_ids = redis.zrevrange("timeline:" + user_id, 0, 100)
2. Identify followed celebrities: celebs = db.get_user_celebrities(user_id)
3. Fetch recent celebrity posts: celeb_ids = redis.mget(["posts:" + c for c in celebs])
4. In-memory k-way heap merge by timestamp: final_feed = merge_sorted(cached_ids, celeb_ids)[:20]
5. Hydrate post metadata from Redis MGET / PostgreSQL read replica
Queue Explosion & Little's Law Saturation
Why naive message brokers collapse during major world events. See the mathematical breaking point where queue backlog becomes insurmountable.
Suppose a background task queue has c = 200 workers, each taking an average service time of 1/μ = 2ms to execute a timeline ZADD. Maximum sustainable processing rate is c * μ = 100,000 tasks/s.
During an unexpected global live event, 5 popular accounts post simultaneously, generating 2,500,000 fan-out write tasks in 500ms. Inflow rate λ = 5,000,000 tasks/s exceeds capacity by 50x.
Utilization ρ = λ / (c * μ) = 50.0. By queueing fundamentals, when ρ > 1, queue length L_q grows linearly toward buffer capacity K. Once buffer fills, queue drops tasks, causing silent timeline desynchronization and missing posts.
- Follower Threshold Capping: Strictly exclude accounts with > 25k followers from the write fan-out pipeline.
- Active User Filtering: Do not fan-out to followers who have not opened the application in over 14 days. Populate their timelines lazily on login.
- Priority Queue Partitioning: Separate instant fan-out queues for friends and close contacts from broad public distribution queues to ensure personal notifications remain fast.
Six-Tier Distributed Architecture
The complete component architecture required to maintain sub-150ms p99 latency across 300 million daily active users.
Simulate the News Feed Topology Live
Test push vs pull fan-out trade-offs, trigger celebrity follower posting storms, and observe queue length escalation before tuning worker pool concurrency.