News Feed: First Design
Nothing in this design is computed ahead of time. A post is a row, and between minutes ten and twenty you show that a feed can be worked out at the moment someone asks for it. It is exact, which no later version will be, and all of its work lands on the reader.
You draw it, follow one post and one feed load through it, price it for a small network, and name what gives way as it grows. The interviewer's next questions wait at the end of the page. Commit to an answer on each before Scaling It shows its own.
Two Tables and a Merge
Clients reach stateless app servers behind a load balancer. Behind the servers sits one relational database with two tables. Posts has an index on author and ID. Follows records who follows whom.
Post IDs are the time-ordered 64-bit numbers of Chapter 5's IDs Without Coordination, so sorting by ID is sorting by time, as closely as the machines' clocks agree.
One Post, One Load
An author posts. A server makes a new ID, inserts one row, and answers with the post. That is the whole write path, and every reader can now find the post.
A reader who follows 200 accounts opens the app. A server reads their follow list. It asks the database for the newest posts whose author is in that list, and with the index that is one seek for each followed account. It merges what comes back by ID, keeps 20, and returns them with a cursor. The next page asks the same way, starting below the cursor's ID.
-- once per followed account: one seek SELECT id, author, text FROM posts WHERE author = <followed> ORDER BY id DESC LIMIT 20; -- the server merges the answers by ID and keeps 20
A delete removes the row, and a follow or an unfollow changes one row. The next load sees either one, because nothing has been copied anywhere.
Exact, and Enough for a Million
A post is in the feed the moment it is written, and a deleted post is gone the moment it is deleted. A load is the average 200 seeks, mostly served from memory while the indexes fit. Take a network of a million daily users at the same 4 loads a day. That is 40 loads a second, and it needs nothing more.
This design also shows you where the work is: all of it at read time, per reader, per account followed. Every later change moves some of that work somewhere else.
What the Merge Costs
Leave four limits on the board without fixing any of them. A load's work grows with the number of accounts followed. One node's disk fills in 100 days. The database is one failure. And the merge cannot be split, because the authors of one feed are everywhere.
The first limit has two numbers. At peak, 8,000 loads a second at 200 seeks each is 1.6 million seeks a second. Someone following 2,000 accounts pays 2,000 seeks for one load, and at about 0.1 milliseconds for a read from an NVMe disk, that is 200 milliseconds once those seeks leave memory.
Get that out yourself, and keep it short: "Seeks in memory are fine for now. At 8,000 loads a second and 200 seeks each, that's 1.6 million seeks a second, and someone following two thousand accounts costs 200 milliseconds on disk alone. Their tail latency is what I'd see go first."
Two things stay parked by name. Nothing is being pushed anywhere yet, so where a hybrid would draw its line can wait for the probes. Ranking waits longer, and one sentence holds it: "I'll draw the merge as it stands. Ranking is a box until newest-first works, and we can take it up once the feed is built."
- Skipping this design and starting from a Redis list per user. The interviewer asks what forced it, and the 2,000 seeks were never stated.
- Calling it naive, when it is exact in a way nothing later will be.
- Partitioning the first design by user, which puts the posts on the side of the reader and copies them.
- The merge said in words, on one post and one feed load.
- What this design gets right, its exactness, named as the thing later designs give up.
- The limit given with both the rate and the heavy follower.
Knowledge Check
"A user follows two thousand accounts, and feed loads arrive by the thousand every second. Where does that go wrong?"
- "Move the follows to a graph database, so that reading the follow list gets faster."
- "Partition posts by author, so that each author's posts are together on one node."
- "Build each feed ahead of time: a new post adds its ID to every follower's list."
- "Cache each user's finished page for a minute, and rebuild it when it has expired."
"So you precompute feeds. An account with fifty million followers just posted. What do the next few minutes look like?"
- "Add fan-out workers until fifty million inserts finish within a few seconds."
- "Give large accounts their own queue, so that normal posts are never held up."
- "Fan out only to followers who are online now, and skip all of the other ones."
- "Leave accounts that large out of the fan-out, and merge their posts at read time."
"Say the feed cache covers hundreds of millions of users. What is in one entry, and how big does the whole thing get?"
- "Five hundred post IDs with their authors, about 8 KB a user, and 2.4 TB in all."
- "The twenty newest full posts, about 20 KB a user, which is about 6 TB in all."
- "Every post they could ever scroll to, as IDs, a few megabytes for every user."
- "Only the IDs from the last hour, about 1 KB a user, and refill it on each miss."
"The user is on page three and new posts keep arriving. What does the next page show them?"
- "Page by offset, and let the client drop any repeats it has already shown."
- "Page by a cursor, so the next page starts just after the last post ID they saw."
- "Freeze a copy of the feed when they open the app, and page through that copy."
- "Reload the first page whenever a post arrives, and scroll back to their place."
You got correct