Quick revision
The short answer
Product changes are written only to PostgreSQL, and they reach Elasticsearch through an outbox table or change data capture, read by a separate indexer that writes to the index idempotently and uses external versioning so a late, older event cannot overwrite a newer one. For a full reindex we build a new index from the database while live changes keep flowing, then switch an alias from the old index to the new one in one atomic call, so searches never see an empty or half-built index. We monitor indexing lag and failed documents, and we never write to both stores from the request path.
What to remember
In this answer, we designed a pipeline in which the request writes only to PostgreSQL, changes are published atomically through an outbox or change data capture, and an indexer applies them to Elasticsearch idempotently by product id with external versioning, so duplicates are harmless and older events lose. Full reindexing builds a new index from a snapshot plus a replay of changes and then swaps an alias atomically. We monitor indexing lag and dead-lettered documents, reconcile periodically, and avoid dual writes, which diverge on partial failure.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Product changes are written only to PostgreSQL, and they reach Elasticsearch through an outbox table or change data capture, read by a separate indexer that writes to the index idempotently and uses external versioning so a late, older event cannot overwrite a newer one. For a full reindex we build a new index from the database while live changes keep flowing, then switch an alias from the old index to the new one in one atomic call, so searches never see an empty or half-built index. We monitor indexing lag and failed documents, and we never write to both stores from the request path.
Why this question is asked
Keeping a search index in step with a database sounds simple: save the product, then index it. In production that simple version slowly drifts, and nobody notices until a customer finds a product that was deleted last week or cannot find one that was added yesterday. Interviewers ask this design question because it combines several distributed-systems ideas in a very concrete setting: atomic publishing, idempotence, ordering, and zero-downtime switching. In this answer, we will see why dual writes fail, design the change pipeline with an outbox or change data capture, make indexing safe against duplicates and reordering, rebuild the index behind an alias, and decide what to monitor.
1Why Dual Writes From the Request Path Diverge
A dual write means the request handler writes to PostgreSQL and then to Elasticsearch, two systems with no shared transaction. Each write can fail on its own. If the database commit succeeds and the index call times out, the product exists but cannot be found. If we index first and the commit then fails, search shows a product that does not exist. Retrying in the handler does not fix it, because the process can crash between the two writes, and two concurrent requests can reach the two stores in different orders, leaving the index with the older version.
So the first design decision is that the request writes only to the database, the source of truth, and the index is updated afterwards from a reliable record of what changed.
2Publishing Changes: Outbox or Change Data Capture
With the outbox pattern, the same database transaction that changes the product also inserts a row into an outbox table describing the change. Because it is one transaction, either both happen or neither does. A relay then reads new outbox rows and publishes them, typically to a Kafka topic, and marks them as sent.
7 messages in order. Play them, or step through them one at a time.
The request path ends at the commit; everything after it can be retried safely.
Change data capture, CDC, is the alternative: a tool such as Debezium reads PostgreSQL's write-ahead log, the log every committed change is written to, and publishes a change event per row without any extra table. CDC needs no change to application code and captures changes made by scripts too; the outbox gives us control over the event's shape and lets us publish a clean product document rather than raw table rows. Both give the property we need: a change is published if and only if it was committed.
3Idempotent Indexing and Out-of-Order Events
The pipeline delivers each event at least once, so the indexer will sometimes see the same event twice, and after retries or partition rebalances it may see version 7 after version 8. Two choices make that safe. First, the document id in Elasticsearch is the product id, so indexing the same product twice replaces the document rather than adding a second one. Second, we use external versioning: we pass the product's version from the database, and Elasticsearch accepts the write only if that version is higher than the one it holds.
A 409 here is not an error to retry; it means we already have newer data, and the indexer treats it as success. Deletes need the same care. A delete with an external version is remembered only for a limited time (index.gc_deletes, 60 seconds by default), after which a very late older update could bring the product back. Many teams therefore mark products as deleted with a flag and a version, filtered out of searches, and purge them later. Kafka partitioned by product id also keeps events for one product in order, which makes reordering rare in the first place.
4Full Reindexing Without Downtime
Sooner or later we must rebuild the whole index: a new analyzer, a changed field type, or a recovery after a bug. Field types cannot be changed in place, so we build a new index. The application never searches an index name directly; it searches an alias, a pointer name such as products that refers to a real index such as products_v3.
- Create products_v4 with new mapping
- Record the change-stream positionKafka offset or WAL position
- Bulk-load from a database snapshotthrottled, with external versions
- Replay changes from the recorded position
- Lag near zero and counts match?YesSwap alias products -> v4NoKeep replaying, investigate gaps
- Keep v3 for rollback, then delete it
6 steps in order. Play them, or step through them one at a time.
The alias switch is atomic, so there is no moment when the alias points at nothing or at both indexes. While the new index catches up, the indexer keeps writing live changes to the current index, and the catch-up replay covers changes that happened during the bulk load. External versioning is what makes this simple: whether an event arrives through the bulk load or the replay, the higher version wins. Keeping the old index for a day gives us an instant rollback by switching the alias back.
5Monitoring Lag and Failed Documents
Two numbers tell us the pipeline is healthy. Indexing lag is the time between a database commit and the change being searchable; we measure it from the Kafka consumer lag and from a timestamp in each event, and we alert when it exceeds what the business accepts, perhaps 30 seconds. Failed documents are events the indexer could not apply, for example because a value does not fit the mapping; they go to a dead-letter topic with the error, so one bad product does not block the stream, and we alert when that topic receives anything.
We also run a periodic reconciliation job that compares counts and a sample of ids and versions between PostgreSQL and the index, which catches the drift no single event reveals.
6Follow-up questions
Should the indexer use the event payload or reload the product from the database?
Reloading the current product makes the indexer always write the latest state, which naturally fixes ordering problems, at the cost of a database read per event. Using the payload avoids the read but requires that the event carries the full document. Both work with external versioning.
How does this design survive the indexer being down for an hour?
Kafka keeps the events, the indexer resumes from its last committed offset, and the lag alert fires while it is down. Searches still work on slightly stale data, and nothing is lost because the database and the topic hold every change.
7Conclusion
In this answer, we designed a pipeline in which the request writes only to PostgreSQL, changes are published atomically through an outbox or change data capture, and an indexer applies them to Elasticsearch idempotently by product id with external versioning, so duplicates are harmless and older events lose. Full reindexing builds a new index from a snapshot plus a replay of changes and then swaps an alias atomically. We monitor indexing lag and dead-lettered documents, reconcile periodically, and avoid dual writes, which diverge on partial failure.
Open with
The rule I start from is no dual writes in the request path, because the database commit and the index call can fail independently and the two stores drift apart.
Then, if they want more
So the request only writes to PostgreSQL. To publish changes reliably, I use either an outbox table, written in the same transaction as the product change, with a relay sending rows to Kafka, or change data capture with Debezium reading the write-ahead log. Either way, an event exists if and only if the change committed.
An indexer consumes the events, partitioned by product id, and writes to Elasticsearch. It must be idempotent, so the document id is the product id, and it must handle out-of-order events, so I index with version_type external using the product's version column. Elasticsearch rejects an older version with a 409, which the indexer treats as already done.
For deletes I prefer a deleted flag with a version, because delete versions are only remembered briefly. For a full reindex, the app only ever searches through an alias.
I create products_v4 with the new mapping, record the stream position, bulk-load from a snapshot, replay changes from that position, check lag and counts, then swap the alias in one atomic _aliases call, and keep v3 around for rollback.
I monitor indexing lag end to end, send failed documents to a dead-letter topic with an alert, and run a periodic reconciliation between the database and the index.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Design how product changes in PostgreSQL reach an Elasticsearch index reliably...": https://scrivio-interview-library.netlify.app/databases/spring-data-nosql/9eeb918c1a/