What it is
A home feed shows a user recent posts from the accounts they follow, usually ranked. The system behind it stores posts, keeps the follow graph, builds each user's timeline and serves it fast. The core design decision is when a timeline is assembled: when it is read (fan-out on read, "pull") or when a post is written (fan-out on write, "push").
Why it exists
Feeds are the canonical read-heavy, personalised workload. Every user's feed is different, so a single cached page does not help, and reads outnumber writes by orders of magnitude. Building a feed from scratch on every read means querying many shards and merging; doing it ahead of time means copying each post into many timelines. The case study is about choosing where to pay, and then handling the cases where that choice breaks.
How it works
Requirements. Functional: publish a post, follow/unfollow, read the home feed (paginated). Non-functional: feed reads well under 200 ms at the 95th percentile, new posts visible within seconds, high availability; small staleness is acceptable.
Estimates (illustrative). 300 M daily users × 10 feed opens/day ≈ 35 000 reads/s on average, a few times that at peak. If 10% post once a day, about 350 posts/s. With 200 followers on average, pure fan-out on write is about 70 000 timeline inserts/s. Timelines store post IDs (8–16 bytes each) capped at a few hundred entries, so 300 M timelines × 500 IDs × ~10 bytes ≈ 1.5 TB: memory-sized across a cache cluster.
Fan-out on read. Posts live in a store sharded by author. To build a feed, the feed service looks up who the user follows, queries every shard holding their recent posts, merges by time (or score) and returns the top N. Writes are cheap; each read costs one query per shard, so read traffic is multiplied by the shard count and the slowest shard sets the latency.
Fan-out on write. The post service stores the post once, then publishes an event to a topic. Fan-out workers look up the author's followers and insert the post ID into each follower's precomputed timeline (typically a capped list in an in-memory store, partitioned by user). In the lesson, the topic copies each post into one timeline queue per timeline-cache shard; each queue's writer handles the followers on that shard. A feed read is then one lookup, plus hydration of the IDs into posts (from a post cache).
The celebrity problem. Fan-out cost is proportional to follower count. An account with tens of millions of followers turns one post into tens of millions of inserts, which delays every other post queued behind it. The hybrid approach skips fan-out for accounts above a follower threshold; at read time the feed service fetches recent posts from the celebrities the user follows and merges them with the precomputed timeline. Inactive users are often skipped too: their timelines are rebuilt by fan-out on read when they return.
Ranking. The merged timeline is a candidate set (hundreds of posts). A ranking service fetches features (author affinity, engagement, recency, content type) from a feature store and scores candidates with a model, often in two stages (a cheap first pass, then a heavier model on the top few hundred). It runs on every feed read, so it gets a strict deadline and the feed falls back to chronological order when it misses.
Failure handling. The precomputed timelines are now critical state. If a cache node is lost, its users' reads fall back to fan-out on read, which the posts store is not sized for at full rate. Replicate timeline partitions, rebuild lost ones gradually from a replica or from the source with rate limits, and degrade (shorter, unranked feed) before the source overloads.
When to use it
Fan-out on write suits read-heavy feeds where most accounts have modest follower counts and freshness within seconds is fine. Fan-out on read suits small systems, feeds with very many sources per reader but little traffic, or rarely-read feeds. The hybrid is what large social feeds converge on.
When not to
- Small scale: one database with a good index on (author, created_at) and a query over followed authors is simpler and fast enough.
- Feeds that must be strictly consistent with the source (compliance, financial timelines): precomputed copies are always slightly stale.
- Highly dynamic feeds where ranking dominates and the candidate set comes from search or recommendation retrieval rather than the follow graph; the "timeline" is then a retrieval problem.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Fan-out on write | Feed read is one lookup | One insert per follower per post; storage for many copies; delayed visibility under backlog |
| Fan-out on read | Cheap writes, always fresh | Every read touches every shard; slowest shard sets latency |
| Hybrid threshold low | Little fan-out work | More pulled sources merged per read |
| Hybrid threshold high | Simple reads | Writers fall behind on big accounts |
| Ranking | Engagement, relevance | Latency and compute on every read; harder to explain and debug |
| Skip inactive users | Large saving in fan-out work | Rebuild cost and latency on their return |
Alternatives
- Pure pull with aggressive caching of per-author recent posts: reads merge from a cache rather than shards; good when the follow graph is small.
- Retrieval-based feeds (recommendation feeds): candidates come from embeddings or interest graphs, not only follows; the follow graph is one of several sources.
- Per-user materialised views in a database (instead of an in-memory cache): cheaper storage, slower reads.
Failure modes
- Fan-out backlog: a burst of large accounts posting, or slow writers, delays every post; visible as growing queue depth and age of the oldest message, not as slow reads.
- Timeline cache loss: reads for affected users fall back to the source at full rate, a thundering herd that can take the posts store down.
- Hot partitions: a celebrity's post or profile is read by millions at once; cache it separately and replicate it widely.
- Deleted or edited posts remain as copies in timelines until filtered at read time or cleaned by a reverse fan-out.
- Ranking timeouts: without a fallback, a slow model turns into failed feed loads.
In production
Store IDs, not post bodies, in timelines and hydrate from a post cache. Cap timeline length. Monitor fan-out lag (age of oldest unprocessed post) per queue and alert on it. Make fan-out idempotent (a redelivered event must not insert twice). Treat follow/unfollow as events too: a new follow backfills recent posts, an unfollow filters at read time. Give ranking a deadline and a chronological fallback. Rebuild lost timeline partitions from replicas with rate limits.
Common misconceptions
- "More shards fix slow fan-out on read." Each read still touches every shard; more shards spread data, not requests.
- "Fan-out on write makes posts appear instantly." They appear after the queue and writers process them; under load, freshness degrades first.
- "Celebrities are a read problem." Their followers' reads are fine; the cost is the write fan-out of each post.
- "The cache is just an optimisation." Once timelines are precomputed, losing them is an outage unless the fallback is protected.
Interview angle
Start with requirements and the read/write ratio, then present both fan-out models with their costs in numbers. Interviewers almost always ask about celebrities: answer with the hybrid, the threshold, and how merge cost grows with followed celebrities. Expect follow-ups on ranking latency, deleted posts, inactive users, new follows (backfill), pagination (cursor by post ID or score) and what happens when the timeline cache fails.
Model assumptions
The simulation is a simplified model, not a benchmark, and it runs a small slice of the illustrative scale: 150–800 feed reads/s and 10 posts/s. Posts shards have 8 connections, ~12 ms per read and ~20 ms per write (lognormal), with a 250 ms pool timeout. With fan-out on read, the Feed Service queries all three shards at the same time and merges when the slowest has answered, so a read costs the slowest shard's time, and every read still touches every shard. Once timelines are precomputed, the Feed Service asks its dependencies in order (cache first); a miss goes to a Timeline Builder that rebuilds the timeline by querying the three shards in parallel. The topic copies each post into three timeline queues; one copy stands for the inserts into all followers on that timeline shard. The Timeline Cache is a fixed 98% hit ratio, not a model of which timelines are present. The celebrity post is modelled as 600 fan-out batches in one second. The Feature Store lookup in the ranking step is a scripted illustration; Ranking itself is simulated as a 15 ms service. Timeline Writers updating both cache replicas is described, not simulated. Network hops are 2–20 ms. Numbers show behaviour and orders of magnitude only.