In a nightly-reindexed RAG pipeline, why build chunk IDs from the source document ID plus position, not random UUIDs or chunk-text hashes?
answer
- what a second run writes
- overwrite instead of append
- the document is the unit you delete
- stable source identifier, not the URL
- source prefix + document ID + ordinal
basics
~20 sDeterministic IDs from the document ID plus chunk position make a re-run overwrite the same records instead of adding copies, and let the pipeline name and delete a document's whole chunk set. Random IDs duplicate every run; text hashes lose the document link.
solid answer
~50 sA nightly re-index has to be safe to run again. If each chunk gets a random UUID, every run inserts a fresh copy of every chunk, so an unchanged page appears twice after two nights and retrieval fills its top results with duplicates. An ID like `docs:4812#0003` (source, the source system's immutable document ID, chunk ordinal) is the same on every run, so an upsert overwrites in place and the write is idempotent. It also makes the document the addressable unit: with the previous chunk count or ID list you can name every chunk a page owns and delete them. A hash of the chunk text is deterministic too, but it ties the record to its words rather than its document, so identical boilerplate in two pages collapses into one record and an edit leaves the old record behind under a different ID.
go deeper
Recall that the ingestion job runs again and again, and that a random ID turns every run into a fresh insert. Say what a second run does under each scheme.
Explain why the document, not the chunk text, must anchor the ID, and how a stable source identifier plus a position lets you name and delete a page's chunks.
Show the edges: renames under URL-based IDs, cross-source collisions, stores that only accept UUIDs, and the orphans positional IDs still leave when a page shrinks.
Treat the ID scheme as a contract every connector must honour; a team that lets each source invent its own IDs pays for it in duplicates and undeletable chunks.
## The problem a chunk ID has to solve A **retrieval index** for retrieval-augmented generation (RAG) stores each source document as many **chunks**: passages of text, each with an embedding vector and a payload of metadata such as the document's title and URL. A nightly ingestion job reads every page of, say, a product-documentation site, splits each page into chunks, embeds them and writes them to a vector store. The job runs again tomorrow, and the night after. The chunk's **record ID** decides what that second run does. Many vector stores offer an **upsert**: write this record, replacing any existing record with the same ID. Upsert is only as safe as the ID you feed it. The requirement is simple to state: running the job twice over the same sources must leave exactly the records one run would have left. ## What each ID scheme does on a re-run | ID scheme | Unchanged page, second run | Page edited | Deleting the page later | |---|---|---|---| | Random UUID per chunk | Every chunk inserted again: duplicates | New chunks added beside the old ones | Needs a `doc_id` field in every payload, or a separate record of the IDs it got | | Hash of the chunk text | Same IDs, overwritten: fine | Changed chunks get new IDs; old ones stay | Shared boilerplate chunks belong to several pages at once | | Source + document ID + position | Same IDs, overwritten: fine | Same positions overwritten in place | Every ID is derivable from the document ID and its chunk count | - **Random IDs** make every write an insert. Two runs give two copies of every chunk; thirty runs give thirty. Retrieval returns near-identical passages for the top results, and nothing in the IDs tells the copies apart. - **Text-hash IDs** are deterministic, so an unchanged chunk is not duplicated. But the ID describes the words, not their owner. A legal footer or a "Getting started" paragraph that appears on fifty pages hashes to one ID, so fifty pages fight over one record and its metadata, and deleting one page can remove a chunk the other forty-nine still need. When a page is edited, its changed chunks hash to new IDs and the old records stay behind. - **Document ID plus position** ties every chunk to the document it came from. The same page always produces the same IDs, so re-runs are idempotent, and the pipeline can enumerate a page's chunks from its ID and how many chunks it had. ## Choosing the document part of the ID The document ID carries most of the weight, and it must be **stable for as long as the document exists**: 1. Use the source system's own **immutable identifier** for the page (a page number or object ID), not its URL, path or title. URLs change when a page is moved or renamed, and a URL-based ID turns a rename into a delete plus an add, with the old chunks left behind if the delete is missed. 2. **Prefix the source** (and the tenant, in a multi-tenant index). Two sources can both number their pages from 1; without a prefix, page 1 of one source overwrites page 1 of the other. 3. **Zero-pad or delimit the ordinal** so `#0003` is unambiguous and sorts in order, which makes a range of positions easy to name. 4. If the store only accepts UUIDs or integers, derive a **name-based UUID** (version 5 hashes a namespace plus a name) from the readable string. The same string always gives the same UUID. Keep the readable form and the document ID in the payload, so you can debug and delete by document. ```json { "id": "docs-site:4812#0003", "payload": { "doc_id": "docs-site:4812", "position": 3, "title": "Configuring alerts" } } ``` (Field names are illustrative, not any store's schema.) ## What positional IDs do not solve on their own Deterministic IDs make a re-run idempotent for documents that did not change shape. They leave two gaps that the rest of a re-indexing design closes: - **A shrinking document.** If a page goes from twelve chunks to nine, the upsert overwrites positions 0 to 8 and never touches 9 to 11. Those three records are **orphans**. The fix is to remember each document's previous chunk set and delete what the new run did not write. - **An insert near the top.** A positional ID changes meaning when text is inserted early in a page, because every later chunk shifts down one position and is rewritten. Keeping one edit's rewrite small is a chunk-boundary design question, separate from the re-run safety covered here. Idempotency in general (deduplication stores, idempotency keys on requests) is a wider distributed-systems subject. The point here is narrower: a deterministic, document-anchored chunk ID is what makes this one pipeline's writes safe to repeat. ## How interviewers probe it - "What happens on the second run?" is the opening: random IDs duplicate. - "Why not hash the text?" tests whether you see that a hash identifies content, not ownership. - "The page was renamed; what happens?" tests the stable-identifier point. - "How do you delete a page's chunks?" checks that you can name them all from the ID scheme plus a stored count or ID list.
- The vector store only accepts UUIDs as record IDs. How do you keep IDs deterministic?Build the readable string first, such as `docs-site:4812#0003`, then derive a name-based UUID (version 5) from a fixed namespace plus that string. The same string always yields the same UUID, so upserts still overwrite. Store the readable string and the document ID in the payload so you can debug a record and delete by document.
- Why prefix the source or tenant to the document ID?Different sources often number documents independently, so page 4812 can exist in two of them. Without a prefix their chunks share IDs and each run overwrites the other source's chunks. The prefix makes IDs globally unique and lets you scope a delete or a cleanup to one source.
- Do positional IDs make an edit near the top of a page cheap to re-index?No. An inserted paragraph shifts every later chunk by one position, so every later record is rewritten even if its text barely moved. Positional IDs buy re-run safety and the ability to name a document's chunks; keeping one edit's rewrite local is a separate chunk-boundary design choice.
- Random UUIDs are fine because the store deduplicates identical vectors.
- Use the page URL as the document ID; it never changes.
- A hash of the chunk text gives every chunk a unique owner.
- Upsert makes any ID scheme safe to re-run.
- Positional IDs keep an edit near the top of a page cheap.