Skip to the page

Search and Elasticsearch · senior

Keeping the index in sync and reindexing without downtime

The database owns the data and the search index is a copy. Use an outbox or change data capture to feed it, order events by version, and change mappings by building a new index behind an alias.

The notes

What you must know

  • Dual writes from the app lose data when the second call fails and race under load. Use an outbox (event written in the same transaction, relayed to Kafka) or CDC (Debezium reading the database log).
  • CDC needs no app changes and catches every write, including scripts. Then an indexer service builds the document (re-read the product with its joins, or use a denormalised read model) and writes with the Bulk API in batches.
  • Deletes: a tombstone event becomes a delete by id. Soft deletes become a filter or a delete.
  • Ordering: key Kafka messages by product id so one product is in order, and use external versions (a row version or log position) so an older event cannot overwrite a newer one.
  • A poison record the indexer cannot write (for example a mapping conflict) is retried forever and blocks one partition. Move it to a dead-letter topic, let the partition catch up, then fix and replay. Watch database disk too, because a stalled replication slot makes the log pile up.
  • Missing product checklist: GET products/_doc/<id>, then indexer errors and consumer lag per partition, the Kafka topic, the connector status, replication slot lag.
  • Zero-downtime mapping change: create products_v2, reindex (or replay from the source), keep it in sync, then switch an alias (products) from v1 to v2 atomically. The app always uses the alias.

The red pen

Common traps: mistake → fix

  • Mistake: Writing to both database and Elasticsearch in the request is simple and safe

    Fix: If the second write fails the two stores differ, with no way to know.

  • Mistake: I can change a field type in place

    Fix: You must create a new index and reindex, then switch the alias.

  • Mistake: Consumer lag on one partition means the whole system is slow

    Fix: It often means one stuck poison record.

Easy to hard

Questions interviewers ask, easy to hard

  1. Why is dual writing a bad way to keep a search index in sync?
  2. What is an outbox and what is CDC? When would you choose each?
  3. A new product does not show in search. Walk through what you check.
  4. How do you change a mapping without downtime?
  5. How do you stop an old event from overwriting newer data in the index?

Practise

Practise with these questions

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview