Sikau

System design · Day 12 · about 50 min

Capstone: A news feed

Fan-out on write for most authors, on read for the famous ones.

Show me what my friends posted

Opening the app, a learner expects a feed of what the people they follow posted — newest first, instantly. There are two ways to build it. Fan-out on read: when someone opens the feed, query every author they follow and merge. Simple to write, but slow when they follow 500 people. Fan-out on write: when an author posts, push the post into each follower's ready-made inbox. Reading is one cheap lookup.

Write fan-out has a trap. A celebrity with 10 million followers would trigger 10 million inbox writes for one post. Real systems use a hybrid: push for ordinary authors, and for celebrities skip the push and pull their posts in when the feed is read.

The hybrid feed

Our model: an author with at most pushLimit followers is pushed to inboxes; above it, readers pull. At read time we merge the inbox with the posts of every followed author who is over the limit, remove duplicates (an author may have been pushed earlier, before becoming popular) and sort newest first.

function createFeed({ pushLimit = 3 } = {}) {
  const followers = new Map();
  const following = new Map();
  const inbox = new Map();
  const byAuthor = new Map();
  const texts = new Map();
  let nextId = 1;
  let pushed = 0;
  const setOf = (m, k) => { if (!m.has(k)) m.set(k, new Set()); return m.get(k); };
  const listOf = (m, k) => { if (!m.has(k)) m.set(k, []); return m.get(k); };
  return {
    follow(user, author) {
      setOf(following, user).add(author);
      setOf(followers, author).add(user);
    },
    post(author, text) {
      const id = nextId++;
      texts.set(id, text);
      listOf(byAuthor, author).push(id);
      const fans = setOf(followers, author);
      if (fans.size <= pushLimit) {
        for (const fan of fans) { listOf(inbox, fan).push(id); pushed++; }
      }
      return id;
    },
    timeline(user, n = 10) {
      const ids = new Set(listOf(inbox, user));
      for (const author of setOf(following, user)) {
        if (setOf(followers, author).size > pushLimit) {
          for (const id of listOf(byAuthor, author)) ids.add(id);
        }
      }
      return [...ids].sort((a, b) => b - a).slice(0, n).map((id) => texts.get(id));
    },
    stats() { return { pushed }; },
  };
}
  • Push cost grows with followers per post; pull cost grows with authors followed per read.
  • Most reads happen far more often than posts, so moving work to write time is usually a win.
  • Cache the finished timeline for active users (Day 3) and page it with a cursor.

The rest of this lesson

  • Feed vocabularymatch
  • How many writes?predict
  • Warm-up: build the timeline on readwrite code
  • Warm-up: push posts into inboxeswrite code
  • createFeedchallenge
  • Keeping the feed fast and freshexplain
  • Posting and readingorder
  • Badge quizquiz
  • System design: unit 3 complete!recap

Continue this lesson free — run real code in your browserright here

Continue with Google

See all 12 lessons of System design