Skip to the guide

Adding search to a product: indexing, sharding and freshness

IntermediatePattern5 pages

A big city's transport network runs a lost property office. Every found item goes into the main ledger. Each counter, one per district, also keeps cards by word ("umbrella", "blue", "phone") for the items found in its district, copied from the ledger a few minutes after each entry. A caller who knows the district is put through to that counter, and for one who doesn't, the switchboard asks every counter at once and reads back the best matches.

The items are documents, here a marketplace's listings, and the ledger is the database, the source of truth. The cards are an inverted index, and the counters are shards, each a complete small index over its own documents, kept a few seconds behind by the change stream. The switchboard is the coordinator that asks every shard and merges. The district is a routing key that every search on some path carries, like the seller in her own dashboard. Interviewers listen for how the index is fed, how it's split, and what each search costs. The move: feed a search engine from the database's change stream, split it by document so any search can ask every shard, and route by a key wherever every search carries one.