Scrivio Interview library
ScenarioAdvanced7 min read · updated 12 November 2026

The search index must denormalise data from four services (product, price, stock, reviews). Updates arrive out of order and some documents show prices from yesterday. How do you design the pipeline?

Yesterday's prices appear because events from four services arrive out of order and the indexer either overwrites newer data with older data or loses updates in a read-modify-write race inside the search engine. We fix it by giving each source its own version, partitioning every event stream by product id so one worker owns each product, and assembling the full document in a materialised view store such as a database table or Kafka Streams state, applying each section only if its version is newer. The assembled document is then indexed whole, we can rebuild everything by replaying compacted topics, and we track freshness per source with alerts.

Why this question is asked

A search document for a product usually needs data that no single service owns: the description from the catalogue, the price from pricing, availability from stock and the rating from reviews. Combining them is where most search freshness bugs come from, because four independent streams have no shared order and no shared transaction. Interviewers ask this to see whether we can reason about ordering per key, versions per source and where the merged state should live. In this answer, we will diagnose why old prices win, then design the pipeline: per-source versions, partitioning by product id, the materialised view, writing to the index, rebuilding, and freshness monitoring.

1Why Yesterday's Price Wins

Let us look at what the current pipeline probably does. Four consumers each listen to one service's events. When a price event arrives, the price consumer fetches the product document from the search engine, changes the price field and writes the whole document back. The stock consumer does the same for stock, at the same time, for the same product.

Two things go wrong. First, a lost update: both consumers read the same document, each changes its own field and writes back, and the second write carries the old price, quietly undoing the first. Second, out-of-order events: pricing republishes a backlog after an outage, or a retry delivers an older price change after a newer one, and the consumer applies whatever arrives last. Both lead to exactly the symptom we see, a price that was correct yesterday.

Step throughA lost update in read-modify-write
Price consumerSearch engineStock consumer
get product 42 (price 10, stock 5)
get product 42 (price 10, stock 5)
write price 12, stock 5
write price 10, stock 3
stored: price 10, the new price is lost

5 messages in order. Play them, or step through them one at a time.

Each consumer writes back the other's field as it read it, so the last writer silently undoes the first.

2Give Every Source Its Own Version

There is no global clock across four services, so we do not try to order their events against each other. We only need to order each service's events for one product. Each source puts a version on every change event: a counter for that product that the service increments on every update, or the database LSN if the events come from change data capture. The price service's version for product 42 says nothing about the stock service's version, and that is fine.

Our merged document then carries a version per section, such as price_version and stock_version, next to the data. A price event with version 18 is applied only if the stored price_version is lower than 18. An older price event, however late it arrives, is ignored. This is the key idea: we compare versions within one source, never across sources.

3Partition Every Stream by Product Id

Versions stop old data from winning, but we also want to avoid two workers fighting over one product. So all four topics use the product id as the Kafka message key. Kafka sends all messages with the same key to the same partition, and one consumer instance in a group owns each partition. If the four topics have the same number of partitions and use the default partitioner, product 42's events from all four sources land on the same partition number, and one instance of our assembler handles all of them.

This means updates to one product are processed one at a time, in order per source, by one owner. There is no read-modify-write race left, because nobody else is writing product 42.

4Assemble Documents in a Materialised View Store

Instead of reading and modifying documents inside the search engine, we keep the merged state in a store we control, called a materialised view: a stored, precomputed result of combining the sources. It can be a table in PostgreSQL with one row per product and one column group per section, or a Kafka Streams state store, a local RocksDB database backed by a compacted changelog topic. The assembler applies each event to the stored row if its version is newer, then emits the complete document.

The search engine is a poor place for this coordination: its updates are not transactional, scripted partial updates are slow, and every partial update rewrites the whole document internally anyway. In our own store, applying a section is a single conditional update, and we can see the merged state, debug it and rebuild it.

