AI Agents 3 min read

Deduplicate RAG Knowledge Base Refresh Events

Handle duplicate notifications, failed writes and older revisions when refreshing a RAG knowledge base. Run a local Python fixture to check each outcome.

(Updated: ) • 558 words

A webhook that reports a source change should tell your RAG pipeline what to refresh. Record the event before acknowledging delivery. A separate result tells you whether the worker published the new revision. That distinction matters when the sender retries or when a write fails.

Use separate identities for the delivery and the revision

After verifying a delivery, record its event ID, source ID and source version. The event ID identifies the producer’s notification across delivery retries. The pair of source ID and version identifies the work, so two event IDs for the same revision can share one job. If a sender reuses an event ID for a different source or revision, retain the conflict for review.

The example assumes increasing integer sequences from a source that defines their order. If your provider only gives unordered version labels, use its documented ordering or verify the current source revision before writing. Keep the producer’s version separate from the revision token used by your storage system.

Reproduce a duplicate delivery and a failed write

The local fixture starts with revision 1. Two event IDs refer to revision 2, whose first write fails. Revision 3 then succeeds before revision 2 is retried. The assertions check that an old job never calls the simulated writer and that successful work is not applied twice.

class RefreshQueue:
    def __init__(self):
        self.events = {}
        self.jobs = {}
        self.committed = {}

    def receive(self, event_id, source, sequence):
        if type(sequence) is not int or sequence < 1:
            raise ValueError("Expected a positive source sequence")
        key = (source, sequence)
        if event_id in self.events:
            return "duplicate" if self.events[event_id] == key else "conflict"
        self.events[event_id] = key
        if sequence <= self.committed.get(source, 0):
            return "stale"
        if key in self.jobs:
            return "already_queued"
        self.jobs[key] = "pending"
        return "queued"

    def run(self, source, sequence, apply_revision):
        key = (source, sequence)
        if self.jobs[key] != "pending":
            return self.jobs[key]
        if sequence <= self.committed.get(source, 0):
            self.jobs[key] = "stale"
            return "stale"
        if apply_revision(source, sequence) is not True:
            return "retry_pending"
        self.committed[source] = sequence
        self.jobs[key] = "committed"
        return "committed"


queue = RefreshQueue()
source = "returns-policy"
queue.committed[source] = 1
assert queue.receive("event-2", source, 2) == "queued"
assert queue.receive("event-2", source, 2) == "duplicate"
assert queue.receive("event-2-copy", source, 2) == "already_queued"
assert queue.receive("event-2", source, 3) == "conflict"
assert queue.run(source, 2, lambda *_: False) == "retry_pending"
assert queue.committed[source] == 1
assert queue.receive("event-3", source, 3) == "queued"
writes = []

def accepted_write(source, sequence):
    writes.append((source, sequence))
    return True

assert queue.run(source, 3, accepted_write) == "committed"
assert queue.run(source, 3, accepted_write) == "committed"
assert queue.run(source, 2, accepted_write) == "stale"
assert queue.receive("event-1-late", source, 1) == "stale"
assert writes == [(source, 3)]
assert queue.committed[source] == 3
print("committed sequence:", queue.committed[source])

Acknowledge the event and confirm the refresh separately

The fixture runs in memory, one job at a time. In a service, store the accepted event and its pending job together, then acknowledge the delivery. Keep unfinished work available for retry. If a storage timeout leaves the outcome unknown, check the committed revision before deciding whether to retry.

Workers can finish out of order. Check the version again in the write operation, and mark the job complete only after the replacement commits. The atomic replacement and tombstone example covers how to replace visible chunks without leaving a partial revision, and how to record a confirmed removal.

Tags:

AI Agents RAG Knowledge Base Automation Real-Time Data Python LLM Optimization
SearchCans Team

SearchCans Team

SERP API & Reader API Experts

The SearchCans engineering team builds high-performance search APIs serving developers worldwide. We share practical tutorials, best practices, and insights on SERP data, web scraping, RAG pipelines, and AI integration.

Ready to build with SearchCans?

Test SERP API and Reader API with 100 free credits. No credit card required.