Real-time search and scatter-gather
The primer’s search service, inverted indexes, and Twitter’s Earlybird: hash-partitioned shards, scatter-gather and merging.
What you will learn
- Follow a search query through parsing, the search cluster and the merge.
- Intersect posting lists in an inverted index.
- Explain how Twitter's Earlybird indexes while serving and scatter-gathers queries.
Search in the primer
The primer's Search Service:
- Parses and tokenizes the query: removes markup, breaks the text into terms, fixes typos, normalizes capitalization, and converts the query to boolean operations.
- Queries the Search Cluster (such as Lucene). It scatter-gathers: it asks every server in the cluster whether it has results.
- Merges, ranks, sorts and returns the results.
The primer also notes the search cluster "would likely need to keep the tweets in memory to keep latency low".
Inverted indexes
An inverted index maps each term to its posting list, the IDs of the documents that contain it:
"raft" → [1042, 1089, 2104]
"consensus" → [1042, 1520, 2104]
For raft AND consensus, walk both sorted lists together and keep IDs that appear in both. That takes time and finds 1042 and 2104. Starting with the shortest list keeps the work down.
Earlybird: Twitter's real-time search
Busch et al. describe Earlybird, the engine behind Twitter's real-time search (ICDE 2012). The paper reports that it served "over two billion queries a day, with an average query latency of 50 ms". "Usually, tweets are searchable within 10 seconds after creation."
- Partitioning: tweets are "hash partitioned across Earlybird servers", which index them as soon as they are processed.
- Scatter-gather: a front-end Blender sends each query to multiple Earlybird servers, then "merges and re-ranks the results".
- Indexing while serving: each server uses "a single-writer, multiple-reader lock-free algorithm". One thread adds new tweets while many threads answer queries, coordinated by a memory barrier rather than locks.
- Time first: in real-time search "the temporal signal remains dominant". Twitter's first version simply listed hits in reverse chronological order.
- Ranking signals: static signals added at indexing time, and resonance signals such as retweet counts that are updated over time. Earlybird combines these with the searcher's local social graph to rank results for each user.
Because tweets are hash-partitioned, any shard can hold a match, so every query goes to every shard. Each shard returns its best few results and the front end merges them. Merging each shard's top gives the same global top as searching one big index, which the visual lets you check. The cost is that the slowest shard sets the response time (the tail-latency effect from chapter 1).
Scatter-gather search
12 sample tweets, hash-partitioned across 4 index shards. Every shard gets the query; a front end merges each shard’s top 3.
- New post on system design for real-time search@lea · id 112 · engagement 140
- Good system design is mostly about trade-offs@hal · id 108 · engagement 510
- System design interview: design the Twitter timeline@dara · id 104 · engagement 300
- Search Query Clients: 1 × Clients & Users
- Search API Gateway: 4 × App Compute Server
- Root Scatter-Gather Aggregator: 4 × App Compute Server
- Earlybird Search Shard 1 (RAM): 2 × App Compute Server
- Earlybird Search Shard 2 (RAM): 2 × App Compute Server
- Earlybird Search Shard 3 (RAM): 2 × App Compute Server
- Search Index Queue: 3 × Message Queue / Kafka
- Search Query Clients sends requests to Search API Gateway
- Search API Gateway sends requests to Root Scatter-Gather Aggregator
- Root Scatter-Gather Aggregator sends requests to Earlybird Search Shard 1 (RAM)
- Root Scatter-Gather Aggregator sends requests to Earlybird Search Shard 2 (RAM)
- Root Scatter-Gather Aggregator sends requests to Earlybird Search Shard 3 (RAM)
- Search Index Queue sends requests to Earlybird Search Shard 1 (RAM)
- Search Index Queue sends requests to Earlybird Search Shard 2 (RAM)
- Search Index Queue sends requests to Earlybird Search Shard 3 (RAM)
Key takeaways
- An inverted index maps each term to the sorted IDs of the documents that contain it; an AND query walks the lists together, shortest first.
- Tweets are hash-partitioned, so every query goes to every shard; each returns its best few, and merging each shard's top gives the global top .
- Earlybird adds new tweets with one writer while many threads read, without locks, and ranks with time as the dominant signal, combined with engagement and the searcher's social graph.
Sources
- System Design Primer: Design the Twitter timeline and search, search use case
- Busch et al., “Earlybird: Real-Time Search at Twitter”, ICDE 2012
Adapted from The System Design Primer by Donne Martin, licensed under CC BY 4.0. Changes were made.