Data and Knowledge SystemsIngestion and freshnessStaff
How should an index process a source primary-key change without leaving two entities searchable?
By Amit Kumar3 min readWorked answer includedReviewed 28 Sept 2026
The question
Interview question
A source corrects an account identifier from A-018 to A-180 and now contains one row. Its change-stream consumer indexes A-180 but leaves A-018 searchable, so the assistant reports two accounts. Trace how the key change should reach the index, explain recovery across a crash between removal and creation, and compare a mutable business key with a stable source identity.
Draft your approach first
Take a few minutes before you read the answer. Name the constraint that matters most, the failure you are designing for and how you would know it worked.
Reveal the worked answerWorked answer
A search document needs an identity that tells the index which existing record a write replaces. If that identity is the source’s mutable primary key, changing the key can look like removing one object and creating another. An upsert for the new identity does not automatically remove the old one.
In Debezium's PostgreSQL connector documentation, a primary-key change is represented as a delete and tombstone for the old key followed by a create for the new key. If a consumer handles only the new upsert, drops tombstones, or loses the old-key event during routing, the old index entry remains. The delete event tells a sink to remove the old entity. A Kafka tombstone is a null-valued record used by log compaction to remove a key’s retained value. A consumer must interpret its connector’s envelope rather than assuming those two messages are interchangeable application commands. This is a concrete connector behavior, so inspect the actual connector and topic configuration before assuming every CDC system emits this exact sequence.
I would trace source transaction ID, old and new keys, event keys, partition placement, offsets and index mutation logs. Do not assume the old and new key events share a partition or that arrival order across partitions is globally guaranteed. The consumer needs to delete the old indexed identity and create the new one, with retries and version checks so a late event cannot resurrect the old identity. If the search document ID is derived from a mutable business key, consider a stable source row identity or explicit alias map. A stable ID simplifies moves but does not remove the need to update the searchable business identifier and permissions.
Test key changes with one row, then with concurrent updates and a restart between delete and create. A partially applied change can temporarily make the row absent or duplicated. Decide whether the product tolerates that window or needs a staged atomic index swap or read-time reconciliation. An assistant answer should not count duplicate index hits as two entities when the source of truth has one. If one of the hits is from an older revision, expose that mismatch rather than silently merging contradictory facts.
Would an eventual consistency promise excuse this? Only for a bounded and observable interval. Record per-source revision or cursor watermarks and alert when old identities persist past the freshness target. A connector uses file paths as document IDs. What breaks on a move? covers a connector incorrectly treating a moved file path as a new document ID. A deleted customer document returned after an index rebuild. Which deletion did replay miss? covers an old snapshot missing a deletion tombstone during rebuild. Here the source intentionally changes its primary key, and the CDC event sequence must remove the old indexed identity in normal operation.
A hypothetical recovery record can group old key A-018, new key A-180 and the source transaction into one logical identity-change operation. Retrying the removal must not delete a later, unrelated entity that reused A-018. Retrying creation must preserve the accepted source revision. If the sink cannot publish both mutations atomically, a read-time reconciliation or generation switch can hide the intermediate duplicate at additional operational cost. Merely deduplicating answer text is insufficient because filters, counts and permissions still operate on two index records.
Related questions
Keep practising data and knowledge systems
Follow another question in this area, or search the complete library.
Get my essays on AI engineering and distributed systems by email. Subscribe to ArchCrux