What a feed has to do
After this: Write down what a feed must do and work out posts and feed loads per second for 10 million people.
On this page · 7 sections · 2 checks
You open a social app, and the first screen is a list of recent posts from the people you follow, newest at the top. Ten million other people open the same app today, and each of them sees a different list. How does the app put together the right list for each person, fast?
This tutorial designs that service from the first question to the final diagram, as the URL shortener tutorial did. The last lesson then shows how a system design interview runs from start to finish.
What a feed is
A is the list of recent posts from the accounts a person follows, newest first. When Ben follows Ana, Ben is one of Ana's followers, and Ana's posts belong in Ben's feed.
Here is the smallest example. Ben follows Ana and Cy.
- Ana posts at 9:00.
- Cy posts at 9:05.
- Ben opens the app at 9:10. His feed shows Cy's post first, because it is newer, then Ana's.
Start with questions
Before drawing anything, write down what the service must do and how big it is. That list is the . For our feed we agree on these:
- What it does. A person can post, follow other accounts, and open a feed of the posts of the accounts they follow, newest first.
- How big. 10 million people use it each day. Each makes 0.5 posts a day (one post every two days) and opens the feed 10 times a day. A person has 200 followers on average, and follows 200 accounts on average.
- How fresh. A new post may reach followers' feeds a few seconds late. For a feed, that delay is acceptable.
- Out of scope. Ranking posts by interest, likes, comments, photos and ads. Each could be added later; leaving them out keeps the design about one idea.
From people to requests per second
In this learning path the useful numbers are per second, because what a server can do is counted per second. A day has 24 × 60 × 60 = 86,400 seconds. Work out the two kinds of request:
- Posts. 10 million people × 0.5 posts = 5 million posts a day. 5,000,000 ÷ 86,400 is about 57.9 posts per second.
- Feed loads. 10 million people × 10 loads = 100 million loads a day. 100,000,000 ÷ 86,400 is about 1,157 loads per second.
- The ratio. 100 million loads against 5 million posts is 20 loads for every post. Reading feeds is most of the work.
Interviewers call requests per second , for queries per second. This tutorial keeps the plain words.
| What | Per day | Per second |
|---|---|---|
| New posts | 5 million | about 57.9 |
| Feed loads | 100 million | about 1,157 |
These are averages. Busy hours bring more, and Estimating: back-of-the-envelope numbers assumed the busiest time is 2 × the average. Here the averages are enough to compare designs.
Run it
The same sums as a short program. Java does not run here: the Run button runs the Python version, and the Java version is shown for reading.
users // 2users / 2 is half a post per person, kept as a whole number, and users * 10 is ten loads per person. Each result is divided by the seconds in a day and rounded. Change the loads per person to 20 and run it again: the loads per second double, and the posts stay the same.
The question the rest of the tutorial answers
Ben's feed can be built at two moments. It can be built ahead, when someone he follows posts: then opening it is quick. Or it can be built on demand, when Ben opens it: then posting is quick. Lesson 2 builds feeds ahead, lesson 3 builds them on demand, and lesson 4 works out which one wins with the numbers above, and why the answer is "both".
Check yourself
10 million people use the app each day, and each opens the feed 10 times a day. About how many feed loads arrive per second, on average?
Each person makes 0.5 posts and 10 feed loads a day. For every new post, how many feed loads are there?
Honesty notes
- Say what a news feed is: the recent posts of the accounts a person follows, newest first.
- Write down the requirements of a feed before designing it: what it must do, for how many people, and how fresh it must be.
- Work out posts and feed loads per second from daily users: 5 million posts a day is about 57.9 per second, 100 million loads a day about 1,157.
- Say that reading feeds is most of the work here: 20 feed loads for every new post.