Case Study: News Feed at Scale

A Twitter/LinkedIn-style feed: fan-out on write vs read, the celebrity problem, ranking, and real-time updates.

Advanced9 min read
Language
Theme
New postfan-out on writeFollower feed cachesRedis lists(one entry each)Celebrities: pullfetch their posts at read timeMerge + rankcandidates -> scoreUser's feedHybrid: push for normal users, pull for celebrities (the Twitter answer)
Key idea at a glance

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.
  • Ranking: the feed is not a list, it is a product

    Reverse-chronological died years ago. Ranking is typically two-stage:

    1. 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.
    2. 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.

    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

    • 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.

    Production failure modes

    • 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.

    Common interview traps

    • 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.

    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.

Key takeaways

Interview pointer

This is the core question for both Instagram and Twitter. Estimate the ratio first, then the fan-out tradeoff, then the celebrity problem, then the ranking pipeline. Follow that order and the answer stays structured.