Requirements & scope
Problem Statement & Requirements
Functional Requirements
- Publish posts -- users create text posts, links, photos, or videos
- News feed -- a personalized, ranked stream of posts from friends/followed accounts
- Retweet / share -- amplify someone else's post to your followers
- Like, reply, quote-tweet -- engagement actions
- Follow / friend -- asymmetric (Twitter: follow) or symmetric (Facebook: friend)
- Trending topics -- surface popular topics in real-time
- Notifications -- new followers, likes, mentions, replies
- Real-time updates -- new posts appear in feed without manual refresh (for active users)
Non-Functional Requirements
- Low latency -- feed renders in < 500 ms
- High availability -- 99.99% uptime
- Eventually consistent -- slight delay in feed propagation is acceptable
- Massively read-heavy -- read:write ratio ~1000:1
- Scalability -- 500M+ DAU, 1B+ feed reads/day
- Freshness -- breaking news and viral content surface within seconds
Key Differences from Instagram Design
| Aspect | Twitter / Facebook | |
|---|---|---|
| Primary content | Photos/video | Text + mixed media |
| Feed model | Visual grid + ranked feed | Chronological stream (Twitter) or ranked (Facebook) |
| Engagement style | Like + comment | Like, reply, retweet, quote-tweet, bookmark |
| Amplification | None (no reshare) | Retweet = core mechanic, viral amplification |
| Graph type | Asymmetric (follow) | Twitter: asymmetric; Facebook: symmetric |
| Real-time need | Moderate | High (breaking news, live events) |
| Threading | Flat comments | Threaded conversations (Twitter threads) |
Scale estimations
Scale Estimations
Users & Content
| Metric | Value |
|---|---|
| Daily Active Users (DAU) | 500M |
| Total users | 2B |
| New posts / day | 500M |
| Average post size (text + metadata) | 1 KB |
| Posts with media | 30% (images/video served via CDN) |
| Average friends/following per user | 300 |
| Avg feed reads per user per day | 10 |
| Total feed reads / day | 5B |
Traffic
| Metric | Value |
|---|---|
| Feed reads / second (avg) | ~58K RPS |
| Feed reads / second (peak) | ~200K RPS |
| Post writes / second | ~5,800 WPS |
| Read:Write ratio | ~10:1 (feed reads) to ~1000:1 (including passive reads) |
Storage
| Metric | Value |
|---|---|
| Post storage / day | 500M x 1 KB = 500 GB/day |
| Post storage / year | ~180 TB/year |
| Feed cache (Redis) | 500M users x 500 post_ids x 16 bytes = ~4 TB |
| Social graph | 2B users x 300 avg edges = 600B edges |
Fanout
| Metric | Value |
|---|---|
| Avg followers per poster | 300 |
| Total fanout writes / day | 500M posts x 300 = 150B feed cache writes/day |
| Fanout writes / second | ~1.7M WPS |
| Celebrity post (10M followers) | 10M writes per single tweet |
Layered architecture
High-Level Architecture
High-Level ArchitectureExcalidraw diagram · editable shapes · reveal step by stepExplore
Detailed Architecture
Detailed ArchitectureExcalidraw diagram · editable shapes · reveal step by stepExplore
API & contracts
Workshop note · added for the website’s common reading format
For News Feed, define contracts around the boundary components: Client, API gateway, Post service. Specify authentication, request identity, versioning, pagination or streaming semantics, timeouts, and retry behavior. Name which operations are idempotent and how callers discover an uncertain outcome.
The source discusses these contracts within its subsystem walkthroughs rather than as a standalone endpoint catalog. The examples in this article are design exercises, not published service APIs.
Data model
Data Model
Posts Table
Posts TableExcalidraw diagram · editable shapes · reveal step by stepExplore
Conversation Threading
Conversation ThreadingExcalidraw diagram · editable shapes · reveal step by stepExplore
Core design decisions
Ranking System (Deep Dive)
Feature Categories
Feature CategoriesExcalidraw diagram · editable shapes · reveal step by stepExplore
Ranking Model Architecture
Ranking Model ArchitectureExcalidraw diagram · editable shapes · reveal step by stepExplore
Two-Phase Ranking
Two-Phase RankingExcalidraw diagram · editable shapes · reveal step by stepExplore
Fanout Strategies Compared
Fanout Strategies ComparedExcalidraw diagram · editable shapes · reveal step by stepExplore
Request flows
Post Publishing Flow
Post Publishing FlowExcalidraw diagram · editable shapes · reveal step by stepExplore
Retweet / Share Flow
Retweet / Share FlowExcalidraw diagram · editable shapes · reveal step by stepExplore
Feed Read Flow -- Timeline Assembly
This is the core of the design -- how a user's feed is constructed on read.
Feed Read Flow -- Timeline AssemblyExcalidraw diagram · editable shapes · reveal step by stepExplore
Timeline Assembly Flow Diagram
Timeline Assembly Flow DiagramExcalidraw diagram · editable shapes · reveal step by stepExplore
Performance & caching
Caching Architecture (Multi-Layer)
Caching Architecture (Multi-Layer)Excalidraw diagram · editable shapes · reveal step by stepExplore
Advanced design
Real-Time Feed Updates
Real-Time Feed UpdatesExcalidraw diagram · editable shapes · reveal step by stepExplore
Trending Topics
Trending TopicsExcalidraw diagram · editable shapes · reveal step by stepExplore
Handling Viral Posts
Handling Viral PostsExcalidraw diagram · editable shapes · reveal step by stepExplore
Edge cases
Workshop note · added for the website’s common reading format
Walk through three failure moments in News Feed: a request times out before its result is known; a dependency becomes slow rather than unavailable; and a process restarts after committing state but before acknowledging it.
For each case, name the authoritative record, define a safe retry, cap resource usage, and describe what the caller sees. Event pipeline should help detect and contain the problem: Track fanout lag and cap expensive viral workloads before queues cascade.
Tradeoffs
Tradeoffs & Design Decisions Summary
| Decision | Option A | Option B | Chosen | Why |
|---|---|---|---|---|
| Fanout strategy | Fanout-on-write | Fanout-on-read | Hybrid | Push for normal users (fast reads), pull for celebrities (avoid 100M writes) |
| Feed order | Chronological only | ML-ranked only | Both tabs | "Following" for chronological purists; "For You" for engagement optimization |
| Ranking model | Single-task (P(click)) | Multi-task (like, reply, RT, hide) | Multi-task | Optimizes for healthy engagement, not just clicks |
| Ranking pipeline | Single model | Two-phase (pre-rank + heavy rank) | Two-phase | Pre-rank prunes 80% cheaply; heavy rank uses expensive features on fewer candidates |
| Real-time updates | WebSocket (all users) | Polling + SSE hybrid | Hybrid | Only ~5% of users are actively watching feed; WebSocket for all is wasteful |
| Feed cache | Database query | Redis sorted set | Redis | O(1) reads, pre-computed, handles 200K RPS |
| Post cache | Redis | Memcached | Memcached | Simple key-value, no persistence needed, better memory efficiency for cache |
| Trending detection | Batch (hourly) | Stream processing (real-time) | Stream (Flink) | Trends need to surface within minutes, not hours |
| Trending metric | Absolute volume | Relative spike vs baseline | Relative spike | "Weather" always has high volume but isn't trending; relative detects actual spikes |
| Retweet storage | Copy post | Pointer to original | Pointer | One post can have 1M retweets -- can't copy 1M times |
| Thread model | Flat replies | Conversation threading | Threading | conversation_id groups all replies; reply_to enables tree structure |
| Out-of-network content | None (only followed users) | SimClusters + social proof | SimClusters | "For You" tab needs content discovery beyond your follows |
| Viral post handling | Same as normal | Hot key replication + async counters | Special handling | 100K reads/sec on one post breaks a single cache node |
| Counter updates | Synchronous DB write | Kafka -> aggregate -> batch flush | Async batch | Can't do 100K writes/sec to a single row |
Reliability & fault tolerance
Workshop note · added for the website’s common reading format
Track fanout lag and cap expensive viral workloads before queues cascade.
Set service-level objectives for the user-visible path, then map its dependencies. Define bounded retries with jitter, deadlines, and backpressure. Keep a degraded mode that protects authoritative state, and test recovery instead of treating replication as a backup.
For News Feed, pay special attention to Post store, Feed cache, Graph + features when deciding failure domains and recovery procedures.
Production architecture
Production Architecture
Production ArchitectureExcalidraw diagram · editable shapes · reveal step by stepExplore
Further exploration
Twitter-Specific: "For You" vs "Following"
Twitter-Specific: "For You" vs "Following"Excalidraw diagram · editable shapes · reveal step by stepExplore
Interview playbook
Interview Tips
Frame the two core problems -- "A news feed has two hard problems: (1) how to build the feed efficiently (fanout strategy) and (2) how to rank it (ML pipeline). Let me address both."
Fanout is the opening move -- Start with fanout-on-write vs fanout-on-read. Show the celebrity problem. Land on hybrid. This is the foundation everything else builds on.
Timeline assembly is the full picture -- "Building the feed is a 5-stage pipeline: gather candidates, hydrate, rank, filter, return." Walk through each stage. This shows you think about the complete system, not just storage.
Ranking shows ML maturity -- "We use a multi-task model that predicts P(like), P(reply), P(retweet), and P(hide). The final score is a weighted combination optimizing for healthy engagement." Name specific features.
Caching layers show systems depth -- "There are 6 layers of cache: client SQLite, CDN for media, Redis for feed, Memcached for posts, Memcached for users, TAO for social graph." Name each one and its purpose.
Trending is a stream processing problem -- "We use Flink to process every post in real-time, extract entities, count in sliding windows, and compute spike ratios vs baseline." Shows you know stream processing.
Retweet is a unique design concern -- "A retweet is a pointer, not a copy. It triggers the same fanout as a new post but for the retweeter's followers. Celebrity retweets = same celebrity fanout problem."
Real-time updates are overvalued -- "Only ~5% of users are actively watching their feed. For the other 95%, the feed is assembled on next app open. Don't over-engineer WebSocket for everyone."
"For You" vs "Following" is a product decision -- "Algorithmic feeds increase engagement 40-60% but users feel less control. Offering both tabs is the modern compromise."
Viral content handling shows production thinking -- "A single viral post can get 100K reads/sec on one cache key. We detect hot keys, replicate across cache shards, and use approximate counters to avoid write storms."