Apply document revisions, permission changes and deletions safely when source events arrive late, twice or out of order.
Incremental retrieval ingestion: replay events without reviving deleted passages
Make the event contract explicit
Each source event needs tenant, document ID, monotonic source version, action and a stable event ID. The index writer should compare versions before replacing passages. At-least-once delivery means duplicate events are normal; processing the same revision twice must yield the same searchable state. A late upsert for version 6 cannot restore a document deleted at version 8. Index freshness depends on this ordering rule.
Separate removal from embedding work
Delete and permission-revocation events should invalidate answer caches and remove eligible passages promptly. A new revision may take longer because extraction and embeddings run asynchronously. During that interval, either keep an explicitly permitted old revision under a defined policy or mark the document unavailable. Do not show a half-written revision whose metadata and vectors disagree. Publish the new generation only after all required passages and filters are ready.
Retain a tombstone
A tombstone records the latest delete version even after the vector rows are removed. Without it, a replayed old upsert can recreate a deleted passage. Keep the tombstone until upstream retention guarantees make older events impossible, and test that guarantee rather than guessing. A permission change also needs a versioned marker; it may be independent of content revision and should not wait for re-embedding.
Observe lag and recovery
Measure event age at application, pending extraction count, index generation and deletion-to-unavailability delay. Replay a durable event log into a staging index, compare eligible document sets, then atomically switch the alias. Encoder migrations need the same generation boundary. A process restart should resume from a checkpoint without losing the version guard.
Worked contract
from dataclasses import dataclass
@dataclass(frozen=True)
class DocumentEvent:
document_id: str
version: int
action: str
text: str = ""
def apply_event(state: dict[str, tuple[int, str | None]],
event: DocumentEvent) -> None:
current_version = state.get(event.document_id, (-1, None))[0]
if event.version <= current_version:
return
if event.action == "delete":
state[event.document_id] = (event.version, None)
elif event.action == "upsert" and event.text.strip():
state[event.document_id] = (event.version, event.text)
else:
raise ValueError("invalid document event")
index_state: dict[str, tuple[int, str | None]] = {}
apply_event(index_state, DocumentEvent("policy-47", 8, "delete"))
apply_event(index_state, DocumentEvent("policy-47", 6, "upsert", "old policy"))
apply_event(index_state, DocumentEvent("policy-47", 8, "delete"))
assert index_state["policy-47"] == (8, None)Performance and operating cost
The version guard is O(1) expected time per event with a hash map and O(D) state for D documents. Extraction and embedding dominate CPU and queue time. Tombstones consume storage, but deleting them too early risks data resurrection. Report end-to-end publication lag and revocation lag separately.
Common Mistakes
- Deleting vector rows without retaining the latest source version.
- Assuming queue delivery is exactly once or ordered across partitions.
- Waiting for slow re-embedding before enforcing a permission revocation.
- Publishing metadata for a new revision before its passage set is complete.
Validation drill
Replay 53 events with duplicates, delayed upserts, revisions and deletes into an empty index. Shuffle events while preserving per-document source versions; assert that eligible final documents and tombstones match the source of truth.
Read next
- Index freshness: publish complete revisions and remove stale chunks
- Retrieval corpus contracts: identity, permissions and document lifecycle
- Embedding migrations: keep query and document vectors in one space
- Retrieval caches and conversation turns: recheck access every time
