AlgoViz

Design a news feed like Facebook's

hard 50 min

Show each person the best recent posts from everyone they follow, in a fifth of a second.

Video lesson · Chapter 1 of 5

A feed opens for just one reader

0:00 / 3:55

Theory

Read or listen first. About 2 min.

What it is

A news feed is a list of recent posts from the people, pages and groups you follow, put in an order that suits you. Opening the app asks one question: of everything these accounts wrote, which twenty posts should I see now?

Two actions meet here. Writing a post is rare and small. Reading a feed is frequent and touches hundreds of accounts. The design is about moving work from the frequent action to the rare one, without letting one famous writer break it.

Why it exists

Nobody wants to visit 500 profiles to find out what is new. A feed gathers it in one place, so it becomes the first screen people see and the busiest read path in the product.

It is asked often in interviews because a simple model (posts, follows, sort by time) turns into real trade-offs very fast: copy data early or gather it late, be fresh or be fast, be clever or be available.

A short history

  1. 2006

    Facebook redesigns its pages and introduces News Feed in September. Some users protest at seeing every friend's activity in one list.

  2. 2010

    The paper "Feeding Frenzy" (SIGMOD) shows how to choose push or pull separately for each pair of producer and consumer.

  3. 2011

    Facebook changes the feed from strictly newest-first to a mix of top stories and most recent stories.

  4. 2012 to 2013

    Raffi Krikorian's QCon talk "Timelines at Scale" describes how Twitter delivers posts into ready-made timelines. InfoQ publishes the recording in April 2013.

  5. 2013

    Facebook publishes TAO, its store for the social graph, at USENIX ATC. The paper reports a billion reads a second.

  6. 2021

    Meta's engineering blog describes ranking in passes: a light model keeps about 500 posts, a heavier one scores them, a last pass adds diversity.

Words to know

Fan-out
Turning one post into many deliveries.
Push (fan-out on write)
Copy the post's ID into each follower's ready feed when it is written.
Pull (fan-out on read)
Gather posts from followed accounts when the feed is opened.
Ready feed
A stored list of post IDs for one person, built before they ask.
Hydration
Replacing IDs with full posts: text, names, pictures, counts.
Candidate
A post that may be shown, before ranking decides.
Big account
An account above a follower limit, whose posts are pulled, not pushed.
Read your own writes
You see your own post at once, even when others do not yet.

Solve it like an interview

New to it? Start with Approach. Revising? Go straight to the mock.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

Understand it

Picture it

The paper round and the notice wall

Two ways to spread the news
Everyone walks to the wall

Who walks: the paper carrier or the reader? A good design uses each where it is cheap.

Scope

What are we building?

A service like Facebook's news feed: people post, friends and followers see it in a personal, ranked, endless list. Similar products share the shape, each with one twist. The company's real system is not described here, except where we link its own engineering pages.

Similar applications

ProductThe twist
X (Twitter)Timeline of short posts; one-way follows, so follower counts are very uneven
InstagramPictures first, so media delivery dominates
LinkedInProfessional posts; connections of connections matter
TikTokMostly recommendations, little follow graph
RedditCommunities, not people: one feed per community is shared by many
YouTubeHome feed of long videos
PinterestSaved pictures grouped by interest
ThreadsShort text posts with a follow graph

Why it is interesting

  • Fan-out

    One post may become 500 deliveries, or 100 million.

  • Read-heavy

    About 50 feed requests for every post.

  • Personal

    No two people get the same list, so the CDN cannot cache it.

  • Private

    Copies travel ahead of permission changes. Reads must check.

The finished system

Where the design ends up

This is the last version, not the first. Design starts with one service and one database and names the break that forces each box.

The whole system

People reach a CDN for photos and video, and a gateway for everything else. Reads go to the feed service, which holds recent posts of big accounts in memory, reads ready feeds from Redis, asks the ranking service and reads posts from a post cache in front of Cassandra. Writes go to the post service, Cassandra and Kafka; fan-out workers ask the follow graph and write into Redis; a hint service tells connected people that new posts exist. Photos are stored in S3.
Blue is reading, pink is writing, orange happens later. Dashed means asynchronous or only on a miss.

Where to read what

QuestionInterview tabConcept page that owns the idea
What must it do?ScopeScoping
How big is it?NumbersBack of the envelope
What is the interface?API and dataPagination, file uploads
How does it grow?DesignPush and pull, pub-sub
What is hard?Deep divesThundering herd, real-time APIs
Why this choice?ExplainChoosing and defending
Can I do it aloud?Mock45 minutes

Summary

The whole topic in one minute.

A feed reads far more than it writes: here 231,000 feed requests a second against 4,600 posts. So we do the work when a post is written. Workers copy its ID into the ready feed of every follower, and a read becomes one lookup.

Copying breaks for accounts with millions of followers, so their posts are kept once and merged in at read time. Ranking chooses the order, a post cache turns IDs into posts, and every read checks again that the viewer may see each post, because a copied ID is never permission.

  • Push costs posts times followers; pull costs reads times follows. Compare the two numbers before choosing.
  • Store IDs in ready feeds, full posts once.
  • Big accounts are few, so their recent posts fit in every feed server's memory.
  • Rank once for each visit and page through that result, or posts will repeat.
  • If ranking fails, show newest first. Never show nothing.
  • Check deletion, blocks and audience at read time, every time.

Sources