Design a news feed like Facebook's
hard 50 minShow each person the best recent posts from everyone they follow, in a fifth of a second.
See it run
Every flow of the design, one hop at a time: open the feed, create a post, choose push or pull, rank, rebuild a lost feed and send hints.
Operations
Open the feed (read path)
Take the ready feed, merge big accounts, filter, rank once, and turn 20 IDs into posts.
Step 1 of 11
Ask for the feed
request
page 1
The app sends GET /v1/feed. Photos will come from the CDN; this personal reply never does. Why: no two people share it.
Step 1: Ask for the feed
def open_feed(viewer): ids = redis.newest(f"feed:{viewer}", 500) ids |= big_accounts.recent(graph.following(viewer)) ids |= posts.recent_by(viewer) # own posts top = light_pass(ids, viewer)[:100] seen = [h for h in posts.headers(top) if may_see(viewer, h)] order = rank(viewer, seen, limit_ms=60) # or newest first sid = sessions.save(viewer, order, ttl=600) return posts.full(order[:20]), cursor(sid, 20)- Latency
- 200 ms p99
- Load
- 231,000/s
- Peak (3x)
- 694,000/s
- Posts looked up
- 4.6M/s
Page 2 repeats only the last line and the filter: the order is already stored.
In the real world
The same news feed doing real jobs. Pick one and step through it.
The spike: one post, a hundred million possible readers.
Step 1 of 4
No push
inserts
0 feeds
The account is far above the limit. One event, one entry in each feed server.
Step 1: No push
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Fan-out before answering
criticalPosting waits for hundreds of writes, and a failure in the middle delivers to only some friends.
Wrongfor f in followers: redis.add(f, pid)return 201Rightdb.store(pid, change=True)return 201 # workers deliver laterPush for every author
criticalOne post by a star becomes 100 million inserts and everybody else's posts wait.
Wrongpush_to_all(author)Rightpush(author) if followers <= 10_000 else big_accounts.add(post)Trusting the cached feed for privacy
criticalIDs stay behind after blocks, deletions and audience changes.
Wrongreturn posts.full(redis.feed(viewer))Righths = posts.headers(ids)return [h for h in hs if may_see(viewer, h)]Calling a pipeline atomic
majorA pipeline saves round trips. Some commands can succeed and others fail, so the batch must be safe to repeat.
Wrongpipe.lpush(feed, pid) # repeated twice after a retryRightpipe.zadd(feed, time, pid) # the same ID again changes nothingMore workers than partitions
majorIn a consumer group one partition has one consumer. The others are idle.
Wrongpartitions = 256; workers = 1000Rightpartitions = 1024; workers = 694
Interview tips
Lead with two products
- Posts × followers = 2.3M a second.
- Reads × follows = 116M a second.
- Fifty times: push.
Then break it yourself
- 100 million followers.
- Keep one copy, 2 GB for all big accounts.
- Merge, drop duplicates.
Find the hidden reads
- 20 posts a page: 4.6M lookups a second.
- A post cache at 99%.
- Score 100, not 500.
Finish with trust
- Filter at read.
- Newest first when ranking fails.
- Never an empty feed.