Earlybird Self-Test

Self-Test

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

Question 1

Why can't the home timeline's Redis ZSETs (per-viewer sorted sets of tweet IDs) be extended to also answer "show me tweets containing X"? Be precise about the key mismatch.

Show answer

A timeline ZSET is keyed by viewer ID, and its values are tweet IDs from that viewer's followed accounts — the key encodes a relationship (who follows whom), not content. Answering "which tweets contain X" requires an index keyed by term, mapping to tweets regardless of who posted them or who follows whom. There is no way to derive a term-keyed structure from a viewer-keyed one without building an entirely separate index — and even a brute-force scan across every viewer's ZSET would still miss tweets from accounts nobody in the query happens to follow, since the timeline only ever contains tweets that were fanned out to someone.

Question 2

A tweet is searchable within seconds of being posted, yet the index holds hundreds of billions of historical tweets. Explain the mechanism (segments) that makes both of those true simultaneously without contradiction.

Show answer

The index is not one monolithic structure — it's a collection of Lucene segments, and only one of them (the live segment) is mutable at any time. New tweets are tokenized and appended only to that small, in-memory live segment, which is cheap regardless of how large the rest of the corpus is. Queries check all segments (live + immutable) and merge results, so a tweet is visible the instant it lands in the live segment — typically seconds. The live segment is periodically sealed into an immutable segment and a fresh one takes over, and immutable segments are later merged into larger ones in the background. The historical corpus's size never affects the cost of an individual write, because writes never touch it.

Question 3

Why does Earlybird split into a realtime cluster and an archive cluster instead of one uniform index? Name the two different cost profiles being optimized for, and explain what class of production incident this split creates that a single uniform index wouldn't have.

Show answer

Recent and old tweets have opposite cost profiles: recent tweets need seconds-level freshness and are queried extremely heavily (a live event drives huge query volume against the last few hours), which favors a small, in-memory, mutable-friendly structure; old tweets are queried rarely but need to be stored compactly at enormous and ever-growing volume, which favors disk-backed, immutable, batch-built storage. One uniform index would have to either keep the entire historical corpus in memory (unaffordably expensive) or serve breaking-news queries against disk-backed storage sized for the whole corpus (too slow). The split creates a genuine incident class: a query spans both clusters, and if the archive cluster degrades while the realtime cluster stays healthy, a naive implementation either blocks the fast tier on the slow one (defeating the point of having a fast tier) or silently drops results with no signal that they're incomplete — the same slowest-shard risk the main study's fan-out-on-read section describes, now at the cluster level.

Question 4

A search query fans out to hundreds of leaf shards. Explain why chronological (time-based) sharding lets the root terminate early for recency-ranked queries, and why a hash-based sharding scheme couldn't offer the same optimization.

Show answer

Realtime-tier leaves each own a contiguous, bounded, time-ordered window of tweets rather than a randomly-hashed slice of the corpus. For a recency-ranked query, the root can query the newest shards first and stop once it has enough high-confidence results, skipping older shards entirely — because it knows exactly which shards could possibly contain newer matches. A hash-based scheme gives no such guarantee: any shard could hold a recent match (hashing scatters by ID, not by time), so the root would have to query all of them to be sure it isn't missing something newer.

Question 5

Both the timeline service and Earlybird are described as holding "derived, rebuildable state." What's the actual source of truth in both cases, and why does that fact justify running both on comparatively low-durability, in-memory-heavy infrastructure?

Show answer

In both cases the source of truth is T-bird, the durable tweet store: the timeline ZSET is a projection of "recent tweets from accounts I follow" (derived from T-bird + the social graph); Earlybird's posting lists are a projection of "which tweets contain term X" (derived from T-bird via tokenization). Since neither structure holds anything that isn't recoverable by rebuilding from T-bird (plus, for the timeline, the graph), losing a shard of either is a bounded, rebuildable cache miss rather than permanent data loss — which is exactly what justifies running both on comparatively low-durability, in-memory-heavy infrastructure (Redis for timelines, in-memory live segments for search) instead of paying for maximally durable storage on data that's disposable by construction.

Question 6

A posted tweet triggers two independent downstream deliveries: fan-out to timelines, and indexing into Earlybird. Why are these two separate queues/streams rather than two consumer groups sharing one author-partitioned topic?

Show answer

The two consumers need fundamentally different things from the stream's partitioning: fan-out workers need per-author ordering (so one author's tweets never arrive at followers' timelines out of order), which is why that queue is partitioned by author_id; Earlybird's ingestion has no author-ordering requirement at all — it only needs roughly-chronological arrival across all authors combined, so posting lists stay recency-ordered. Beyond partitioning, keeping them as separate streams enables independent replay: rebuilding or reindexing Earlybird's corpus (after corruption, or a tokenizer change) means replaying the "tweet created" stream into search alone, without also re-triggering fan-out, which already succeeded the first time and would just be redundant work on the timeline cluster. Separate streams let the two downstream systems lag, fail, and get rebuilt independently of each other.

Primary Sources

  • The Engineering Behind Twitter's New Search Experience (Earlybird) — blog.twitter.com/engineering/en_us/a/2011/the-engineering-behind-twitter-s-new-search-experience
  • Twitter Home Timeline — Staff-Level Study Guide — the companion document this guide extends (the Earlybird part there now links here)

Names/details are historical and approximate; treat as a mental map of the shape of the system (segments, realtime/archive split, root/leaf fan-out), not current production fact.