Sub topic 7
Keeping the index in sync with the source of truth
This chapter assumes you know SQL, REST APIs, and message queues. The search index is a derived copy of your data. The primary database stays the source of truth, and the index must follow it. If the index is lost, you rebuild it from the database. Nothing is rebuilt the other way.
Choosing a sync approach
There are three common ways to move data into the index.
| Approach | How it works | Strength | Weakness |
|---|---|---|---|
| Dual write | The application writes to the database and to the index in the same request | Simple, low latency | The two writes can fail separately and leave them out of agreement |
| Change data capture (CDC) | A tool reads the database's transaction log and publishes each change | Catches every change, including ones made outside the app | Extra infrastructure to run |
| Outbox pattern | The app writes the change and an "outbox" row in one transaction. A worker publishes the outbox rows | Atomic with the business write, no log tooling needed | You operate a polling worker |
Dual write is the usual first mistake. If the database commit succeeds and the index call times out, the two stores disagree and nothing records the gap. Prefer CDC or an outbox.
Making updates safe to repeat
Queues usually deliver a message at least once, so the indexer sees duplicates. It can also see events out of order, for example after a retry. Two rules handle both.
- Index by document ID. Writing the same ID twice replaces the document, so a replay does no harm.
- Attach a version from the source database, such as a row version or update counter. The engine rejects a write whose version is not higher than the stored one.
PUT /products/_doc/42?version_type=external&version=7
{
"name": "Trail running shoes",
"category": "footwear",
"price": 89.0,
"in_stock": true
}With version_type=external, a late event carrying version 6 is refused with a version conflict, so it cannot overwrite version 7. The indexer should treat that conflict as "already up to date" and acknowledge the message instead of retrying forever.
Handling deletes and failures
A delete is also an event. If the indexer ignores it, removed products keep appearing in results. Send delete events through the same pipeline, and index by ID so the engine can remove the right document.
Plan for messages that keep failing, such as one with malformed data. Retry a few times with backoff, then move the message to a dead-letter queue so one bad record does not block the rest. Alert on the size of that queue.
Building documents
The indexer often needs data from several tables, such as a product with its category name and brand. Two options exist:
- Have the event carry the full document. This is fast, but the event can be stale by the time it is processed.
- Have the event carry only the ID, and let the indexer read the current row. This costs a database read, but it always indexes the latest state, which also helps with out-of-order events.
Initial load and repair
New pipelines need a backfill: scan the table in batches by primary key and index each row, using the same indexing code as live updates. Run it periodically in a lighter form, comparing counts or checksums between database and index, to catch drift that events missed.
Exercise
Your app writes a product to the database and then calls the search engine. The engine call times out after the database commit succeeds. What goes wrong, and which pattern fixes it?
Show answer
The product exists in the database but not in the index, and nothing records the gap, so search misses it until some later update. The outbox pattern fixes this. The change and an outbox row are saved in one transaction, and a worker publishes the row until indexing succeeds. CDC also fixes it by reading the database log.
Exercise
The indexer receives an update with version 5 after it already indexed version 7 for the same product. What should happen, and why use external versions?
Show answer
The engine rejects the write with a version conflict, and the indexer acknowledges the message without retrying. External versions come from the source database, so the engine can discard older updates that arrive late.