Skip to content
mentoor

Topic 1 of 4 · 4 min read

Incremental sync from source systems

Goal: Design an incremental ingestion job that re-embeds only changed documents and prove it with a run log.

Re-embed only what changed: give every document a stable id and a content hash, compare it with what the index remembers, and send only the differences through the parser and the embedding model.

Check yourself · 1 of 3

Why is a content hash a better change signal than a file's modified date?

Choose one answer

Why it matters

Parsing and embedding are the slow, paid steps. A nightly job that rebuilds 200,000 pages to pick up 300 edits wastes hours and API spend, and it makes the index briefly inconsistent while it runs. A job that touches only the 300 is cheap enough to run every few minutes, so answers stay current.

How to do it

  1. Pick a stable key per document. Use the source's own id (a Drive file id, a Confluence page id, a canonical URL), not the file name. Names change; ids do not.
  2. Hash the content, not the timestamp. A modified date also moves when someone opens and saves without editing. A SHA-256 of the extracted text (or of the raw bytes) changes only when the content does.
  3. Keep a manifest. One row per document: id, content hash, chunk ids, last synced time. This is the index's memory.
  4. Diff on each run. New id means add. Same id and different hash means replace. Id in the manifest but missing from the source means delete (next topic).
  5. Replace, never append. When a document changes, write its new chunks under deterministic ids (doc-id:chunk-index), then delete any of its old chunk ids that were not rewritten. A re-run is then safe, and the document is never missing.
  6. Use events when the source offers them. Webhooks and change feeds tell you what moved. Keep a periodic hash scan as well, because webhooks get dropped.

Before and after

# Before: re-embeds everything on every run
for doc in source.list_all():
    chunks = split(parse(doc))
    index.upsert(embed(chunks))
# After: skip unchanged documents
for doc in source.list_all():
    text = parse(doc)
    h = sha256(text.encode()).hexdigest()
    old = manifest.get(doc.id, {})
    if old.get("hash") == h:
        continue
    chunks = split(text)
    ids = [f"{doc.id}:{i}" for i in range(len(chunks))]
    index.upsert(ids=ids, vectors=embed(chunks))
    stale = set(old.get("chunk_ids", [])) - set(ids)
    index.delete(ids=list(stale))
    manifest[doc.id] = {"hash": h, "chunk_ids": ids}

What goes wrong

  • Timestamp-only detection. Re-saves trigger needless work, and sources with unreliable clocks hide real edits.
  • Hashing before normalisation. A changing footer date or tracking id in the HTML makes every page look new. Hash the cleaned text.
  • Changing the parser or embedding model without invalidating. The hashes match but the stored vectors are from the old setup. Store a pipeline version next to the hash and treat a version bump as a full rebuild.
  • Half-finished runs. If the job crashes after deleting old chunks and before writing new ones, the document vanishes. Write the new chunks first, then remove the old ones, or record progress in the manifest.

Examples

A run log that proves it works

sync 02:00  scanned=18204 unchanged=18171 new=9 changed=24 deleted=0
sync 02:00  parsed=33 embedded_chunks=212 skipped=18171
sync 02:00  duration=94s

The job saw 18,204 documents and touched 33. If embedded_chunks equals the whole corpus every night, change detection is broken.

The footer that defeated the hash

Every crawled page ended with Generated 2026-11-30 02:00. The raw HTML hash changed nightly, so all 18,000 pages were re-embedded each run. Stripping the footer before hashing cut the nightly embeds to the few dozen pages that really changed.

Key points

  • Key every document by the source's stable id, never by file name.
  • Hash the cleaned content: a modified date also moves on saves that change nothing.
  • A manifest of id, hash and chunk ids is the index's memory and drives add, replace and delete decisions.
  • Store a pipeline version with each hash so a new parser or embedding model triggers re-embedding.
  • Write new chunks before removing old ones, so a crash never leaves a document missing.

Common mistakes

  • Using the modified timestamp as the only change signal.
  • Hashing raw HTML that contains dates, session ids or ads.
  • Keeping the old hash after changing the chunker or embedding model.
  • Deleting a document's old chunks before the new ones are safely written.

Check yourself · 2 of 3

A manifest records id, hash and chunk ids per document. What is the main use of the chunk ids?

Choose one answer

Check yourself · 3 of 3

You switch to a new embedding model but every document hash is unchanged. What should the sync job do?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic