Practical technical guides / AI engineering

Switch Embedding Models Behind a Shadow Index and Measure Overlap First

Re-embed into a second index, shadow queries, read overlap as a change detector and labelled hits as the gate, and cut over per query class with a rollback.

By Sandeep Belgavi · Published 2026-11-22

Re-embed into a second index, shadow queries, read overlap as a change detector and labelled hits as the gate, and cut over per query class with a rollback.

Why an embedding change is a migration

Changing the embedding model behind a retrieval system looks like a configuration change: a new model name, maybe a new dimension. It is closer to a database migration. Every stored document vector was produced by the old model, and a vector is only meaningful relative to the model that produced it. A query embedded by the new model and compared with documents embedded by the old one returns rankings that are close to arbitrary, even when the dimensions happen to match and nothing raises an error.

So the change needs the shape of a migration: build the new representation beside the old one, compare them on real traffic and labelled cases, switch readers deliberately, and keep a way back. This guide does that with a shadow index, using two small deterministic embedders as stand-ins for an old and a new model, over sixteen synthetic help articles from the fictional Belgavi's Book Store.

The two stand-in models

Real embedding models would make the example depend on downloads, versions and hardware. The companion code uses two functions that behave like embedders and are fully reproducible:

  • The old model hashes each lowercase word into one of 64 dimensions with a sign, sums, and normalizes to unit length. It is a bag of words.
  • The new model does the same with character trigrams of each word, so "retrun" still shares most of its trigrams with "return".

Both produce 64-dimensional unit vectors, deliberately, so that the dangerous mistake of mixing them is possible and silent. The numbers below show how to read the measurements of a migration; they say nothing about how any real embedding model performs.

Step one: build the shadow index

Re-embed the whole corpus with the new model into a second index. Do not overwrite vectors in place. In a vector database this is a second collection or table; in a library such as FAISS it is a second index file. Record, for each index, the model identifier, its version, the dimension, the normalization and the text preprocessing, because a later change to any of them is another migration.

Re-embedding cost is predictable from the corpus size in tokens, and it is a batch job you can rate-limit. The index build itself can take far longer than the embedding calls for large corpora, so measure it on a sample before scheduling.

Step two: shadow queries

Send each production query to both indexes: serve the old results, log both result lists. The query must be embedded twice, once per model. The cost is one extra embedding call and one extra search per query, which is why shadowing usually runs on a sampled share of traffic.

Two measurements come out of the logs and labels:

  • Overlap at k: the share of the top k results that the two indexes have in common. It needs no labels, so it can run on all shadowed traffic.
  • Labelled hits at k: for queries with known relevant documents, whether a relevant document appears in the top k. It needs a labelled set, usually a few hundred queries per important query class.

Overlap tells you how much the change moves results. Only labels tell you whether the movement is good.

def top_k(qvec, index, k):
    scored = sorted(((sum(a * b for a, b in zip(qvec, dv)), d) for d, dv in index.items()),
                    key=lambda t: (-t[0], t[1]))
    return [d for _, d in scored[:k]]

old_ids = top_k(embed_words(query), old_index, 3)
new_ids = top_k(embed_trigrams(query), new_index, 3)
overlap = len(set(old_ids) & set(new_ids)) / 3

The worked comparison

Sixteen labelled queries are split into two classes: eight in clean wording, such as "reset password", and eight with misspellings, such as "rest pasword". Each has one relevant article. At k = 3:

  • Clean queries: the old index finds the relevant article for 7 of 8, the new index for 8 of 8. Mean overlap between the two lists is 0.42.
  • Misspelled queries: old 4 of 8, new 7 of 8. Mean overlap 0.25.
  • Mixed vectors, a new-model query against old-model documents: 2 of 8 clean and 0 of 8 misspelled.

