← Back to System Design, page 2

system-design-0038 · Hard senior

Design a social news feed like Facebook or LinkedIn for 300 million daily active users.

Store posts once, and build each user's feed as a list of post IDs. Use fan-out on write (push post IDs into followers' cached feeds) for normal users and fan-out on read (merge at request time) for accounts with huge follower counts, then rank and hydrate the candidates. With 60M posts per day and 200 average followers, fan-out alone means about 139,000 feed inserts per second on average.

Full answer

Requirements

Functional:

  • Users create posts (text, images, links) and follow other users.
  • The home feed shows recent, relevant posts from people and pages they follow, with infinite scroll.
  • Likes and comments counts appear with each post.

Non-functional:

  • Feed loads fast (target p99 around 300 ms server time).
  • High availability; a few seconds of delay before a new post appears in followers' feeds is acceptable (eventual consistency).
  • The author sees their own post immediately.

Capacity estimates

Assumptions: 300M DAU; 10 feed loads per user per day; 20% of users post once a day; average 200 followers; peak factor 2; a feed cache holds 500 post IDs per user at 8 bytes each.

  • Feed reads: 300M x 10 = 3B per day = about 34,700 per second; about 70,000 at peak.
  • Posts: 300M x 0.2 = 60M per day = about 700 per second.
  • Fan-out writes: 60M x 200 = 12B feed inserts per day = about 139,000 per second on average.
  • Feed cache: 500 x 8 B = 4 KB per user; 300M users x 4 KB = 1.2 TB, before replication. A Redis cluster can hold this, or keep cached feeds only for recently active users.
  • Post storage: if a post record averages 1 KB (media stored separately in object storage), 60M x 1 KB = 60 GB per day.

API

POST /v1/posts {"text": "...", "media_ids": ["m1"]} -> 201 {"post_id": ...} GET /v1/feed?cursor=...&limit=20 -> {"items": [{post, author, like_count, comment_count, liked_by_me}], "next_cursor": "..."} POST /v1/users/{id}/follow

Data model

  • posts: post_id (time-ordered ID), author_id, body, media, created_at. Partitioned by post_id or author_id.
  • follows: follower_id, followee_id, stored both ways (who I follow, who follows me) for fast fan-out.
  • feed_cache (Redis sorted set per user): member = post_id, score = time or rank score; trimmed to 500 entries.
  • Counters for likes and comments in a separate counter service.

High-level design

mermaid
flowchart LR
  U[Client] --> GW[API gateway]
  GW --> PS[Post service] --> PDB[(Posts store)]
  PS --> K[Post events: Kafka]
  K --> FO[Fan-out workers]
  FO --> SG[(Follow graph)]
  FO --> FC[(Feed cache: Redis)]
  GW --> FS[Feed service]
  FS --> FC
  FS --> CEL[Celebrity posts lookup]
  FS --> RK[Ranking]
  FS --> HY[Hydration: posts, users, counts]

Fan-out strategies

  • Fan-out on write (push): when a post is created, workers add its ID to each follower's feed cache. Reads are cheap (one sorted-set range), writes are expensive and delayed for large follower lists.
  • Fan-out on read (pull): at read time, fetch recent posts from everyone the user follows and merge. Writes are cheap; reads are expensive for users following many accounts.
  • Hybrid (chosen): push for authors with fewer than, say, 10,000 followers. For celebrities (millions of followers), do not fan out; at read time the feed service fetches their recent posts and merges them in. One celebrity post with 50M followers would otherwise create 50M writes.

Also skip fan-out to inactive users (no login in 30 days); build their feed on demand when they return.

Ranking and hydration

The feed service takes a few hundred candidate IDs (cache plus celebrity posts), scores them with a ranking model (recency, relationship strength, engagement predictions), then hydrates the top 20 with post bodies, author info and counts using batched multi-get calls to caches. The author's own new post is inserted directly into their own feed so they see it at once.

Bottlenecks and trade-offs

  • Fan-out throughput (about 139k inserts per second, more at peak) is handled by partitioned Kafka topics and horizontally scaled workers; spikes just add delay.
  • Hot posts: like counts for viral posts use sharded counters; hydration caches post data heavily.
  • Cache loss: feeds can be rebuilt from the follow graph and posts store, so the cache does not need full durability, but the database must handle rebuild load gradually.
  • Pagination uses a cursor on the ranked list; re-ranking between pages can cause duplicates, so the client sends seen IDs or the server snapshots the ranked list for the session.
  • Trade-off summary: push gives fast reads at high write cost; pull gives cheap writes and slow reads; the hybrid keeps reads fast while capping write amplification, at the cost of more complex merge logic.

By level

Fresher: Describe posts, follows and feed tables, and explain the idea of copying post IDs to followers' feeds versus collecting them at read time. A fresher can explain push versus pull with a small example.

Mid: Estimate fan-out and cache size, design the hybrid approach, data model and feed API with cursors, and explain hydration. Expect the fan-out multiplication to justify the hybrid approach.

Senior: Handle celebrity and inactive users, cache rebuild, ranking latency budgets, deletes and privacy changes across feeds, multi-region feed generation and cost of fan-out storage. The senior answer covers deletes and privacy changes across millions of feeds.

Key points

  • Feed = list of post IDs plus hydration at read time
  • Hybrid fan-out: push for normal authors, pull for celebrities
  • 60M posts x 200 followers = 12B inserts per day, about 139k per second
  • Feed cache of 500 IDs per user is about 1.2 TB for 300M users
  • Rank candidates, then hydrate the top results in batches

Common mistakes

  • Using pure fan-out on write and ignoring celebrities
  • Storing full post bodies in every follower's feed
  • Fanning out to inactive users
  • Offset pagination on a ranked, changing feed

Follow-ups

  • How would you remove a deleted post from millions of feeds?
  • How would you add ads into the feed?
  • How would the design change if feeds were strictly chronological?

Tips

  • Do the fan-out multiplication; it justifies the hybrid
  • Separate candidate generation, ranking and hydration
  • Mention the author seeing their own post immediately

Evaluation rubric

  • Requirements with consistency expectations
  • Correct capacity math including fan-out volume and cache size
  • Clear fan-out strategy with celebrity handling
  • Ranking, hydration, pagination and failure trade-offs