LOAD-BEARINGfree
Architecture labTier 13 · unit 14 of 31

Search with reindex

A product catalogue served by full-text search. The catalogue changes constantly, the index must reflect changes within seconds, and changing the analyzer or the field mapping requires rebuilding all eighty million documents while the site stays up.

Envelope80M documents · index lag under 10 s · full rebuild with no downtime

What the design must do

  • The relational store stays the source of truth; the index is derived from it
  • Updates reach the index without the application writing to two systems
  • A full reindex builds alongside the live index and swaps atomically
  • Permission filtering happens inside the query, not after it

The worked envelope

Assumed. 80M documents averaging 2 KB of indexable text, 5% of the catalogue changes daily, index lag under 10 s, ten shards.

Worked envelope
Source text80M × 2 KB = 160 GB
Index sizewith positions, ~0.6× source ≈ 100 GB
Per shard100 GB ÷ 10 = 10 GB — comfortably in page cache
Update rate4M/day ≈ 46/s, peak ~500/s
Reindex throughput~5k docs/s/shard × 10 = 50k docs/s
Full rebuild80M ÷ 50k ≈ 27 minutes

What the numbers decide. The last row is the one to sit with. A full rebuild of the entire corpus takes about half an hour — which means a reindex is a routine operation you can run on a Tuesday afternoon, not a quarterly event requiring downtime.

That changes the design posture completely. Because rebuilding is cheap, the index can be treated as fully disposable derived data: you never migrate it in place, you build a new one alongside, dual-write for the half hour, verify, and flip an alias. And because a query scatters to ten shards and gathers, your p99 is the slowest shard — so adding shards past what memory requires makes tail latency worse, not better.

The decisions, and what each one costs

Build a new index alongside, dual-write, verify, flip an alias

Because
Analyzer and mapping changes apply to all 80M documents and cannot be applied in place, and a 27-minute rebuild makes the parallel path affordable
The cost
Double the index storage and double the write path for the duration, plus an alias layer to operate
Rejected
In-place reindexing, which is downtime measured in half-hours on a user-facing search box

The index is derived; the catalogue database is the source of truth

Because
A rebuild has to have something to rebuild from, and search engines have weaker durability and consistency guarantees than the store you would be trusting
The cost
A pipeline with lag, so writes are visible in search seconds after they are visible in the catalogue
Rejected
Search as primary store, which is discovered to be a mistake during the first rebuild, when the only copy is the thing being rebuilt

Permission filters inside the query, not applied to results

Because
Post-filtering ranks over documents the user cannot see, returns too few results, and leaks the existence of the rest through the count
The cost
Permissions must be denormalised into the index and updated when they change, which is its own fan-out
Rejected
Filtering after retrieval, which is simpler, faster to build, and wrong in a way that is hard to see in testing

Ten shards sized by memory, with a hard cap on page depth

Because
10 GB per shard fits in page cache, and deep pagination is quadratic — page 500 makes every shard produce 5,000 results for the coordinator to discard
The cost
Users cannot page arbitrarily deep and must refine instead; scatter-gather means p99 tracks the slowest shard
Rejected
More shards for headroom, which spreads each query wider and makes the tail worse for no capacity gain

Components in the reference design

Client
Browser or mobile app
Load Balancer
L7 routing, TLS termination, health checks
App Service
Stateless business logic
SQL DB
Relational, ACID, joins
Event Log
Kafka-style, ordered, replayable
Worker
Async consumer, background jobs
Search Index
Inverted index / trie, text and prefix

The reference design

Do not dual-write. The obvious design has the application write the database and then the index. It is wrong for the reason Lesson 6.05 gave: two systems, one transaction, no atomicity. The database commit succeeds, the index write fails on a network blip, and the document is invisible to search forever with nothing logging an error.

Derive the index from the write log. Change data capture reads committed changes from the database and publishes them to an ordered log; an indexer consumes that log and updates the index. The application writes exactly one system. Indexing lag becomes a measurable number — consumer lag, from Lesson 6.04 — instead of an unknown, and a broken indexer is a backlog you can drain rather than data you have lost.

Reindex is the requirement that shapes everything. Analyzers, field mappings and tokenisation change, and the change applies to all eighty million documents. The pattern is an alias: the application queries catalogue, which points at catalogue_v4. Build catalogue_v5 from the log while v4 serves traffic, let it catch up to the live tail, then repoint the alias in one atomic step. Lesson 7.04's read-model rebuild and Lesson 9.05's blue-green deploy, applied to an index.

Two properties make this work and both are worth stating: the source of truth is elsewhere, so the index is disposable; and the log is replayable from a position, so a rebuild is a normal operation rather than an incident.

Permission filtering belongs in the query. Filtering results after retrieval ranks over documents the user cannot see, returns too few, and leaks the existence of what was removed. Push the constraint into the query as a filter clause — Lesson 12.02 in a system whose builders were thinking about relevance.

Deletes need to reach the index. A removed product that stays searchable is the failure users report first. Deletion is a change like any other, which is another reason the log is the right transport — it carries the delete for free.