Query Serving: Root/Leaf Fan-Out

One search query touches hundreds of index shards in parallel, and the fact that shards are ordered by time — not by a random hash — is what lets the system stop early instead of waiting on all of them.

The intuition first

No single machine holds "the index" — even within just the realtime cluster, the corpus is sharded across many hosts. A query has to become a scatter-gather: broadcast to every relevant shard, collect what each one found, merge. This is the same shape as the main study's read-time fallback (scatter-gather + k-way merge when a user scrolls past the cached 800 tweets) — search just runs that pattern on every single query, not just a fallback path.

Root and leaves

client query "earthquake worldcup"
        │
        ▼
   ROOT (query coordinator)
        │
        ├──► leaf shard 1 (recent window)   → local matches, ranked
        ├──► leaf shard 2 (recent window)   → local matches, ranked
        ├──► leaf shard 3 (older window)    → local matches, ranked
        ├──► ...                                   (hundreds of leaves,
        └──► leaf shard N (archive tier)    → local matches, ranked    across both clusters)
        │
        ▼
   ROOT merges all leaves' results, re-ranks globally, returns top-K

Each leaf owns a shard of the index and executes the full query (tokenize, intersect posting lists, rank) against just its own slice, returning its local top-K. The root fans the query out to every relevant leaf, waits (with a bounded timeout — see the production trap in the realtime and archive clusters part), and merges the per-leaf results into one globally-ranked answer.

Why chronological sharding enables early termination

Unlike the timeline's follower-ID sharding or the tweet store's Snowflake-ID sharding (Tweet Store appendix), search shards within the realtime tier are effectively time-ordered — a given leaf owns a contiguous, bounded window of recent tweets. This has a concrete payoff: for a query sorted by recency (the common case), the root can query the newest shards first, and if it already has enough high-confidence results, it can stop before querying older shards at all — an early-termination optimization that a randomly-hashed shard layout couldn't offer, because with random hashing there's no shard you could skip without risking a miss.

Latency amplification is the same tail-latency problem as everywhere else

A query that fans out to hundreds of leaves is bound by its slowest leaf, not its average leaf — exactly the read-amplification risk the main study's fan-out-on-read section describes for the naive timeline design ("bounded by your slowest author shard"). The more leaves a query touches, the higher the odds that some leaf is momentarily slow (GC pause, a hot term, a degraded host), so query latency has a heavier tail than any individual leaf's latency distribution would suggest. This is mitigated the standard way: per-leaf timeouts, treating a non-responding leaf as "no results from this shard" rather than blocking the whole query, and (for very hot terms during live events) caching results for a few seconds so identical concurrent queries don't each re-trigger the full fan-out.