SkillSprout
Chapter 36 · Lesson 3
Advanced
4 min read

Capstone: Design a News Feed

Overview

A news feed shows each user a fresh, ranked list of posts from the people they follow. Reading the feed is far more common than posting, so the design is about doing the work at the right moment. It combines caching, queues, partitioning and CDNs.

Detailed Notes

What is the task

Design the home feed of a social app: users publish posts, follow other users, and open a feed of recent posts from the people they follow.

The problem it solves

Building a feed on demand means collecting posts from hundreds of accounts, sorting them and returning the result in a fraction of a second. Doing that for every open of the app, for millions of users, overwhelms any database.

How it works: follow these steps

  1. Requirements. Functional: publish a post, follow or unfollow, view the feed (newest first, or ranked), with media. Non-functional: feed loads in a few hundred milliseconds, high availability, and it is fine if a new post shows up a few seconds late (eventual consistency, so we favour availability).
  2. Estimate. Say 300 million daily users open the feed 5 times a day: 1.5 billion reads a day, about 17,000 a second. If 100 million posts are made a day, that is about 1,150 writes a second. At 1 KB of text per post, about 100 GB a day of posts; photos and video go to object storage behind a CDN. Reads outnumber writes roughly 15 to 1, so optimise reads.
  3. High-level design. A post service stores posts in a database partitioned by user id. A graph store (or table) keeps who follows whom. A feed service builds and serves feeds, and a cache holds ready-made feeds. The API is simple: POST /posts and GET /feed?cursor=... (cursor-based pagination, so new posts do not shift pages).
  4. Deep dive: fan-out. Two ways to build a feed:
    • Fan-out on read (pull): when you open the app, fetch recent posts from everyone you follow and merge them. Writes are cheap, reads are slow.
    • Fan-out on write (push): when someone posts, a queue and workers copy the post id into each follower's cached feed list. Reads become one fast cache lookup.
  5. Hybrid. Push has a problem with celebrities: one post by an account with 50 million followers means 50 million cache writes. So push for normal users, and pull celebrity posts at read time and merge them in.
  6. Ranking and media. Store only post ids in the feed cache and fetch the post details in one batch. A ranking step can reorder by recency and engagement. Images and video are served by the CDN.

A simple example

Asha has 200 followers and posts a photo. The post service saves it and puts an event on a queue. Workers read Asha's follower list and add the post id to each follower's feed cache. When Ben opens the app, the feed service reads his cached list, loads the 20 newest post bodies, merges in posts from any celebrity he follows, and returns them with a cursor for the next page.

System design example

text
Client -> Load balancer -> Post service -> Post database (partitioned by user id)
Post service -> Queue -> Fan-out workers -> Feed cache (user id -> list of post ids)
Client -> Load balancer -> Feed service -> Feed cache + Post cache -> (merge celebrity posts)
Media -> object storage -> CDN

With 200 followers on average, push means about 1,150 x 200 = 230,000 feed-cache writes a second. That is large but spread across many workers and cache shards, and it keeps reads cheap.

Bottlenecks and fixes:

  • Celebrity posts: hybrid pull, as above.
  • Slow fan-out: the queue absorbs bursts; workers scale out.
  • Inactive users: skip pushing to people who have not opened the app in weeks, and build their feed on demand when they return.
  • Cache loss: replicate the feed cache and rebuild from the database if needed.

Trade-offs to mention: push uses more storage and write work but gives fast reads; pull is simpler but slow reads; the hybrid adds complexity. Feeds are eventually consistent, so a post may appear a few seconds late.

Summary

  • Reads dominate, so precompute feeds and cache them.
  • Push for normal users, pull for celebrities (hybrid).
  • Use a queue so fan-out never slows down posting.
  • Store ids in the feed, fetch bodies in batches, serve media from a CDN.

Architecture Diagram

News feed architecture
Architecture
Author
Post service
Queue
Fan-out workers
Feed cache
Reader

Posts are fanned out through a queue into per-user feed caches.

  1. 1A new post is saved, then an event goes on a queue.
  2. 2Workers add the post id to each follower's cached feed.
  3. 3Celebrity posts are skipped here and pulled at read time.
  4. 4The reader gets ids from the cache, then post bodies in one batch.

Common Interview Questions

Finished this lesson?

0 of 91 lessons done · 0% of the course