skip to content

For a changed page in a RAG index, how do delete-then-insert, write-new-then-delete-old and an atomic generation flip differ in what a query can see mid-update?

level: seniorimportance: should knowfreq 45%

answer

  1. a gap, an overlap, or neither
  2. what a crash halfway leaves
  3. positional IDs overwrite in place
  4. one row decides which set is live
  5. transactions where the store has them

basics

~20 s

Delete-then-insert opens a gap where the page has no chunks; write-new-then-delete-old opens an overlap where both versions are retrievable. Writing a new generation and flipping one active-generation record shows queries the whole old set or the whole new one.

solid answer

~50 s

With delete-then-insert, queries between the two steps find nothing for the page, and if embedding fails after the delete the page is missing until a later run succeeds. With write-new-then-delete-old, queries can retrieve old and new chunks together and quote contradictory versions; with positional IDs it is worse, because the new chunks overwrite old positions one by one and a query sees a mix. The generation flip puts a generation number in each chunk ID, writes generation N+1 beside N, and treats a single registry record per document as the switch: the read path keeps only chunks whose generation matches the document's active one. One atomic row update flips the page; the old generation is deleted afterwards and a sweep retries failures. If the store supports multi-record transactions, delete plus insert in one transaction gives the same all-or-nothing view more simply.

go deeper

for a junior

Recall that replacing a page's chunks takes many writes, and that a query can arrive between any two of them.

for a middle

Explain the gap of delete-then-insert and the overlap of write-new-first, and why positional IDs turn the overlap into a mix.

for a senior

Design the generation flip end to end: generation in the ID, wait for visibility, one-row flip, read-side check, retire and sweep, and the transactional shortcut.

for a principal

Weigh the read-path cost of the generation check against the harm of a gap or an overlap for this corpus, and pick one replacement policy for every source.

## The replacement problem A changed source page in a retrieval-augmented generation (RAG) index owns a **chunk set**: every chunk record, with its vector and payload, that the page was split into. Updating the page means replacing that whole set. Many vector stores apply writes per record, or per batch with no all-or-nothing guarantee, so the replacement takes many operations, and queries keep arriving while it runs. The question is what a query can see in the middle, and what a crash in the middle leaves behind. ## Three orders of operations | Strategy | Mid-update a query sees | A crash halfway leaves | |---|---|---| | Delete old set, then insert new | Nothing for the page: a **gap** | The page missing until a later run succeeds | | Insert new set, then delete old | Old and new together: an **overlap** | Both versions until a cleanup runs | | Write new generation, flip, retire old | All old or all new | Old version live, or new version live with old chunks hidden | - **Delete-then-insert** is the simplest and needs no extra state, but the gap is real. Embedding a long page takes seconds to minutes, and if the embedding call fails, hits a rate limit or the job is killed after the delete, the page simply vanishes from answers. Outside a transaction, a delete is not rolled back because a later insert failed. - **Write-new-then-delete-old** never leaves the page empty, but during the overlap a query can retrieve the old paragraph stating the old price and the new paragraph stating the new one, and the top results fill with near-duplicates. With **positional IDs** (document ID plus chunk number) it does not even keep the old version whole: chunk 3 of the new version overwrites chunk 3 of the old as soon as it is written, so mid-update the page is a mix of both, with orphans at the end if the page shrank. ## The generation flip The flip separates **writing** the new set from **making it live**: 1. Give every write a **generation number**, and put it in the ID: `docs-site:4812#g15#0003`. Generation 15 never collides with generation 14. 2. Write all of generation 15. Queries do not use it yet. 3. Confirm the new chunks are searchable. In a store that applies writes asynchronously, an acknowledged write may not be returned yet; how long that takes is the store's write-visibility subject, but the flip must wait for it, or it reopens the gap. 4. **Flip**: one atomic update of the document's registry row sets `active_generation = 15`. This is the only step that changes what users see. 5. Delete generation 14. If this fails, nothing breaks: the read path already ignores it, and a periodic sweep removes chunks whose generation is not active. The read path enforces the flip. It over-fetches candidates from the vector search, looks up each candidate document's active generation in the registry (a cached lookup), and drops chunks from any other generation. Filtering on a version field inside the store is a query-time filtering technique of its own; here the point is that one record, not thousands of chunk writes, decides which set is live. ```pseudocode procedure replace_document(doc): old = registry.get(doc.id) # none for a new page gen = if old then old.active_generation + 1 else 1 ids = [] for each (position, text) in chunk(doc.text): id = doc.id + "#g" + gen + "#" + pad(position) store.upsert(id, embed(text), {doc_id: doc.id, generation: gen}) append id to ids wait_until_searchable(ids) # store-specific registry.set(doc.id, active_generation = gen, chunk_ids = ids) # the flip if old: store.delete(old.chunk_ids) # a sweep retries failures procedure search(query_vector, k): hits = store.search(query_vector, 3 * k) # over-fetch live = registry.active_generations(doc_ids of hits) hits = hits where hit.generation = live[hit.doc_id] return first k of hits ``` A crash before the flip leaves generation 15 written but inactive: queries keep serving 14, and the next run computes generation 15 again and overwrites the abandoned records under the same IDs; any extra ones left by a longer abandoned attempt are removed by the sweep. A crash after the flip leaves 14 hidden but stored until the sweep deletes it. ## When the store can do it atomically If the store supports **multi-record transactions with isolated reads**, as a transactional relational database holding the vectors does, delete-then-insert inside one transaction gives readers the old set or the new set and nothing in between, with no generation bookkeeping. Many dedicated vector stores guarantee atomicity per record only, and a batch call can partially apply; check what the store actually promises before relying on a batch. ## Choosing - Low stakes, short pages, rare edits: delete-then-insert may be acceptable if the gap is measured and short. - Pages whose old and new versions contradict each other (prices, limits, policy): avoid the overlap. - Transactional store available: one transaction. - Otherwise: the generation flip, paid for with a registry lookup on the read path and an over-fetch.

  • What does the read-side generation check cost, and how do you keep it cheap?
    Every query needs each candidate document's active generation, plus an over-fetch to replace hidden chunks. Cache the registry lookups with a short lifetime and invalidate on flip, and keep stale generations few by deleting them promptly after each flip, so the over-fetch rarely needs to be large.
  • Why must the flip wait until the new generation is searchable?
    In a store that applies writes asynchronously, an acknowledged upsert can take a while to appear in results. Flipping first makes the read path demand generation 15 while the store still returns only 14, which the filter then hides, so the page briefly has no chunks: the gap is back.
  • Is a single batch upsert-and-delete call enough to make the swap atomic?
    Only if the store documents it as atomic with isolated reads. Many stores apply a batch record by record, so a reader can see part of it and a failure can leave part applied. Treat a batch as a throughput tool unless the store's guarantees say otherwise.

Changing a station's departure board: wiping it and then writing the new times leaves travellers staring at a blank board, and writing new times beside the old ones shows two conflicting departures. Preparing the new board behind the scenes and swapping it in one move shows either the old timetable or the new one, never a blank or a mix.

  • If the insert fails, the store rolls back the delete automatically.
  • Writing new chunks first keeps the old version whole with positional IDs.
  • A batch call is atomic, so the swap needs no other design.
  • The overlap is harmless because retrieval ranks the newer chunk higher.
  • Once the store acknowledges a write, the new chunks are searchable everywhere.