Design a news feed

System design Senior Python 3.13+ PostgreSQL 17+ Reviewed By Best Latest Apps

Pick the fix

An account with 30 million followers posts. With pure fan-out on write, what goes wrong and what's the fix?

Options
Hint 1 of 1

How many writes does one post cause?

Choose an answer, then open Show answer below to check it.

Boxes for post API, fan-out queue, feed workers, feed store, post cache and feed API; follow one post to followers.
Text version

Boxes on the board, in order:

  1. Post API
  2. Fan-out queue
  3. Feed workers
  4. Feed store
  5. Post cache
  6. Feed API
Show answer

Answer: A 30 million feed writes per post; merge that account's posts at read time instead

Correct. The hybrid approach.

Fan-out on write copies each post into every follower's feed. That is cheap for normal accounts and impossible for 30 million followers, so huge accounts are merged in when a follower reads.

Why not the others?
  • B Nothing; Redis is fast. 30 million writes per post backs up the queue.
  • C Fan out synchronously in the request. The request would take minutes.
  • D Drop followers above 1 million. Not a product option.

30-second answer

Two approaches. Fan-out on write: when someone posts, push the post id into each follower's precomputed feed list (in Redis or a wide-column store), so reading is one fast lookup. Fan-out on read: build the feed at request time by merging recent posts from everyone you follow. Write fan-out breaks for accounts with millions of followers, so use a hybrid: fan out for normal accounts, and merge posts from celebrity accounts at read time. Paginate with a cursor.

Practise saying it · 40 s. No recording, just a timer

Design a feed.

Say your answer out loud, then check which of these points you covered:

  • Fan-out on write vs read
  • Hybrid for celebrities
  • Ids in feeds, hydrate
  • Queue-driven fan-out
  • Cursor pagination

Deep dive

Store post ids in feeds, and fetch post bodies from a cache in batches.

Cap feed lists (for example the latest 800 ids).

Ranking adds a scoring step over candidates.

Fan-out runs as background jobs from a queue.

Common mistakes

  • Pure fan-out on write for celebrities.
  • Offset pagination on a changing feed.
  • Storing full post bodies in every follower's feed.

Senior take & follow-ups

Compare write vs read fan-out, propose hybrid, then storage, hydration and pagination.

Follow-up questions

Think of your answer first, then open the hint.

  1. Why the hybrid?

    Show hint

    Celebrity posts would mean millions of writes each.

  2. Why cursor pagination?

    Show hint

    New posts shift offsets and cause duplicates.

Suggest a fix if something here is wrong or out of date.