Lesson 4 · Minutes 30-45

News Feed: Deep Dives and Curveballs

Problem

You have a line at a million followers and one reason for it, and the last fifteen minutes start by leaning on that reason. After that come hydration and its hot post, then what a page really promises. Then the curveballs arrive, led by the ranking you parked in First Design.

Where the Hybrid's Line Goes

Set the three designs side by side, on average. All write is 190,000 inserts a second, 380,000 at peak, which is nearly twice what the fleet can do, and it takes minutes per large post. All read is 800,000 seeks a second, 1.6 million at peak. The hybrid is 40,000 inserts and 40,000 list reads.

Three designs: work a second at peak, and the largest post's delay
All writeevery post fans out
380,000 inserts a second, against a fleet of 200,000. Largest post: 250 seconds.
All readnothing fans out
1.6 million seeks a second. A post is one row, so it has no delay.
Hybrida line at a million followers
80,000 inserts and 80,000 list reads a second. Largest fan-out: 5 seconds.

Follower count alone does not choose between them. Take one author over a day. Fanning out costs an insert per follower per post, and merging costs a list read per follower per load. Both prices grow with followers.

What separates them is their shape. A write is a burst the whole fleet carries at once. A read is spread over the day, and it is bounded by how many large accounts one reader follows.

So two limits set the line: a post's fan-out time, and merge reads per load. A lower line means more reads per load, and a higher one means a longer burst. At a million, those are 10 reads and 5 seconds. You can defend it in those terms: "A million gives me 5 seconds of fan-out and 10 reads a load. If lag grows I lower it, and if merges get slow I raise it, and neither change needs a redesign."

The line is one setting, kept with the follow graph next to each author's follower count. Workers read it to choose a post's path, and feed servers read it to know which large-account lists exist. An account crossing the line changes mode at its next post. One going up leaves its older IDs in followers' lists to age out. One coming down has its older posts only in its own list, so feed servers keep merging that list until those posts age out. Each post goes down one path only, so the merge never shows it twice.

Which way an author goes
Few followers, as nearly every author has→Fan out on write: an insert per follower
Past the line, a million followers→Stay out of the fan-out: its own list, merged at read time
Crossing the line, either way→Change mode at the next post; older posts stay put and age out
Fan-Out on Write vs Fan-Out on Read

Fan-out on write does the work once per post, and a load is one list. Use it while loads outnumber posts and an author's followers are few, which is nearly everyone.

Fan-out on read does the work once per load, and a post is one row. Use it for an author whose one post would hold the shared fleet long enough to delay everyone else's, here past a million followers.

Hydrating and the Hot Post

A page's 20 posts go out as one batched read to each of up to 4 nodes, and the page waits for the slowest. Count the whole load: one round for the reader's list, follows and own posts, one for the large-account lists, and one for the posts. Between zones a round trip is 1 to 2 milliseconds, so the trips are not the risk. One slow node is.

A large account's post is also one hot key, the first shape of heat in Chapter 3's Hot Partitions. Say all 50 million followers read it within an hour. That is about 14,000 reads a second on one key, inside one node's limit and heavy for it. A node already carries 40,000 at peak, so this key takes it past the 50,000 you planned for.

Chapter 2's When the Cache Hurts has the answer in its local tier: a few seconds of the hottest posts inside each feed server. Its price is those same seconds of staleness, out of reach of any removal. The first reads of a new post all miss together, and that lesson's coalescing and lease keep them to one trip to the store.

Author names and like counts join the page here, from systems this design does not own, and they may be seconds stale. That is a stated staleness on an eventual read, as in Chapter 4's Consistency Models That Come Up.

What a Page Promises

Take the promises one at a time. No repeats: the cursor gives you that. No holes: not quite, because late fan-out can put a post behind a reader who has scrolled past its place. Say so before the interviewer finds it: "A late post can land above where the reader has scrolled to, in a place they've already passed. They see it on the next reload, and that's what the minute we agreed buys."

The author sees their own post at once. Fan-out will lag it, so a load always merges the reader's own recent posts from the table keyed by author, on the copy that took the write, where the row went in before the post was accepted. That is read-your-writes from Chapter 2's Read Replicas and Lag, at no extra store, for one more read per load.

A deleted post leaves a short page, and the server reads a few more IDs to fill it.

Curveballs

"Rank the feed instead." The ranking you parked comes off the board now, and the fan-out does not change. The list becomes candidates: score the 500 IDs plus the large accounts' posts at read time, with the model as a box and a latency budget you state. Order now moves between loads, so a cursor on ID fails.

