Courses System design · Part 6 · Case study 4
Case Study: A News Feed
In a social network, each user’s home page is the list of new posts from the people they follow. The main question of this design is one sentence: when do we build that list? When someone posts, or when someone opens the page?
1. Requirements
Functional:
- A user posts (text, and a link to a picture).
- A user sees their feed: posts from the people they follow, newest first, page by page.
Non-functional:
- The feed opens fast: under 200 milliseconds.
- A new post may appear in others’ feeds a few seconds late. A little staleness is fine (in the CAP lesson we saw this data is AP).
- Smart ranking, recommendations and ads are out of scope.
2. Numbers
- 10 million daily active users. Each follows 200 people on average.
- Each user posts twice a day: 20 million posts a day, about 230 posts a second.
- Each user opens the feed 10 times a day: 100 million times a day, about 1,200 a second, perhaps 5,000 at the peak.
The result: feed reads are five times the posts, and every read must gather posts from 200 people. Doing that at read time is expensive.
3. API
POST /posts {text, imageUrl?}
GET /feed?cursor=... → {posts: [...], nextCursor}
cursor is the ID of the last post the user saw; the next page is the posts older than it. Because IDs are in time order, this is simple.
4. Data model
- Posts:
(postId, authorId, text, createdAt), sharded byauthorId. - Follows:
(followerId, followeeId), with two indexes: “who do I follow” and “who follows me”. - Ready-made feeds: for each user, the list of the IDs of the last 500 posts in their feed, in a cache in memory.
5. High-level picture and deep dive: two ways to build the feed
Way 1: build it when reading (pull)
When a user opens the feed: read the 200 people they follow, fetch each one’s new posts, merge and sort.
- Good: posting is cheap, just one row.
- Bad: each feed open is 200 reads from different shards. At the peak, 5,000 × 200 = a million reads a second. Very expensive and slow.
Way 2: build it when writing (push, or fan-out on write)
When someone posts, the post ID is added to the ready-made feed of every follower. Opening the feed is just reading one ready list from the cache.
- Good: reads are very fast and cheap.
- Bad: each post means 200 writes: 230 × 200 = 46,000 writes a second to the cache. A lot, but possible for an in-memory cache, and it can go through a queue so workers do it at their own pace. A post then appears in feeds a few seconds late, which we accepted.
The famous account problem
Now a famous singer with 5 million followers posts. With way 2, that is 5 million writes for one post. The queue fills up, and everyone else’s posts wait behind it. This is the hot key problem again.
The common fix is to mix the two ways:
- For ordinary users (say, under 10,000 followers): build when writing.
- For famous accounts: no fan-out. When a user opens the feed, the new posts of the few famous accounts they follow are read separately and merged with the ready-made feed.
A user follows maybe 10 famous accounts, so the merge is only 10 extra reads, and famous accounts’ posts are read so often that they are always in the cache.
Other details
- Post content is kept apart from the feed: the feed holds only IDs, and the post text comes from a post cache. If a post is edited or deleted, it changes in one place only.
- Inactive users: for someone who has not come back for a month, we keep no ready-made feed. When they return, it is built once with way 1. This saves a lot of cache memory.
- Pictures come from a CDN, not from the feed servers.
6. Trade-offs
- Cheap writes or cheap reads: with these numbers reads are five times more, so making reads cheap wins, except for famous accounts.
- Freshness: a post may arrive a few seconds late. Fine for a social network; not for a trading system.
- Memory: 10 million users × 500 IDs × 8 bytes = 40 GB. Large but possible, especially once inactive users are dropped.
Labs used
Cache · Message queue · Unique IDs · Backpressure · Consistent hashing