News Feed: Scope
The question gives you no rates, and a feed is decided by the ratio of two of them. You have until minute ten to get that ratio, the size of the largest account, and the lag the product will accept, because every box you draw later spends one of those three. Five questions get them. After that come the agreement, the sums and the API.
What to Ask About a Feed
Each of these moves something on the board. The second and the third move the most.
- Newest first, or ranked? Ranking changes what the cache holds and how a page is defined. Agreed: newest first, with ranking parked for a curveball.
- How many people open it, how often, and how many posts go in? The two rates and their ratio decide whether the work happens on reading or on writing. Agreed: 100 million daily users, 4 feed loads a day each, and 20 million posts a day.
- How many accounts does a person follow, and how large does a following get? The average sets the normal cost, and the largest sets the problem. Agreed: 300 million accounts, an average of 200 follows, and following is one-way, so the largest account has 50 million followers.
- How soon must a post show in a follower's feed, and in the author's own? The answer allows asynchronous work. Agreed: within a minute for followers, at once for the author.
- Are posts text, or media too? Media moves bytes through a different system. Agreed: up to 280 characters plus links to media. The media itself sits in an object store behind a CDN, as in Chapter 2's The Edge: CDNs as a Black Box, and is out of scope.
The third question is really two, and candidates who ask it as one get an average back. Ask for the extreme by name: "I've got the average. What's the largest following on the network, and can anyone follow an account without being followed back?"
What Is In, What Is Out
Four things are functional: publish a post, follow and unfollow, read the feed newest first a page at a time, and delete a post.
Four more are non-functional. A feed load is fast, and it is the one thing that must almost never fail. A post is never lost once accepted. A feed may lag a post by up to a minute. A scrolling reader sees no repeats.
Name what is out before you draw anything, so the interviewer can object now: ranking, media handling, likes and comments, search, and recommending accounts nobody follows.
The Ratio and the Other Sums
Start with the reads. A hundred million users at 4 loads a day is 400 million loads a day. With about 100,000 seconds in a day, that is 4,000 a second. Assume the peak doubles it, to 8,000.
Now the posts. Twenty million a day is 200 a second, and 400 at peak. So there are 20 loads for every post, and the first choice in this design turns on that number. A job done once per post runs a twentieth as often as a job done once per load.
Storage comes next. A post is at most 280 characters, which is 1,120 bytes in the worst case in UTF-8, and most are far shorter. With metadata, assume 1 KB. Twenty million posts a day is then 20 GB a day. Five years of that is about 37 TB, and three copies make about 110 TB. At an assumed 2 TB a node, that takes 55 nodes. One node holding one copy fills in 100 days, so a single database has an end date, and you say which.
The follow graph sounds frightening until you size it. Three hundred million accounts at 200 follows is 60 billion edges. At 16 bytes an edge, that is about 1 TB one way. Storing it both ways, who a person follows and who follows them, makes 2 TB, and three copies make 6 TB. That is three nodes, and it decides little.
Twenty posts at 1 KB is 20 KB a load. Eight thousand loads a second makes 160 MB a second, and a 10-gigabit link carries 1.25 GB a second. Nothing hangs on bandwidth, and that is all you say about it.
API and Entities
A POST to the posts endpoint publishes and returns the post with its ID. A GET on the feed with a limit of 20 returns a page and a cursor, and the same GET with that cursor, passed as after, returns the next page down. A third form passes since and the newest ID on screen, and asks only for what has arrived above it.
A PUT and a DELETE on an account under follows change the graph. A DELETE on a post removes it.
POST /posts {"text": "...", "media": ["https://..."]}
201 {"id": "7312845190264832", "text": "..."}
GET /feed?limit=20
200 {"posts": [...], "cursor": "7312845190264832"}
GET /feed?limit=20&after=<cursor> the next page down
GET /feed?since=<newest id> only what is above it
PUT /follows/{account} 204
DELETE /follows/{account} 204
DELETE /posts/{id} 204
There are two entities. A post has a time-ordered ID, an author, its text and its media links. A follow is a follower and the account they follow. The cursor is the same choice Chapter 1's API and Data Before Boxes makes: this list changes while it is being read, so the call never takes a page number.
- Saying "fan-out" in minute two. The interviewer asks which rates it rests on, and none are agreed.
- Assuming friends rather than followers. Mutual follows cap the largest account, and that removes the hard part without anyone noticing.
- Working out media bytes, when a CDN carries them and you have just ruled them out.
- The ratio of 20 loads per post worked out before anything is proposed.
- The largest following asked for as a separate question from the average.
- "Up to a minute" settled as a number the design is allowed to spend.
Knowledge Check
You get one question before you draw. Which answer moves this design the most?
- Whether a post can be edited after it has been published
- How many posts go in a day, against how many feeds get loaded
- Whether posts can carry links to media as well as their text
- How long a deleted post has to be kept before it is purged
Loads outnumber posts by 20 to 1. What does that ratio suggest before anything is drawn?
- Reading from replicas, since the traffic here is nearly all reads
- Leaving the writes out of the design, since there are so few
- Doing the work once per post, which runs far less often than per load
- Caching each post on first read, with a long expiry on every copy
The interviewer says following is one-way. What does that do to the hard part?
- It halves the follow graph, since only one direction gets stored
- It caps the largest account at about the size of a friends list
- It removes the need to read a follow list on every single load
- It lets one account gather 50 million followers for each of its posts
Which estimate can you state in a sentence and then drop, because nothing hangs on it?
- Bandwidth, about 160 MB a second at peak on a 10 Gbps link
- Storage, about 110 TB in five years, counting all three copies
- The ratio, about 20 feed loads for each post that goes in
- The largest following, about 50 million on just one account
You got correct