Real-Time Ingestion: Segments and the Mutable/Immutable Split

A tweet is searchable in seconds because indexing only ever touches one small, mutable structure — never the rest of the corpus.

The intuition first

Classic search engines (think: indexing a web crawl) batch-build their index — crawl for hours, then build one big immutable index file, swap it in. That's fine when "fresh" means "this morning's crawl." It's useless for Twitter, where "fresh" has to mean seconds ago during a breaking news event — by the time a batch rebuild finished, the event would be over.

Segments: the unit that makes incremental indexing possible

Earlybird is built on Lucene, and the concept that makes real-time ingestion tractable is the segment. A Lucene index isn't one monolithic structure — it's a collection of segments, each an independent, self-contained mini-index. Critically, once written, a segment is immutable.

Earlybird shard's index =  [ segment 1 (immutable) ]
                           [ segment 2 (immutable) ]
                           [ segment 3 (immutable) ]
                           [ live segment (mutable, small, in-memory) ]  ◄── new tweets append here

New tweets are tokenized and appended only to the live segment — a small, mutable, in-memory structure. This is what keeps ingestion cheap and fast: appending to a small in-memory structure is orders of magnitude cheaper than touching an index spanning the full historical corpus. A search query, in turn, checks all segments (the immutable ones plus the live one) and merges results across them — so a tweet is visible to search the instant it lands in the live segment, typically within seconds of being posted.

Periodically, the live segment is sealed — closed off, made immutable, and optionally merged with other small immutable segments into progressively larger ones (a background compaction process, conceptually identical to how an LSM-tree merges SSTables — see the Cassandra compaction study for the general pattern). A fresh, empty live segment takes its place for new writes.

t=0     new tweet arrives → tokenized → appended to LIVE segment (mutable)
t=0+ε   query for a term in that tweet → live segment is included in the search → tweet found
...
t=T     live segment sealed (now immutable) → becomes segment N
        new empty live segment created for subsequent tweets
...
later   background merge: segments 1+2+3 → one larger segment (fewer, bigger segments = faster queries)

Why single-writer, lock-light matters

The ingestion path is engineered so that appending to the live segment never blocks concurrent reads. This works because of the immutability property: once a segment (even the eventual sealed version of the live one) is handed to a reader, its content never changes underneath that reader. A search thread can be mid-scan of the live segment's current state while a write appends a new posting — Lucene's segment model is specifically designed so this doesn't require the heavy locking a mutable-in-place structure would need. This is the same "append-only, immutable-once-written" principle behind the timeline ZSET's cheap concurrent access, applied one layer down at the index-segment level.

The queue feeding ingestion — and why it's a separate stream from fan-out

A posted tweet triggers two independent downstream deliveries off the same write event: fan-out to followers' timelines (main study async fan-out), and indexing into Earlybird. These are not the same queue, and shouldn't be, because the two consumers want fundamentally different things from the partitioning:

Fan-out queue (main study, the Earlybird synthesis part) Search-ingestion stream
Partition key author_id — needed to preserve per-author tweet ordering into followers' timelines No author-ordering requirement at all — ingestion just needs roughly-chronological arrival so posting lists stay recency-ordered
Consumer Fan-out workers doing ZADDs Earlybird ingestion nodes doing tokenize-and-append
What backpressure means Deeper queue → wider eventual-consistency window for followers Deeper queue → longer delay before a tweet is searchable
Replay use case Almost never replayed — a completed fan-out shouldn't be redone Replayed deliberately when reindexing history (tokenizer change, corruption, adding a new index)

That last row is the sharpest reason to keep them as genuinely separate topics rather than two consumer groups sharing one author-partitioned stream: if Earlybird's index needs to be rebuilt or reindexed under new tokenization rules, you want to replay the "tweet created" event stream into search only — you do not want that replay to also re-trigger fan-out, which already succeeded the first time and would just be redundant (if harmless, given ZADD's idempotency — main study Timeline Service) load on the timeline cluster. Two independent streams mean the two downstream systems fail, lag, and get rebuilt independently — the same decoupling argument the main study already makes for separating the poster's write from fan-out, one level further out.