NEWS FEED: FAN-OUT

Push for most users, pull for celebrities

L318 MINSYSTEM DESIGN
PREDICT

Push copies each new post into every follower's stored timeline; pull reads the followed accounts' posts when a feed loads. Ann follows ten accounts, three of them with more than 100K followers (one is the pop star, with 9M). A post may write at most 100K timelines and a feed load may query at most 6 sources. Which design keeps both budgets?

A feed can be built at write time or at read time. Fan-out on write (push) copies each post into every follower’s precomputed timeline: a feed load is one lookup, and a post costs one write per follower. Fan-out on read (pull) stores the post once and merges at load time: posting is free, and every refresh queries every account the reader follows.

Neither survives a real follow graph. Push dies on the accounts with millions of followers; pull dies on readers who follow hundreds of accounts. The hybrid pushes for accounts below a follower threshold and pulls the few above it, merging them into the precomputed timeline at read time.

▶ SIMULATION
POSTS (PUSH)
—
worst post 0 / 100K writes
ANN'S PRECOMPUTED TIMELINE
—
total timeline writes 0
ANN LOADS HER FEED
sources queried — / 6
feed —
not loaded yet
last event—
✓ write work per post <= 100K timelines✓ read work per feed load <= 6 sources
SCHEDULE
01[ANN]fan-out on write for every account
02[SAM]sam (150 followers) posts "lunch pic"
03[CHEF]chef (95K followers) posts "new risotto"
04[POPSTAR]popstar (9M followers) posts "tour dates!"
05[ANN]Ann loads her feed
06[NEWS]news (400K followers) posts "storm warning"
07[CAFE]cafe (30K followers) posts "open late"
08[LEAGUE]league (2.5M followers) posts "final score"
09[ANN]Ann loads her feed
10[ANN]report
Start of push to everyone scenario. 10 steps.
// fan-out on write for everyone
function publish(post, author) {
  savePost(post);
  for (const f of followersOf(author)) appendToTimeline(f, post);  // 9M writes for one post
}
const readTimeline = (u) => timeline(u);

Watch both invariants. PUSH TO EVERYONE breaks the write budget the moment the pop star posts. PULL FOR EVERYONE breaks the read budget on Ann’s first load. HYBRID at the default 50K keeps both, and Ann’s feed still holds every post.

TUNE IT NOT YET

Every account above the threshold is pulled, and every reader who follows it pays one extra query on every refresh. Pull only the accounts you have to: find a threshold where no post writes more than 100K timelines and Ann's load queries as few sources as possible.

GOAL: writes <= 100K per post and Ann's load queries only the timeline plus the accounts too big to push

FIX IT NOT YET

Make the feed hybrid. publish must push to followers only when the author has threshold followers or fewer; above that, the post is only saved. readTimeline must return every post from accounts the user follows, newest first (higher t first), querying the precomputed timeline plus only the followed accounts above the threshold.

Mental model:

a feed is a trade between write amplification and read amplification, and the follow graph is skewed: a few accounts have most of the followers. Push for the long tail, where writes are cheap; pull for the head, where a reader follows only a few. The threshold is the line between the account your writers can afford and the one they cannot.

IN PRODUCTION

Twitter/X's timeline service famously uses a hybrid: Redis timelines filled by fan-out on write, with high-follower accounts merged in at read time. Instagram and Facebook feeds add ranking on top, but the push/pull split for celebrities is the same idea. Also in notification fan-out, group chats, and any activity stream.

IN INTERVIEWS

“Design the Twitter home timeline.” On the whiteboard: posts go to a post store; a fan-out service writes post ids into each follower's timeline list in a cache. Then do the arithmetic out loud: a 9M-follower account turns one post into 9M writes. Propose the hybrid: skip fan-out above a follower threshold, and at read time merge the cached timeline with the recent posts of the few celebrities the user follows. Say how you pick the threshold (write budget versus reads per load), and mention inactive users: skip fan-out for accounts that have not logged in recently and rebuild their timeline on demand.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: NOTIFICATION PIPELINE →