Dashboard
Courses
Problems
Badges
Theme
Sign inStart free
Courses/Distributed Architecture
Lesson 16 of 24

Secondary indexes and search

Local versus global secondary indexes, inverted indexes for full-text search, and keeping them in sync.

20 min read

Secondary indexes in a partitioned store

Your data is partitioned by product_id, but the page needs products by brand. A secondary index answers that, and there are two ways to partition it.

  • Local (document-partitioned): each partition indexes only its own rows. Writes stay local and cheap, but a query by brand must scatter to every partition and gather results
  • Global (term-partitioned): the index itself is partitioned by brand. A query hits one index partition, but a single write may update index partitions on other nodes, usually asynchronously

Scatter-gather and the tail

A query that fans out to 50 partitions is as slow as the slowest of them. If each partition has a 1% chance of a 200 ms response, a 50-way fan-out hits at least one slow partition about 40% of the time. Limit fan-out width, use hedged requests (send a duplicate to another replica after the p95 latency), and set per-shard timeouts that return partial results rather than nothing for non-critical queries.

Inverted indexes

Full-text search engines such as Elasticsearch, OpenSearch and Solr build an inverted index: for each term, the sorted list of documents that contain it. A query intersects the posting lists for its terms and ranks the results, typically with BM25 relevance plus business signals such as popularity. Facets (brand, price band, rating) are computed from per-field structures over the matching documents. An index of 50 million products might be split into 10 to 20 shards of 10 to 50 GB each, with replicas serving read traffic.

Keeping the index in sync

The search index is a derived view, never the source of truth. Do not dual-write from the API to both the database and the index, because one write can fail and they diverge. Instead capture changes from the database (change data capture or an outbox) into a stream, and have indexer workers apply them. Search engines refresh roughly every second, so results lag writes by a second or more; design the UI so a newly listed product appearing a few seconds later is acceptable. Keep a full reindex job for schema changes and repairs.

Key takeaways

  • Local indexes make writes cheap and reads scatter; global indexes do the opposite
  • Wide fan-out amplifies tail latency; limit it and use hedging and partial results
  • Search engines use inverted indexes with relevance ranking and facets
  • Feed the index from change data capture, not dual writes, and plan for a full reindex

Sign in to track lessons and earn badges.

Idempotency and the exactly-once illusionGeospatial indexing

Challenge checkpoint

Course: 0/24 lessons · 0/10 challenges0%

Build it on the canvas, pass the review (70+) and mark it complete to count it toward the course badge.

  1. 5/10Design Product SearchBuild a sharded inverted index with facets, fed from catalogue changes by change data capture rather than dual writes.Medium
All 10 challenges →