News Feed system design: how it works

Builds most feeds ahead of time, but pulls celebrity posts in when a feed is read, so one post never means millions of writes.

Loading the interactive diagram…

How News Feed works, step by step

Builds most feeds ahead of time, but pulls celebrity posts in when a feed is read, so one post never means millions of writes.

People

Poster's app
Client · Hypothetical. Ana, posting a photo for her 800 followers. Or a celebrity with 40 million.
Reader's app
Client · Hypothetical. Ben, opening the app and scrolling his feed.

Front door

API gateway
Gateway · Hypothetical. Sends posts to the post service and feed requests to the feed service.

Services

Post service
Service · Hypothetical. Saves a new post and announces it. Answers the poster without waiting for any feeds to update.
New-post events
Queue · Hypothetical. "Ana posted 9001": the trigger for fan-out.
Fan-out workers
Service · Hypothetical. Writes each new post's id into every follower's feed. Except for celebrities.
Feed service
Service · Hypothetical. Builds the feed someone sees: cached ids, plus celebrity posts, ranked, then filled in with the actual posts.

Data

Post store
Database · Hypothetical. Every post: author, text, media links, time. Split across many servers by post id.
Follow graph
Database · Hypothetical. Who follows whom, and which accounts count as celebrities.
Feed cache
Cache · Hypothetical. Per user: the ids of the newest few hundred posts for their feed, and briefly the ranked pages being scrolled.

Flow: Post something (fan-out on write)

Ana posts. She gets her answer as soon as the post is saved; copying it into 800 feeds happens afterwards, one-way, in the background.

  1. Poster's app → API gateway (New post). Ana posts a photo.
  2. API gateway → Post service (New post by Ana). The gateway sends it to the post service.
  3. Post service → Post store (Save post 9001). The post is saved with its new id, 9001.
  4. Post store → Post service (Saved). The post exists.
  5. Post service → New-post events (Ana posted 9001). The post service announces it on the event stream, through an outbox written with the post, so a crash between saving and announcing can't lose the announcement.
  6. Post service → API gateway (Posted). Ana's app is answered now, before any feed is touched.
  7. API gateway → Poster's app (Posted). Ana sees her post.
  8. New-post events → Fan-out workers (Ana posted 9001). A fan-out worker picks up the event.
  9. Fan-out workers → Follow graph (Followers of Ana?). It asks who follows Ana, and whether she counts as a celebrity.
  10. Follow graph → Fan-out workers (800, not a celebrity). 800 followers: small enough to push.
  11. Fan-out workers → Feed cache (Add 9001 to 800 feeds). It adds 9001 to the front of 800 cached feeds, in batches. Within a few seconds, every follower's next refresh includes it.

Flow: Open your feed

Ben opens the app: his cached ids, plus fresh celebrity posts, ranked and filled in.

  1. Reader's app → API gateway (GET /feed). Ben opens the app.
  2. API gateway → Feed service (Feed for Ben). The gateway sends it to the feed service.
  3. Feed service → Feed cache (Ben's feed ids). It reads Ben's precomputed list of post ids.
  4. Feed cache → Feed service (300 post ids). 300 ids, already including Ana's 9001, pushed there seconds ago.
  5. Feed service → Follow graph (Celebrities Ben follows?). Celebrity posts were never pushed, so it asks which big accounts Ben follows.
  6. Follow graph → Feed service (@star, @cricketnews). Two.
  7. Feed service → Post store (Latest from 2 + top 150). It fetches the two celebrities' recent posts and merges them with the 300 ids. It ranks them using signals kept with each id (who posted, when, early likes), then fetches the top 150 posts, the first three pages, in one batch.
  8. Post store → Feed service (150 posts). Text, media links, like counts.
  9. Feed service → API gateway (Ranked feed + cursor). The feed service answers with the first 50 posts and a cursor, and keeps pages 2 and 3, ready to show, in the feed cache for a few minutes.
  10. API gateway → Reader's app (Your feed). Ben's feed appears.

Flow: A celebrity posts (no fan-out)

An account with 40 million followers posts. It's saved once and never copied into feeds; readers pull it in when they open the app. The fan-out check runs in the background: nobody waits for it.

  1. Poster's app → API gateway (New post). @star posts to 40 million followers.
  2. API gateway → Post service (New post by @star). Same door as everyone else.
  3. Post service → Post store (Save post 9002). Saved once, like any post.
  4. Post store → Post service (Saved). Stored.
  5. Post service → New-post events (@star posted 9002). Announced like any post.
  6. Post service → API gateway (Posted). The poster is answered.
  7. API gateway → Poster's app (Posted). Done, as far as @star can tell.
  8. New-post events → Fan-out workers (@star posted 9002). A fan-out worker picks up the event.
  9. Fan-out workers → Follow graph (Followers of @star?). It checks the account.
  10. Follow graph → Fan-out workers (40 million: celebrity). A celebrity. The worker stops here: no feeds are written. Readers' feed requests will pull post 9002 in (see "Open your feed").

Flow: Scroll for more

The next pages were ranked and filled in when the feed opened, so scrolling is one cache read. The busiest request in the system.

  1. Reader's app → API gateway (GET /feed?cursor=…). Ben scrolls to the bottom; the app asks for the next page with the cursor it was given.
  2. API gateway → Feed service (Next page for Ben). The gateway sends it to the feed service.
  3. Feed service → Feed cache (Page 2 of Ben's feed). Page 2 was ranked and filled in when Ben opened the app, and kept in the feed cache.
  4. Feed cache → Feed service (50 posts). Fifty posts, ready to show. (Past page 3, or once the saved pages expire, the feed service builds more as in "Open your feed".)
  5. Feed service → API gateway (50 more posts). The feed service sends them back.
  6. API gateway → Reader's app (More posts). The feed keeps going.

HowItScales is not affiliated with the companies shown. Components marked Assumption or Hypothetical are not confirmed by the company.