One person posts a photo. Hundreds or thousands of friends should see it at the top of their home page, newest first. The post has to be copied into all of those feeds at some point. You can do the copying when the post is written, or when each friend opens the app. That one choice decides where your system strains.
Key points
Outcomes
Ch. 11 starts the way a real interview starts: with questions before boxes. The interviewer's answers become the requirements. They are numbered here so later sections can say exactly which one a design choice is fighting.
Requirement (4) is the one that hurts. Storing a post is one write. Getting it in front of every friend can mean up to 5,000 more, one per friend, because each friend's feed is a separate list. Requirement (5) multiplies that by every post published across 10 million daily users.
Requirement (3) helps. With no ranking model, a feed is just "posts from my friends, newest first." That keeps the whole problem about moving data, not scoring it.
The API surface is small. Ch. 11 names two HTTP endpoints:
| Action | Request | Params |
|---|---|---|
| Publish a post | POST /v1/me/feed | content (the post text), auth_token |
| Read your feed | GET /v1/me/feed | auth_token |
Picture a club newsletter. One way to run it: the moment an issue is printed, you stuff a copy into every member's mailbox. Members get it instantly when they check their mail. But you printed and delivered copies for members who are away for months and will never look.
The other way: you pin one copy on the clubhouse board. Nothing is delivered. Each member who wants news walks over and reads every board they care about. No wasted copies, but every member spends time gathering, every single time.
The first is fan-out on write, also called push. The second is fan-out on read, also called pull. Fan-out just means one thing spreading to many places: one post, many feeds.
Here is the publish path from Ch. 11's detailed design. The important split: the post service stores the post exactly once, and the fan-out service is the only part that knows about friends.
How to use: press Next to move one hop at a time, or Play to run it. Lit boxes and moving arrows are the work on that step. Follow post p_9012 from one write to three feeds.
POST /v1/me/feed with content=Hello and an auth token.Two details are easy to skip and worth saying out loud in an interview.
The queue. Without it, the author's request would wait while thousands of feed writes finish. With it, the web tier returns as soon as the job is queued, and a burst of posts piles up in the queue instead of stalling the servers people talk to.
What gets copied. Workers do not copy the post. They copy its ID. Ch. 11 describes the news feed cache as a table of <post_id, user_id> pairs, appended to on every new post:
| user_id (whose feed) | post_id |
|---|---|
| amy | p_9012 |
| amy | p_8840 |
| cy | p_9012 |
| dee | p_9012 |
Illustrative rows. Post p_9012 appears three times, but it is three short IDs, not three copies of the text and photo. Ch. 11 also caps how many IDs each feed keeps, because almost nobody scrolls back through thousands of posts. The cap keeps memory small and the miss rate stays low.
The publish path above is push. Now compare it against pull on the same post. Change who is posting and watch where the cost lands.
How to use: pick a model, then drag the follower slider from a normal user toward a celebrity. Drag the second slider to change how many followers actually open the app today. Watch the three counters and the dot grid.
work done before anyone reads
copies for people who never look
page loads that must fetch this author
Three things to notice while you play with it:
Ch. 11 picks a hybrid, and the reasoning is the part to repeat: fetching the feed fast matters most, so most users get push. For celebrities and users with very large followings, followers pull that author's posts on demand, so one post cannot overload the system.
At read time, a reader's feed becomes: the pushed IDs already in their cache, merged with recent posts pulled from the few high-follower accounts they follow, sorted newest first.
Ch. 11 also names consistent hashing as a help for the hotkey problem: it spreads cached data and requests more evenly across cache servers, so no single box takes all the load.
The read path never touches the fan-out machinery. It reads a short list of IDs and then turns each ID into something a person can look at. That second step is hydration, and it is where most candidates go quiet.
How to use: press Next to walk one GET /v1/me/feed through the system.
GET /v1/me/feed with its auth token.Ch. 11 calls the cache tier extremely important and splits it into five layers. Each one holds a different kind of data with a different read pattern.
| Layer | What it stores | Which path leans on it |
|---|---|---|
| News feed | IDs of news feed posts | every feed read, step 4 |
| Content | every post's data; popular posts in a hot cache | hydration, step 5 |
| Social graph | follower and following relationships | every publish, when fan-out looks up friends |
| Action | whether a user liked, replied to, or acted on a post | rendering each post's state for this reader |
| Counters | like, reply, follower, following counts | rendering, and updated on every action |
A complete, runnable Python 3 model of the hybrid. Publishing enqueues and returns. A worker copies IDs into followers' capped feeds. Authors above a follower threshold skip the queue, and readers pull their posts at read time. The threshold is set tiny so the demo shows both paths.
# fanout.py: hybrid news feed in one process from collections import defaultdict, deque import itertools FEED_CAP = 3 # IDs kept per feed (Ch. 11: a configurable limit) CELEB_THRESHOLD = 3 # more followers than this: pulled, not pushed clock = itertools.count(1) # increasing id doubles as "newer" posts = {} # post_id -> (author, text): the one real copy by_author = defaultdict(list) # author -> post ids, oldest first followers = defaultdict(set) # author -> users who follow them feed_cache = defaultdict(deque) # user -> post ids, newest first queue = deque() # fan-out jobs: (post_id, author) def follow(user, author): followers[author].add(user) def is_celebrity(author): return len(followers[author]) > CELEB_THRESHOLD def publish(author, text): pid = next(clock) posts[pid] = (author, text) by_author[author].append(pid) if not is_celebrity(author): queue.append((pid, author)) # enqueue, return right away return pid def fanout_worker(): while queue: pid, author = queue.popleft() for user in followers[author]: feed = feed_cache[user] feed.appendleft(pid) # copy the ID, never the post while len(feed) > FEED_CAP: feed.pop() # the cap keeps memory bounded def read_feed(user): ids = set(feed_cache[user]) # pushed IDs, already waiting for author, fans in followers.items(): if user in fans and is_celebrity(author): ids.update(by_author[author][-FEED_CAP:]) # pull at read time newest = sorted(ids, reverse=True)[:FEED_CAP] return [f"{posts[p][0]}: {posts[p][1]}" for p in newest] # hydrate # bo is an ordinary user; star has 4 followers, over the threshold follow("amy", "bo") for fan in ("amy", "cy", "dee", "eli"): follow(fan, "star") publish("bo", "lunch?") publish("star", "tour dates are out") publish("bo", "never mind, ate already") fanout_worker() print("amy sees:", read_feed("amy")) print("feed entries holding star's post:", sum(2 in f for f in feed_cache.values()))
Output
amy sees: ['bo: never mind, ate already', 'star: tour dates are out', 'bo: lunch?']
feed entries holding star's post: 0
The lines that matter:
queue.append((pid, author)) is the decoupling. publish returns before a single feed is touched.if not is_celebrity(author) is the whole hybrid in one condition. A high-follower author never enters the queue.feed.appendleft(pid) writes an ID, not a post. That is the memory argument from section 03.ids.update(by_author[author][-FEED_CAP:]) is the pull side: the reader fetches the celebrity's recent posts at read time.Ch. 11 lists the common variants of this interview question. They share the same skeleton but stress different requirements, so the push and pull balance shifts.
| Prompt | Why it is the same problem | What to emphasize |
|---|---|---|
| Facebook news feed | The source of Ch. 11's definition: status updates, photos, videos, links, and likes from people, pages, and groups you follow. | Mutual friendships with a cap, so most authors are safely pushable. Pages are where large followings appear. |
| Instagram feed | Same two flows: publish a post, read a feed of people you follow. | Requirement (6) dominates. Media goes to a CDN; the feed only carries references. |
| Twitter timeline | Same fan-out decision: copy each tweet to followers or gather at read time. | Following is one-directional, so follower counts are not capped the way friends are. The celebrity path matters more. |
Q1Under pure push, an author with 2 million followers posts once. Only 5% of those followers open the app that day. What did that post cost?
Push writes to every follower at publish time, whether or not they show up. 95% of 2 million is about 1.9 million wasted copies.
Q2A teammate wants to store full post objects in the news feed cache so reads skip hydration. What breaks first?
The cap limits how many entries each feed keeps, not how big each entry is. IDs keep every entry tiny.
Q3Feed reads are fine for most users but slow for people who follow dozens of celebrities. Which part of the hybrid explains it?
Celebrity posts are never pushed. Their followers pay for them on every read, so the more celebrities you follow, the more each read gathers.
Q4The web tier stalls during a spike of new posts, even though the feed cache is healthy. What is the most likely missing piece?
Without the queue, each publish request waits for all its feed writes to finish, so a burst of posts ties up web servers.