Freeze a ranked list per session, about 200 IDs, 3.2 KB, with a ten-minute TTL, a time limit that removes it. The cursor is then a position in that list, and the score runs once per session. You would reject a frozen copy for a newest-first feed, where an ordered list already holds the reader's place. It is right here because the order moves. Even if every peak load opened a session, ten minutes of 8,000 a second is about 5 million lists, 15 GB, inside one cache node. A session past its ten minutes starts again from the top.

"A post is deleted, or the reader blocks an author." Do not chase 500-item lists across millions of users. The store marks the post gone. The post cache then stores "not found" for its key for a short while, the negative caching of Chapter 2's When the Cache Hurts, so a miss cannot refill it from a replica that has not caught up, with an assumed day's expiry on every copy as the net. Hydration drops the post, and the ID lingers until it ages out, a few bytes.

Other copies take longer. A feed server's local copy goes when its few seconds run out, and a page already on a phone stays until the next load. The media behind the post is a takedown at the CDN, with the limits Chapter 2's The Edge: CDNs as a Black Box gives. A block is a filter at read time: the reader's blocks sit beside their entry in the feed cache, copied from the store, so the filter runs even when the graph cannot answer, and the item's author ID is what it filters on.

"Some users have not logged in for a year." Assume a third of accounts, 100 million. Give each entry an expiry of a year, renewed by every load, so a dormant user's entry goes by itself. That frees 800 GB, 8 of the 24 nodes. A worker that finds no entry skips it, at the price of one wasted call per such follower. A returning user has no entry, and their list is rebuilt once with the first design's merge, keeping the newest 500, up to 2,000 seeks, about 200 milliseconds on disk.

"The feed cache loses a node." The tier's health check sees it first. With a replica, the replica is promoted and readers notice nothing. Without one, a twenty-fourth of users, 12.5 million, have no list. That is about 330 loads a second at peak, and at 200 seeks each, about 67,000 seeks a second on the author table.

Rebuild each list on demand with a limit on rebuilds a second, and past that limit serve a shorter feed from the large-account lists rather than fail. A worker that finds no entry skips it, as it does for a dormant user, so an entry is either whole or absent, and the store already holds whatever a rebuild needs. The rate falls as lists come back: about 4 million daily readers sit on that node, each is rebuilt once, and within a day it is over. No data was lost, since every list comes from the store and the graph.

How Each Level Answers

Mid-level candidates get to fan-out on write when the seeks are counted, and they reach the large-account problem when the interviewer names the account. They know the entry should hold IDs, and they adopt a cursor when pushed. They need a hint to state a line or a size.

Senior candidates state the ratio of 20 before choosing, and they raise the large account unprompted, with 250 seconds on it. They draw the line from the fleet's rate and say what moves it. They size the cache in nodes, IDs against posts, and they offer what a cursor gives up, late fan-out included.

Staff candidates ask what the feed is for, because a ranked feed changes what the cache holds and what a page means. They price dormant accounts and a lost node as cost decisions, and they keep the first design alive as the rebuild path. They ask who may lower the line and how anyone would know it was wrong, and they run it as an operated setting with a signal behind it.

Interview Pitfalls
  • Answering the ranked feed with a new architecture, when the fan-out does not change.
  • Deleting a post from every follower's list, millions of writes for an item that costs 16 bytes.
  • Treating the feed cache as durable data, so a lost node sounds like data loss.
What Interviewers Listen For
  • What the two prices grow with, named along with what actually decides between them.
  • A filter at read time as the first instinct for a deletion.
  • The first design brought back as the rebuild path, and said to be that.

Knowledge Check

The interviewer says: rank the feed instead. What is your first move?

  • Replace the fan-out with a pipeline that writes each user's order
  • Keep the cursor on post ID, and sort each page by score as it leaves
  • Keep the fan-out, and treat each list as candidates to score at read time
  • Store a score beside each ID, and set it once as the post fans out

A post is deleted. How does it leave the feeds of the people who follow its author?

  • Workers remove its ID from the list of every follower who received it
  • The store marks it gone, hydration drops it, and the ID ages out on its own
  • Each follower's list gets rebuilt with the first design's merge on next load
  • Its copy in the post cache expires, and from then on no reader can find it

A third of accounts have not logged in for a year. What does dropping their lists save, and what does a return cost?

  • All 2.4 TB of the cache, then a full scan of the post store on each return
  • 800 GB and 8 of the 24 nodes, then a wait of a minute for the next fan-out
  • 800 GB and 8 of the 24 nodes, then up to 2,000 seeks when one comes back
  • Almost nothing, since 8 KB a user is too small to be worth collecting

A feed cache node is lost with no replica. What reaches the store?

  • About 160,000 reads a second, the post cache's whole load at peak
  • About 1.6 million seeks a second, the whole of the all-read design
  • About 67,000 seeks a second at peak, from readers who have lost a list
  • Nothing at all, since the lost lists wait for the next scheduled rebuild

You got correct