The problem, stated properly
Design a social news feed (Facebook, Twitter/X, LinkedIn): users post, follow others, and open the app to a ranked, reverse-chronological-ish feed of content from everyone they follow, generated fast enough that the app never spins. This is the classic scale problem because it is a write-once-read-millions shape where the read must feel instant and the graph (who follows whom) makes every user's feed unique.
Scale first (Twitter-flavored numbers)
- Reads vs writes: ~500K posts/day per million active users is posting-heavy; realistically ~200M DAU generating ~100-500M posts/day (roughly 1-6K writes/sec) but reading feeds ~10-20x a day each: ~300K feed reads/sec. The read:write ratio is ~50-100:1.
- Fan-out: average users have ~200 followers, but celebrities have 100M. One celebrity post fanned out on write is 100M timeline writes from one API call: this asymmetry drives the entire design.
- Feed size: a feed needs ~50 items up front, ~1 KB each, and must render in under ~200 ms server time.
The central decision: fan-out on write vs fan-out on read
- Fan-out on write (push model): when Alice posts, her post ID is immediately pushed into the precomputed feed cache of every follower (one write per follower, into Redis lists capped at ~800 items). Reading the feed is then one O(1) cache lookup: blazing fast reads, expensive writes, and feeds are slightly stale by construction (fine for social). Cost: celebrity posts are write bombs, and inactive users accumulate wasted precomputed feed.
- Fan-out on read (pull model): the post goes into Alice's own posts store; when a follower opens the app, the system queries the recent posts of everyone they follow and merges. Writes are trivial; reads are brutal: following 500 people means 500 lookups and a k-way merge per feed load. Works when graphs are small or reads are rare; collapses at social scale.
- The hybrid (what everyone actually ships): push for normal users, pull for celebrities. Alice's post is fanned out on write to her 300 followers. When a celebrity with 100M followers posts, it goes only to the celebrity's posts store; each follower's feed is merged at read time with recent posts from the handful of celebrities they follow. Twitter publicly described exactly this. The classification is dynamic: cross a follower threshold and you flip to pull.
- Candidate generation: pull ~1,000 candidate posts: from the precomputed timeline (in-network), plus out-of-network recommendations (posts liked by people you follow, topic-similar content), which is where most engagement growth comes from.
- Heavy ranking: an ML model scores each candidate on predicted engagement (probability you will like, reply, dwell, and the negative signals: report, mute, "show less"). Features: author affinity, content type, recency decay, your historical behavior. Score x weight, sort, diversify (not 5 posts from the same author, not 8 videos in a row), filter (blocked, muted, already seen), serve the top ~50.
- Posts store: write-optimized NoSQL sharded by post ID (Snowflake IDs keep time-order roughly sortable). Media goes to blob storage + CDN; the post row holds references.
- Timeline cache: Redis lists per user, capped (~800 entries), holding post IDs only, not content. Content is hydrated from a separate post cache, so one viral post is cached once, not millions of times.
- Social graph: adjacency lists sharded by user ID. The two directions (following, followers) are both materialized because both queries exist and one-directional storage makes the other a full scan.
- The celebrity write bomb: before the hybrid model, one celebrity post is 100M cache writes and the fan-out workers fall hours behind. This literally happened to Twitter in its early years; the hybrid exists because of it.
- Fan-out lag: push is asynchronous; if the queue backs up, a follower sees a post minutes late. Monitor queue depth; degrade to pull-merge on read when lag crosses a threshold.
- Hot key reads: a viral post's content gets read by 100M clients: it must be served from cache/CDN, and a cache miss on it needs single-flight or the posts DB takes a 100M-request hug.
- Ranking feedback loops: the model shows what engages, engagement trains the model, and the loop drifts toward outrage. Needs explicit guardrail signals and periodic re-calibration; this is a known real-world failure, not a theoretical one.
- Proposing pure fan-out on read at Twitter scale. The k-way merge over 500 followings per feed load does not survive arithmetic.
- Proposing pure fan-out on write and ignoring celebrities. The follow-up "what about Elon?" arrives in seconds.
- Storing full post content in the timeline cache. IDs only; hydrate separately.
- Forgetting that inactive users make precomputed feeds pure waste. Many systems skip push for users who have not opened the app in N days.
Ranking: the feed is not a list, it is a product
Reverse-chronological died years ago. Ranking is typically two-stage:
Interview answer level: know that ranking is a separate service consuming the candidate feed, that it optimizes a weighted engagement objective, and that every engagement metric is a tradeoff against user trust (rage-bait ranks well and rots the product).
Storage and caching shape
Production failure modes
Common interview traps
Interview Q&A
Q: A user follows 5,000 people including 50 celebrities. How is their feed assembled at read time?
A: One Redis lookup gets their precomputed timeline (fan-out-on-write already merged the 4,950 normal accounts). Then one lookup per celebrity account they follow (50 small reads against a hot post cache, all cache hits in practice) grabs recent celebrity posts. Merge by timestamp/rank score, dedupe, hydrate post content from the post cache, rank the top candidates, serve ~50. Total: ~52 cache reads, no database touched on the happy path.
Q: How do you handle "I already saw this" across devices?
A: Seen-state is tracked server-side per user (a compact set of seen post IDs, often a Bloom filter or capped Redis set), and the ranking stage filters seen items while keeping a small reservoir for resurfacing high-value content. The seen-marker write is asynchronous and batched (marking 50 posts seen on every scroll must never be synchronous), and it syncs across devices through the same user-keyed store.
Problem ko sahi se samjho
Social news feed design karo (Facebook, Twitter/X, LinkedIn): users post karte hain, doosron ko follow karte hain, aur app kholte hain to un sabke content ka ranked, reverse-chronological-ish feed, itna fast ki app kabhi spin na kare. Ye classic scale problem hai kyunki ye write-once-read-millions shape hai jahan read instant feel hona chahiye aur graph (kaun kise follow karta hai) har user ka feed unique banata hai.
Pehle scale (Twitter jaise numbers)
- Reads vs writes: realistically ~200M DAU, ~100-500M posts/day (roughly 1-6K writes/sec) par feeds din mein ~10-20x padhte hain: ~300K feed reads/sec. Read:write ratio ~50-100:1.
- Fan-out: average users ke ~200 followers, par celebrities ke 100M. Ek celebrity post ka write-time fan-out ek API call se 100M timeline writes: yehi asymmetry poora design chalati hai.
- Feed size: feed ko shuru mein ~50 items chahiye, ~1 KB each, aur server time ~200 ms ke andar render hona chahiye.
Central decision: fan-out on write vs fan-out on read
- Fan-out on write (push model): jab Alice post karti hai, uska post ID turant har follower ke precomputed feed cache mein push ho jaata hai (har follower ke liye ek write, Redis lists mein ~800 items cap). Feed padhna phir ek O(1) cache lookup hai: blazing fast reads, mehengi writes, aur feeds thode stale by construction (social ke liye theek). Cost: celebrity posts write bombs hain, aur inactive users ke liye precomputed feed bekaar jama hota hai.
- Fan-out on read (pull model): post Alice ke apne posts store mein jaata hai; jab follower app kholta hai, system un sabke recent posts query karta hai jinhe wo follow karta hai aur merge karta hai. Writes trivial; reads brutal: 500 log follow karna matlab har feed load pe 500 lookups aur ek k-way merge. Tab kaam karta hai jab graphs chhote hon ya reads kam hon; social scale pe collapse.
- Hybrid (jo sab asal mein ship karte hain): normal users ke liye push, celebrities ke liye pull. Alice ka post uske 300 followers ko write pe fan-out hota hai. Jab 100M followers wali celebrity post karti hai, wo sirf celebrity ke posts store mein jaata hai; har follower ka feed read time pe un kuch celebrities ke recent posts se merge hota hai jinhe wo follow karta hai. Twitter ne publicly exactly yehi describe kiya. Classification dynamic hai: follower threshold cross karo aur pull pe flip.
Ranking: feed list nahi, product hai
Reverse-chronological saalon pehle mar gaya. Ranking typically do-stage hoti hai:
- Candidate generation: ~1,000 candidate posts nikalo: precomputed timeline se (in-network), plus out-of-network recommendations (jinhe tum follow karte ho unke liked posts, topic-similar content), jahan se zyadatar engagement growth aati hai.
- Heavy ranking: ek ML model har candidate score karta hai predicted engagement pe (probability ki tum like, reply, dwell karoge, aur negative signals: report, mute, "show less"). Features: author affinity, content type, recency decay, tumhara historical behavior. Score x weight, sort, diversify (ek hi author ke 5 posts nahi, lagatar 8 videos nahi), filter (blocked, muted, already seen), top ~50 serve karo.
Interview level pe: ye jaano ki ranking ek alag service hai jo candidate feed consume karti hai, wo weighted engagement objective optimize karti hai, aur har engagement metric user trust ke against tradeoff hai (rage-bait achha rank karta hai aur product ko sadta hai).
Storage aur caching shape
- Posts store: write-optimized NoSQL, post ID se sharded (Snowflake IDs time-order ko roughly sortable rakhte hain). Media blob storage + CDN mein; post row references rakhti hai.
- Timeline cache: Redis lists per user, capped (~800 entries), sirf post IDs rakhti hain, content nahi. Content alag post cache se hydrate hota hai, to ek viral post millions baar nahi, ek baar cache hota hai.
- Social graph: adjacency lists, user ID se sharded. Dono directions (following, followers) materialized hain kyunki dono queries exist karti hain aur ek-directional storage doosre ko full scan bana deta hai.
Production mein kya tootta hai
- Celebrity write bomb: hybrid model se pehle, ek celebrity post 100M cache writes hai aur fan-out workers ghanton peeche pad jaate hain. Twitter ke early years mein literally yehi hua; hybrid isi wajah se exist karta hai.
- Fan-out lag: push asynchronous hai; queue back up ho to follower post ko minutes baad dekhta hai. Queue depth monitor karo; lag threshold cross kare to read pe pull-merge degrade karo.
- Hot key reads: viral post ka content 100M clients padhte hain: use cache/CDN se serve hona chahiye, aur us pe cache miss ko single-flight chahiye warna posts DB ko 100M-request ka hug milega.
- Ranking feedback loops: model wo dikhata hai jo engage karta hai, engagement model ko train karta hai, aur loop outrage ki taraf drift karta hai. Explicit guardrail signals aur periodic re-calibration chahiye; ye known real-world failure hai, theoretical nahi.
Common interview traps
- Twitter scale pe pure fan-out on read propose karna. Har feed load pe 500 followings ka k-way merge arithmetic survive nahi karta.
- Pure fan-out on write propose karke celebrities ignore karna. Follow-up "what about Elon?" seconds mein aata hai.
- Timeline cache mein poora post content store karna. Sirf IDs; hydrate alag se.
- Bhoolna ki inactive users precomputed feeds ko pure waste banate hain. Kai systems un users ke liye push skip karte hain jinhone N din se app nahi khola.
Interview Q&A
Q: Ek user 5,000 log follow karta hai jinme 50 celebrities hain. Read time pe uska feed kaise assemble hota hai?
A: Ek Redis lookup uski precomputed timeline laata hai (fan-out-on-write ne 4,950 normal accounts pehle se merge kar diye). Phir har followed celebrity account ke liye ek lookup (50 chhote reads hot post cache ke against, practically sab cache hits) recent celebrity posts laata hai. Timestamp/rank score se merge, dedupe, post cache se content hydrate, top candidates rank, ~50 serve. Total: ~52 cache reads, happy path pe database touch hi nahi hoti.
Q: Devices ke across "maine ye dekh liya" kaise handle karte ho?
A: Seen-state server-side per user track hota hai (seen post IDs ka compact set, aksar Bloom filter ya capped Redis set), aur ranking stage seen items filter karti hai, high-value content resurfacing ke liye chhota reservoir rakhte hue. Seen-marker write asynchronous aur batched hai (har scroll pe 50 posts seen mark karna kabhi synchronous nahi ho sakta), aur same user-keyed store se devices ke across sync hota hai.