Self-Test

Answer these from memory before expanding. If you can't, re-read the relevant part.

Question 1

A colleague proposes "just use fan-out on write for everyone — it gives O(1) reads, which is what we need at 300K reads/sec." Explain precisely why this is impossible, at what kind of account it breaks, and what the amplification factor is in the worst case.

Show answer

Fan-out on write makes each post cost O(followers) physical writes (the amplification factor N = follower count). For typical users (N ≈ hundreds) that's fine, but Twitter has accounts with tens to hundreds of millions of followers. A single tweet from a 100M-follower account would require 100,000,000 timeline inserts — the worst-case amplification factor is the celebrity's entire follower count. Worse, these arrive as a burst (one tweet → 100M near-simultaneous writes), correlated across celebrities during big events, and fan-out latency grows with N so the most-watched tweets become the slowest to deliver. No amount of hardware fixes it because the total work is fundamentally linear in N. Hence pure push is impossible; it breaks at high-follower ("celebrity"/"hot-user") accounts.

Question 2

Why is the ~20K-follower cutoff described as "a business decision disguised as a technical one"? What happens to the system if you move the cutoff down to 5K? Up to 100K?

Show answer

There's no follower count at which physics forbids fan-out — the cutoff is the point where fan-out's cost (write bandwidth, burst risk, fan-out latency) exceeds the read-time merge's cost, and that crossover depends on cluster economics, traffic, and how much operational complexity you'll tolerate — all business/ops inputs, not a law of nature. Moving it down to 5K: more accounts treated as celebrities → less write/fan-out load but heavier, slower reads and bigger read-time merges (you pull from more authors). Moving it up to 100K: fewer celebrities → simpler/cheaper reads but more accounts doing large fan-outs → more write amplification and burst risk. You tune it to balance write-side against read-side cost for your actual hardware.

Question 3

Twitter's timeline service stores tweet IDs, not tweet bodies, in a sorted set capped at ~800 entries. Give the two distinct reasons for storing IDs and the two distinct reasons for the cap. Then explain why it's safe for this Redis data to be lost.

Show answer

Store IDs (not bodies) because: (1) storage amplification — an 8-byte ID duplicated into every follower's list instead of hundreds/thousands of bytes of tweet content, keeping RAM-bound Redis small; (2) single source of truth — the tweet lives once in the tweet store, so edits/deletes touch one canonical copy instead of millions of duplicates. Cap at ~800 because: (1) nobody scrolls that far — readers consume tens of tweets/session, so more buys nothing; (2) bounded, predictable memory per user → a knowable cluster size (200M × 800 × 8B). It's safe to lose because the timeline is derived state: a home timeline is a function of durable inputs (every tweet in the tweet store + every follow edge in the graph). A lost shard is a lost cache, rebuildable via the read-time pull path; replication handles availability, recomputation handles the worst case. Durability lives in the tweet store + graph, not the cache.

Question 4

Your friend sees a celebrity's tweet 8 seconds before it appears in your feed, and separately you notice a tweet "pop in" above ones you already scrolled past. Explain both phenomena in terms of fan-out latency, the push/pull merge, and Snowflake IDs. Why does Twitter consider this acceptable?

Show answer

Both stem from fan-out being asynchronous (see eventual consistency). Your friend sees it first because fan-out sprays ZADDs over time — early followers get the tweet in milliseconds, the tail of a large fan-out waits seconds; you're in the tail (or the tweet is a celebrity pull-half item you haven't refreshed to merge yet). The "pop-in above already-read tweets" happens because a tweet created earlier (lower Snowflake ID) was delivered later (backlogged worker, late large fan-out, or pulled on a later refresh), and the read path re-sorts the assembled candidate set by Snowflake ID (creation time), so the late-arriving older tweet correctly slots above newer ones you'd already seen. It's acceptable because a timeline isn't a transactional system of record — Twitter deliberately chose availability + low latency over strong consistency; "shows up for everyone within seconds, in correct order once present" is a fine guarantee and far cheaper than synchronous global consistency.

Question 5

Explain the relationship between fan-out delivery and the algorithmic "For You" ranking. Specifically: does the algorithm replace fan-out? What does fan-out contribute to the ranking pipeline, and what would happen to each if the other failed?

Show answer

The algorithm does not replace fan-out — it's layered on top of it and consumes its output. Fan-out (push + read-time pull) produces the in-network candidate set — the raw tweets you could see from accounts you follow — cheaply and predictably. The ranking pipeline then adds out-of-network candidates (SimClusters/UTEG retrieval), light-ranks, heavy-ranks with a big neural net, filters/diversifies, and mixes. If ranking failed/degraded, you fall back to chronological over the same candidate set — still a usable feed. If fan-out failed, there are no in-network candidates to rank — nothing to show. So the robust must-not-fail layer (delivery) is deliberately separated from the fancy can-degrade layer (ranking); they also scale on different axes (graph/post-rate vs. model complexity/candidate count), letting ML iterate without touching fan-out infrastructure.

Question 6

Earlybird (search) and the timeline service both ingest every tweet, yet they're separate systems. What different question does each answer, what different data structure does each use, and where in the modern stack do the two worlds touch?

Show answer

The timeline service answers "what did the accounts I follow post?" — organized by the social graph, using a per-user sorted set of tweet IDs. Earlybird answers "which tweets contain term X?" — organized by content, using a global inverted index (term → time-ordered posting list of tweet IDs), with near-real-time ingestion so a tweet is searchable within seconds. They're separate because they're indexed on different keys (relationship vs. content), need different consistency, and scale on different axes (followers/post-rate vs. corpus/query-terms). The two worlds touch in the algorithmic timeline (see algorithmic timeline): out-of-network candidate retrieval uses search-style/content-and-engagement indexes (SimClusters embeddings, graph walks) to find tweets from people you don't follow, folding search-like retrieval into the modern feed as an input — even though classic chronological delivery and the search product remain separate systems.