Skip to main content

Module 6: Case Studies

News Feed

Design Twitter/Instagram-style feeds — fan-out on write vs read, celebrity hybrid, ranking, caching, and pagination.

Feeds mix graph relationships, fan-out cost, and ranking. The classic debate is when to precompute timelines — and how celebrity accounts break a naive push model.

What you will learn

  • Clarify feed types, ranking needs, and scale assumptions
  • Compare fan-out on write vs fan-out on read with concrete trade-offs
  • Design a hybrid for celebrity / high-follower accounts
  • Place post store, graph, timeline cache, and ranking stages
  • Paginate with cursors and keep analytics off the hot path
Left: a normal post is copied into three timelines. Right: a celebrity post is pulled at read time.
Push ordinary posts. Pull celebrity posts when the reader opens the feed.

Clarify requirements

  • “News feed” can mean home timeline, following-only, or ranked Explore. Lock the product shape first.
  • Home feed of people you follow vs algorithmic recommendations
  • Post types: text, image, video pointers; likes/comments optional for v1
  • Latency: publish-to-follower visibility target; feed scroll p99
  • Scale: users, follows/user, posts/day, read:write ratio (reads dominate)
  • Order: reverse chronological first; ranking as a later layer

Core data model

  • Three stores show up in almost every solid answer.
  • User / social graph — follower and followee edges
  • Post service — postId, authorId, content refs, created_at, privacy
  • Timeline / feed cache — per-user ordered list of postIds (for push models)
  • Counters (likes) often async and approximate at first
  • Media in object storage + CDN; feed rows store pointers only

Fan-out on write (push)

  • When someone posts, precompute: push the postId into each follower’s timeline.
  • Write path: persist post → lookup followers → enqueue fan-out jobs → append to timeline caches
  • Read path: load precomputed timeline postIds → hydrate post bodies → return page
  • Pros: very fast home-feed reads
  • Cons: write amplification — 1 post × N followers writes/updates
  • Painful for celebrity accounts with millions of followers

Fan-out on read (pull)

  • On feed request, fetch recent posts from followees and merge/rank.
  • Write path: just persist the post (cheap)
  • Read path: get followee list → fetch each author’s recent posts → merge by time/score
  • Pros: no celebrity write explosion
  • Cons: heavier reads; many dependent fetches without caching
  • Mitigate with per-author recent-post caches and short feed result caches

Push vs pull comparison

  • Use this side-by-side when the interviewer asks which you choose.
  • Write cost — push: high; pull: low
  • Read cost — push: low; pull: high
  • Celebrity fit — push: bad; pull: better
  • Inactive users — push wastes work writing timelines never read
  • Interview default — hybrid: push for normal users, pull for celebrities

Hybrid celebrity pattern

  • Most production-style answers land here.
  • Normal users: fan-out on write into follower timelines
  • Skip push when follower count exceeds a threshold (celebrity)
  • On read: merge precomputed timeline with recent posts pulled from celebrity followees
  • Fan-out workers async so publish API returns quickly
  • Optionally do not push to inactive users; let them pull on next login

Publish & read paths

  • Narrate both paths end-to-end so the design feels concrete.
  • Publish: API → post DB → (async) fan-out queue → timeline updates for non-celebrity authors
  • Read: API → timeline cache / merge pull candidates → hydrate posts → rank → paginate
  • Hydration: batch-get posts by ids; cache hot posts in Redis
  • Privacy/blocks applied at candidate or hydration time
  • Never block publish on finishing millions of timeline writes

Ranking (keep it light)

  • Candidate generation first; ranking second. Do not start with a deep ML digression.
  • Stage 1: candidates from push timeline + celebrity pulls
  • Stage 2: score with recency, affinity, and simple engagement signals
  • Stage 3: diversity filters (not 20 posts from one author)
  • ML rankers are an extension once the pipes work
  • Caching ranked pages needs short TTL — rankings go stale

Caching & pagination

  • Feeds are read-heavy. Cache aggressively but invalidate carefully.
  • Timeline lists of postIds in Redis/Memorystore
  • Post body cache by postId; media via CDN
  • Cursor pagination (created_at + postId), not OFFSET
  • Prepend new posts for active sessions when using push
  • TTL / selective bust when users follow/unfollow (rebuild or lazy repair)

Scale & failure modes

  • Fan-out lag and hotspot authors are the usual interview follow-ups.
  • Shard timelines by userId; shard posts by postId/authorId
  • Fan-out lag: users may see posts seconds later — state the SLA
  • Hot post hydration after viral content — cache and protect origin
  • Follow/unfollow storms invalidate assumptions about timeline membership
  • Backfill jobs rebuild timelines after outages

Interview framing

  • Use this order in a feed design round.
  • Requirements + scale numbers + chronological v1
  • Data model: graph, posts, timeline cache
  • Push vs pull trade-off → hybrid celebrity solution
  • Walk publish and read paths with async fan-out
  • Add ranking stage, cursors, and cache

Quick revision checklist

  • Draw follow graph + post store + timeline cache.
  • Call out the celebrity / mega-follower problem.
  • Prefer hybrid push+pull in the final design.
  • Keep ranking after candidate generation.
  • Use cursor pagination, not OFFSET.
  • Fan-out asynchronously — do not block publish.

FAQs

Where does ranking sit?

After candidate generation (fan-out/pull). Start with recency; mention a ranking service as a later optimization.

Fan-out on write or read?

Write (push) optimizes reads but explodes for celebrities. Read (pull) optimizes writes but slows feeds. Hybrid is the usual interview answer.

What is the celebrity problem?

A user with millions of followers makes fan-out-on-write push a single post into millions of timelines. Skip push for those authors and pull their posts at read time.

How do you paginate feeds?

Use opaque cursors based on timestamp + postId (or rank key). OFFSET pagination gets slow and inconsistent as new posts arrive.

What do we store in the timeline cache?

Usually ordered postIds (and maybe light metadata), not full post bodies. Hydrate bodies in a batch get from the post service/cache.

How fast must a post appear in followers’ feeds?

Define an SLA (e.g. a few seconds). Async fan-out workers make publish fast while timeline updates catch up within that window.

Practice

Learner’s Task

  • Practice out loud for 10–12 minutes as if designing Twitter/Instagram home feed.
  1. State v1 requirements (chronological vs ranked), follows/user, and posts/day assumptions.
  2. Draw graph store, post store, and timeline cache; narrate a normal-user publish with push fan-out.
  3. Explain why a celebrity breaks push and how hybrid pull fixes it on read.
  4. Describe feed read: candidates → hydrate → light rank → cursor page.
  5. Write a 4-sentence closing: push/pull choice, celebrity handling, cache contents, and ranking placement.