Indexing in distributed search
medium 28 minHow documents become a searchable index spread over many machines: the analysis chain, posting lists and their compression, segments and merges, and the choice between splitting by document or by term.
When it comes up
The moments this idea belongs in a design interview, and the moments it does not.
Signals
When it comes up
| You hear | Bring up | Because |
|---|---|---|
| Find by words | Inverted index | Scanning does not scale |
| New posts visible at once | Refresh interval | Freshness costs segments |
| Too much for one machine | Split by document | Local joins |
| We changed the language rules | New index and alias | Analyzers need a rebuild |
| Search misses some rows | Versions and reconciliation | It is a second copy |
Safe is not findable
Interviewer
A seller adds a product and searches for it. Nothing. Is it a bug?
You
Not yet. The write is in the log and the buffer. It becomes findable with the next refresh, about a second later.
Interviewer
Sellers complain.
You
I show the seller their product from the database by id, and let search be a second behind. Refreshing after every write would make thousands of tiny segments for everybody.
Options
Compare your moves
The index no longer fits on one machine. How do you split it?
BreaksBy the first letter of the word
- How
- A to M on one machine, N to Z on the other.
- Limit
- Every document touches both, two-word searches ship lists, and s is far larger than x.
- Choose when
- Never.
WorksBy hash of the term
- How
- Each term lives on one shard.
- Limit
- One-word searches are cheap, writes and multi-word searches are not.
- Choose when
- Nearly static data with one-word lookups.
StrongBy hash of the document id
- How
- Each shard is a complete index of its documents. Searches ask all shards.
- Limit
- Every search costs one request for each shard.
- Choose when
- Almost always. Route by tenant if queries name one.