BOOKS DEEP-READ · SYSTEM DESIGN INTERVIEW VOL 1 · CH 11

Design a News Feed System

System Design Interview Vol 1 · Ch 11 · Alex Xu · 2020

中文 →

What is this chapter about?

Open any social app and scroll. That endless column of things your friends posted is the news feed. Chapter 11 asks you to build one. It looks like nothing more than "sort your friends' posts by time", and it is in fact the book's best training ground for one instinct: the same work can be done when someone posts or when someone looks, the cost differs by tens of times, and neither side is always the cheaper one.

A comparison: is the newspaper printed in advance, or assembled when you walk in?

Think of each person's feed as a personal newspaper. There are two ways to make it.

One: print ahead. The moment anyone posts, you run around and add a line to the newspaper of every single person who follows them. When you wake up, your paper is already printed and waiting. You pick it up and go.

Two: assemble on arrival. You do nothing in advance. The instant a reader shows up, an editor runs to each person they follow, copies down the newest few items, and staples a paper together on the spot.

Why the obvious way collapses

"Assemble on arrival" is the easier thing to build and the first thing to break. If you follow two hundred people, every refresh means two hundred errands — and if just one of them is slow, you wait. The time to assemble a paper is set by the slowest errand, not by the average one. A refresh that takes several seconds is a product nobody keeps using.

The core idea: move the work to a moment when nobody is waiting

So the mainstream answer is print ahead. You post, and the system immediately looks up your friend list and adds a line to each of their papers. Reading becomes a single action: pick up the copy that is already printed. Refresh feels instant not because anything got faster, but because the work was moved to the moment you hit "post".

Except for one very expensive case

Printing ahead has a soft spot: the number of copies equals the number of followers. An account with a hundred million followers posts once, and the system must add a line to a hundred million papers. Even printing a hundred thousand copies a second, that is over a quarter of an hour. Worse, only a few million of those people will actually look today — the rest were printed for nobody.

So real systems split people by who did the posting. Ordinary accounts: print ahead. Huge accounts: don't print, assemble on arrival — because the number of huge accounts any one person follows is a few dozen at most, and a few dozen errands is a bargain. What you see is the pre-printed stack plus the freshly fetched celebrity items, shuffled together by time.

The honest cost: printing ahead means the same post gets copied hundreds or thousands of times inside your system, the write volume and storage go up accordingly, and the vast majority of those copies will never be read by anyone.

One line to remember

Every design decision in a news feed answers one question: do you assemble the paper when somebody posts, or when somebody looks? The first reads instantly but writes brutally; the second writes cheaply but reads painfully. The answer is not to pick one — it is to split by publisher: print ahead for ordinary accounts, assemble on the fly for the huge ones, and merge the two at the end.

Want the fan-out strategies, the cache layers and the real numbers? → Switch to the deep read