The overlap numbers are the instructive part. Less than half of the results are shared even for clean queries, where the new index is better. If the team had used overlap as a quality gate ("ship if at least 70 percent of results are unchanged"), it would have blocked an improvement. Overlap is a change detector. It tells you where to look, and where to put labelling effort; it cannot decide.

The mixed result is the failure that a half-finished migration produces: the application is configured with the new model, the documents still have old vectors, and retrieval quietly collapses without an error, because both vectors are 64 floats with unit length.

Step three: cut over per query class

A single switch for all traffic is rarely necessary. Route by query class, and only where the labelled measurement says the new index is at least as good:

def cutover_plan(per_class):
    return {cls: 'new' if c['new_hits'] >= c['old_hits'] else 'old'
            for cls, c in per_class.items()}

In this run both classes move. With real data, it is common for one class to regress, for instance exact product codes or ISBNs, which a word-level representation can match better than a model trained for semantic similarity. Keep that class on the old index, or add a lexical retriever for it, while the rest moves.

Per-class routing is not an exotic precaution. The BEIR benchmark paper evaluated ten retrieval systems, lexical, sparse, dense, late-interaction and re-ranking, on 18 datasets from different tasks and domains, and reported that BM25 remained a robust baseline while dense and sparse retrieval models often underperformed other approaches out of their training distribution. A model that wins on average can lose on your hardest class, and only your own labelled queries reveal which.

Keep the old index serving-ready until the new one has run for a full business cycle. The rollback is a routing change, which is only possible while the old vectors still exist.

What must stay consistent

  • The query embedder and the document index must be chosen together, from one configuration record. Never read the model name from one place and the index name from another.
  • Documents added during the migration must be written to both indexes, or the new index will be missing recent content when you switch. Dual-write from the ingestion path, then backfill.
  • Deletions must also reach both. A document removed from the old index but not the new one comes back after cutover.
  • Cached query embeddings must be keyed by model, or a cache hit will return an old-model vector to a new-model search.

Running two indexes costs something

The shadow period doubles more than storage. Every ingested document is embedded twice, every shadowed query is embedded twice and searched twice, and the comparison logs need retention and a reader. Put numbers on each before starting: document count times average tokens for the backfill, daily ingest for the dual-write, shadow sampling rate times query volume for the extra searches. Then set an end date. A shadow index that runs indefinitely because nobody owns the cutover decision becomes a second production system with no users. A two-week window with a named decision owner, a labelled set agreed in advance and a written threshold per class is usually enough.

Failure modes

  • Dimension mismatch is the lucky case: the search fails loudly. Equal dimensions are the dangerous case.
  • Normalization differences: one model's vectors are unit length and the other's are not, and the index metric is inner product. Scores change scale and any fixed similarity threshold becomes wrong.
  • Tokenization or truncation limits differ, so long documents embed differently. Check the new model's input limit before re-embedding.
  • The labelled set covers only the queries people remember. Sample unlabelled production queries where overlap is lowest and label those first.

When a shadow index is unnecessary

For a small corpus that re-embeds in minutes and a feature with little traffic, a blue-green switch with an offline labelled comparison is enough. For a corpus where retrieval is only a suggestion to a person who checks the result, the risk of a temporary regression may be acceptable. The shadow approach earns its cost when retrieval feeds an agent that acts on what it finds.

Verification

The companion example's five tests pin the labelled hits for each class and index, the overlap values, the mixed-vector collapse, the per-class plan when one class regresses, and that both embedders return unit vectors of the same size. Before your own cutover, add a check that fails the deployment when the configured query model differs from the model recorded with the index.

Run the example yourself

The two stand-in embedders, both indexes, the labelled queries, the overlap and cutover plan, with five tests. Companion code on GitHub; its README lists the prerequisites, exact commands, expected output and limits.

Primary references and authorship

Sources read 22 November 2026. Original explanation, measurements and synthetic examples by Sandeep Belgavi. Views are my own and do not represent any employer, past or present.

Related on this site: