AlgoViz

Indexing in distributed search

medium 28 min

How 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 hearBring upBecause
Find by wordsInverted indexScanning does not scale
New posts visible at onceRefresh intervalFreshness costs segments
Too much for one machineSplit by documentLocal joins
We changed the language rulesNew index and aliasAnalyzers need a rebuild
Search misses some rowsVersions and reconciliationIt is a second copy

Safe is not findable

  1. Interviewer

    A seller adds a product and searches for it. Nothing. Is it a bug?

  2. You

    Not yet. The write is in the log and the buffer. It becomes findable with the next refresh, about a second later.

  3. Interviewer

    Sellers complain.

  4. 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.