SQL
-- One row per product; each section has its own version
UPDATE product_view
SET price = :price, currency = :currency, price_version = :v, price_at = :eventTime
WHERE product_id = :id AND price_version < :v; -- 1 row: applied, 0 rows: older event, ignored
-- After any applied change, emit the whole document with a doc_version that only grows
UPDATE product_view SET doc_version = doc_version + 1 WHERE product_id = :id
RETURNING doc_version; -- 233
Step throughThe assembly pipeline
  1. Four source servicesproduct, price, stock, reviews publish versioned events
  2. Kafka topics keyed by product idper-product order, compacted for replay
  3. Assembler with view storeapply section if its version is newer
  4. Full document with doc_versionexternal version, older writes rejected
  5. Search indexread by the product search API

5 layers, from the top down. Step through them one at a time.

Notice that a stale price event simply updates zero rows. The assembler then increments doc_version, a counter for the whole document, and sends the full document to the search engine with that number as an external version. Because one worker owns each product, doc_version increases in order, and the search engine rejects any older full document that arrives late.

5Rebuilding When Documents Are Wrong

Sooner or later a bug corrupts documents or we change the mapping, and we need to rebuild. If each source topic is compacted, which means Kafka keeps at least the latest event for every key, then replaying the four topics from the beginning rebuilds every product's current state. We start a new assembler with an empty store and a new consumer group, let it write into a new index, and swap the search alias when it has caught up.

Where a topic cannot be compacted, because it carries events rather than state, each source should be able to publish a snapshot of its current state on demand. The rebuild path must be tested regularly, for example by rebuilding into a scratch index every month and comparing it with production, because an untested rebuild is only a hope.

6Tracking Freshness per Source

The original symptom was discovered by a customer, which means nobody was measuring. We store, next to each section, the time of the event that last updated it, such as price_at. The assembler exports, per source, how far behind the newest event it has processed is from the current time, and Kafka consumer lag per topic. We set targets per source, for example price within 30 seconds and reviews within 15 minutes, and alert when the 99th percentile exceeds them.

A second check samples a few hundred products every few minutes and compares indexed prices with the pricing service, which catches logic bugs that lag metrics miss.

7Follow-up questions

Could we skip the view store and use scripted partial updates in the search engine?

Yes, a scripted update can compare a section's version inside the document and set ctx.op to noop when the event is older, and that does fix the ordering problem for one section. It is a reasonable step for a small system. But each update still rewrites the document internally, scripts are slower, debugging the merged state is harder, and a rebuild has nowhere to start from. The view store makes the merge explicit and replayable.

What if a product's description changes but the price event for that product is days old?

That is fine. Each section is applied independently, so a new description event updates the description section and emits a new document containing the latest price we know, whatever its age. The price section only changes when a newer price event arrives. Freshness per source tells us whether the price is old because nothing changed or because the price feed is stuck.

8Conclusion

In this answer, we saw that stale prices come from out-of-order events and read-modify-write races when four services update one search document. The fix is to version each source independently, partition all streams by product id so one worker owns each product, and assemble documents in a materialised view store that applies a section only when its version is newer. The full document is indexed with a growing external version, compacted topics or snapshots let us rebuild, and freshness per source tells us when any feed falls behind.

What to say in the interviewAbout 90 seconds aloud

Open with

I think the stale prices come from two things: events from different services arrive out of order, and the consumers probably do read-modify-write on the search document, so one consumer writes back another's old field.

Then, if they want more

I would not try to order events across services. Instead, every source attaches its own version per product, a counter or a database LSN, and the merged document keeps a version per section, so an older price event can never replace a newer price.

All four topics are keyed by product id, so one assembler instance owns each product and processes its events one at a time.

The assembler keeps the merged state in a materialised view store, either a PostgreSQL table or a Kafka Streams state store, applies a section only if its version is newer, and then emits the full document with a document version that only increases.

That document is indexed with an external version, so late writes are rejected by the search engine too. For repairs and mapping changes, I would make the topics compacted or have sources publish snapshots, so I can rebuild into a new index and swap an alias.

Finally I would store the event time per section and alert on freshness per source, for example price within 30 seconds, plus a sampling check that compares indexed prices with the pricing service.

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
Elasticsearch documentation
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 "The search index must denormalise data from four services (product, price, stock...": https://scrivio-interview-library.netlify.app/databases/search-engines/2cfe70e960/