Lesson 3 · Minutes 20-30

News Feed: Scaling It

Problem

The 1.6 million seeks from First Design are still on the board at minute twenty, and the next ten minutes move them. The interviewer starts with the reader who follows thousands, and asks who should be doing that work. Once the work has moved to the moment of posting, they hand you an account with fifty million followers. Then they want to know what all those precomputed lists weigh in memory. They finish on a reader deep in the feed while new posts land overhead.

Every box you add from here points back to one of those four. The figure at the end of this lesson is First Design's, with those boxes marked.

Two Thousand Follows

"That's thousands of seeks for a single load. Whose cost is this, and can someone else pay it?" The author can pay it. Scope's ratio says why: with 20 loads for every post, work done once per post runs a twentieth as often. That is fan-out on write: each post's ID is copied out to its followers, so the merge is done before anyone asks.

A post still goes into the post store, and from there onto a log, the append-only sequence of Chapter 6's Queues, Logs and Pub-Sub. Workers read the log. For each post, a worker reads the author's followers from the follow graph and cuts them into batches, each its own message on the log, so any worker can take one and the whole fleet can share a large post. A worker adds the post's ID to each follower's list in its batch. A load then reads one list.

Now count both sides. Twenty million posts at 200 followers is 4 billion inserts a day. That is 40,000 a second, and 80,000 at peak. Reading instead would be 400 million loads at 200 seeks, which is 80 billion a day. That is 20 times as much, and 20 is Scope's ratio coming back. Assume the fan-out fleet, all the workers together, does 200,000 inserts a second, so 80,000 leaves it room.

This path must not lose a post. The server writes the post and a message in one local transaction, and a relay moves the message onto the log. That is the outbox of Chapter 5's Transactions Across Partitions, and it means a post the store accepted cannot miss the fan-out. If the log is down, the post is safe and the fan-out waits. A worker that dies leaves its offset, its place in the log, behind, and another worker in its group carries on from there. It adds some IDs a second time, which changes nothing because each list is kept sorted by ID and holds an ID once, the idempotent write of Chapter 6's Delivery Semantics at Scale. An offset is enough here. Chapter 15 tracks each message because a repeat there is a second text, and a repeat here changes nothing.

What it costs: a post reaches followers seconds late, inside the agreed minute. You now have a second copy of the feed to keep. And the first design's exactness is gone.

Fifty Million Followers

"Your biggest account posts. How long until the last follower has it, and what else waits?" Fifty million inserts at 200,000 a second is 250 seconds. That is about four minutes of the whole fleet, four times the agreed minute, and every other post waits behind it.

It is not one account either, and the 40,000 you just counted assumed every post reached the average 200. Assume 1,000 accounts above a million followers, averaging 3 million, posting 5 times a day. That is 15 billion inserts a day, or 150,000 a second. About four fifths of the work now comes from a thousand accounts, and on average the fleet has almost nothing spare.

This is not the hot key of Chapter 3's Hot Partitions, one key stuck on one partition. These inserts land on lists spread across every node, so no partition is hot. The fleet is what runs out.

So accounts past a line stay out of the fan-out. Each one keeps its own list of recent post IDs, trimmed to 500 like everyone's, 8 MB in all, and a load merges those lists into the reader's. That is the hybrid, and now it needs a number.

A whole minute of the fleet is 12 million followers, and that is too high, because the fleet is shared and the minute has to cover everyone's posts. With an average of 3 million, most of those accounts sit below 12 million and would keep most of the 150,000. A million followers is 5 seconds of the fleet, which leaves nearly all of the minute for everyone else. So the line goes there. In one breath: "I'd push below a million followers and pull above it. A million is 5 seconds of the fleet, and the thousand accounts above it are four fifths of the work."

A load has to know which lists to merge. Each feed server keeps the IDs of accounts past the line in memory, about a thousand of them, and it reads the reader's follows from the follow graph. That is one more read per load: 8,000 a second at peak, about 2,700 on each of the graph's 3 nodes. If the graph cannot answer, the page goes out without the large accounts' posts and without the unfollow filter rather than fail.

Those accounts hold 3 billion of the 60 billion edges. Over 300 million accounts, that is 10 for each reader, so a peak load adds 10 list reads. That makes 80,000 a second. Assume a cache node runs at half of its 100,000 simple operations a second, and 2 nodes carry it.

What it costs: the read path merges again, on a much smaller scale. The line has a trigger in each direction. Lower it when fan-out falls behind the minute, which the age of the oldest post waiting on the fan-out log shows, and raise it when merges take too long.

What the Cache Holds

"How much memory does this cost me, and what do I get for it?" One user's entry holds IDs: an 8-byte post ID and an 8-byte author ID, 16 bytes an item, trimmed to the newest 500. That is 8 KB a user. For 300 million accounts, it is 2.4 TB before overhead. At 100 GB a node that takes 24 nodes, and that is a floor because it fills each one. Replicas double it.

Memory decides here, not the rate. At peak, 80,000 inserts over 24 nodes is about 3,300 a second on each. And 500 reach back a long way. Four billion inserts a day over 300 million accounts is about 13 IDs a day, so a list covers more than a month, and about 4 days for someone following 2,000.

Whole posts would be 500 KB a user. That comes to 150 TB and 1,500 nodes, and an edit would rewrite millions of copies.

A cache of IDs against a cache of posts
IDs16 bytes an item, 500 items, 300 million accounts
8 KB a user, 2.4 TB, 24 nodes. An edit touches one post.
Whole posts1 KB an item, 500 items, 300 million accounts
500 KB a user, 150 TB, 1,500 nodes. An edit rewrites millions of copies.

The author ID is in the item so a load can filter without fetching a post. An unfollow needs no write to the list: the load already has the reader's follows, and it drops any ID whose author is not among them. A new follow adds that author's recent IDs to this one reader's list.

A list of IDs is not a page, so a load hydrates: it swaps each ID for its post. Twenty posts at 8,000 loads is 160,000 post reads a second. A post cache serves them, 4 nodes at half of 100,000 each, and fewer with batched reads, since these are simple gets. Those 4 nodes have room for 400 GB, about 20 days of posts, so rate sizes this cache and not memory.

Behind it, the post store splits by a hash of the post ID, since every read now names one, the test in Chapter 3's Picking the Partition Key. That is about 55 nodes by year five, a floor again. A second table keyed by author serves the merge, about 1.8 TB with its copies by year five, about one more node's worth of disk, spread across the store.

A post now writes two rows on two partitions, with no transaction across them, so the order matters. The author row goes first, then the post row with its message. A crash between them leaves an ID that hydrates to nothing, the author sees an error, and no follower sees a thing.

What it costs: one more round trip per load, and a page that waits for its slowest post. The deep dives come back to that wait.

Page Three

"They've scrolled down, and two dozen posts have landed above them. What comes next?" The 20 posts just below the last one they saw, as if nothing had landed. The cursor is the last post ID shown. A time-ordered ID is the time and the ID at once, which is what Chapter 1's API and Data Before Boxes asks of a cursor, and new posts sit above it and shift nothing.

It works across the merge, since the reader's list and the large accounts' lists sort by the same ID. New posts wait behind a "new posts" pill, a small button at the top of the feed. Tapping it fetches them with since and the newest ID the reader has seen.

What it costs comes in three parts. There is no jumping to page nine, which nobody asked for. Past the trimmed 500, the read falls back to the first design's merge, on the table keyed by author. And a post whose fan-out arrived late can carry an ID older than the reader's newest. The pill asks only for what is above that ID, so the post stays unseen until the next reload.

The Two Paths Now

A load goes through the load balancer to a feed server. The server reads the reader's list from the feed cache and their follows from the follow graph, then merges in the lists of the large accounts they follow. It hydrates 20 IDs from the post cache, with the post store behind it for a miss. The table keyed by author answers reads past the trimmed 500 and, as the next lesson shows, the reader's own recent posts.

A new post goes to the post store, a relay puts it on the log, and the fan-out workers read the log. For an author under the line, a worker reads the followers from the follow graph and cuts them into batches on the log, and workers add the ID to each follower's list. For an author past the line, a worker adds the ID to that account's own list.

The one database has gone, and nothing it held has. Posts are in the partitioned store. Follows moved to their own 3 nodes, stored both ways: workers read them by the account followed, and loads read them by the follower. The figure does not mark the follow graph new, because its data is not.

The first design, with what changed marked new
Clientposts and loads Large listslarge accounts, IDsNEW Load balancerany server Feed cache500 IDs a userNEW Feed serversmerge, then hydrate Post cache20 posts a loadNEW Post storeby post ID, by author Logposts, then batchesNEW Follow graphboth ways, 3 nodesNEW Fan-out workers200,000 a secondNEW load or post new post adds ID readwriteasynccache missNEWsince the first design
Interview Pitfalls
  • Saying "hybrid" with no line. Asked where it goes, "celebrities" is not a number.
  • Filling the cache with posts, then being asked what a post edit costs.
  • Pushing into every account's list with no word on accounts that never log in.
What Interviewers Listen For
  • The ratio of 20 brought back as the reason for fan-out on write.
  • The line derived from the fleet's rate, with its trigger stated.
  • IDs and whole posts compared in nodes, not adjectives.

Knowledge Check

Fan-out on write comes to 40,000 inserts a second on average. What does that buy, and at what price?

  • One list read per load, with each post visible as soon as it is stored
  • One list read per load, with posts that reach followers seconds late
  • Far fewer writes in all, since each post now takes a single insert
  • A smaller store, since the precomputed lists take over from the table

You set the line at a million followers. What did you just trade?

  • Fresher posts from large accounts, for a slower publish on each one
  • A much smaller fleet of workers, for a feed that can now show repeats
  • Ten more list reads per load, for about 150,000 fewer inserts a second
  • Less cache memory for each user, for posts that show a minute later

An entry holds IDs and not whole posts. What does a feed load owe for that?

  • A full merge of the follow list on each load, 200 seeks at a time
  • A trip to fetch 20 posts, 160,000 post reads a second at peak
  • A rewrite of the whole entry each time one of its posts is edited
  • A larger cache overall, since 500 IDs outweigh a page of 20 posts

Page three uses a cursor on the last post ID. What did that cursor give up?

  • Jumping to a numbered page, and a late post that sorts below the newest seen
  • The guarantee against repeats, since new posts still push old ones down a page
  • The merge with large accounts, since those lists cannot share the same cursor
  • Every page past the newest 500 IDs, which can no longer be read by anyone

You got correct