Learning path: AI System Design: Reliable, Fast and Affordable LLM Apps · Step 11 of 15
Embedding Migration With Zero Downtime: Two Indexes, a Dual-Write Backfill, a Shadow Check and a Flag
300 million chunks on an old embedding model, and a new one with a different dimension. Mixing models in one index drops the right document on top from 0.83 to 0.46 with no error (simulated). Size the job, backfill a second index while writes go to both, shadow-compare 18 of 23 agreeing, and flip a flag that a failed id check can refuse.
What you will learn
- Explain why vectors from two embedding models must never share one index
- Size a backfill out loud: time, tokens and storage from chunks, rate and dimensions
- Run a dual-write backfill and check document ids on both sides before switching
- Compare old and new fairly with a shadow check, then flip a flag with a way back
Before you start
- Real Embedding Models: Open or API
- Vector Databases Hands-On: Chroma and pgvector
Here's the interview question. You've got 300 million chunks embedded with an old model, and you want a newer model with a different dimension. How do you move across with zero downtime, and with a way back?
The thirty-second answer is four steps. Keep the two models apart: two indexes, never one. Size the backfill, then run it while every change goes to both. Compare them fairly on the same questions before any user sees the new one. And flip a flag, keeping the old index ready so going back is one more flip. Numbers below are either for illustration or from a small simulation on synthetic documents, and each one says which.
Moving a library without closing it
A big library is moving to a new shelving system, and it can't close for even one day. So you leave the old room alone, and readers keep borrowing from it. You set up a new room next door, and staff carry books across a few shelves at a time. A new book arrives meanwhile, so a copy goes to each room. A book that's thrown out leaves both. Before opening, testers visit both rooms with the same list. Then you change the sign on the door, and keep the old room in order for a while, because if something goes wrong you just change the sign back.
Two models in one index
The shortcut that breaks it: "Why two indexes? Replace the vectors in place, a batch at a time." Halfway through, one index holds vectors from two models. A vector only means something next to vectors from its own model.
== 1. mixing two spaces (no error!) == (simulated)
v1 query on v1 index: 0.83
v2 query on v1 index: 0.67
half v2 vectors in v1 index: 0.46
That's the share of questions with the right document on top. With the old model on both sides it's 0.83. With half the stored vectors swapped to the new model, it falls to 0.46. And nobody got an error. If the new model has a different dimension you'll probably get a loud failure, and that's the lucky case. With the same dimension nothing stops you, and answers just get worse.
How long, and how much
Interviewers like it when you size the job out loud. This is arithmetic for illustration: 300 million chunks, 200 tokens each, 1,000 chunks a second, 768 and 1,024 dimensions. Not from a run, not a benchmark, and no price quoted.
| What | Formula | For illustration |
|---|---|---|
| Time | chunks / embedding rate | 300M / 1,000 per s = about 83 hours, roughly 3.5 days |
| Cost | tokens x price per token | 300M x 200 = 60 billion tokens, times your own provider's price |
| Storage | both indexes side by side | about 2.15 TB of raw vectors, before index overhead |
One more line: you pay for each chunk once, plus every change that arrives while the backfill runs.
Backfill while the writes keep coming
The old index keeps serving, and the new one starts empty. From now on every update and every delete goes to both. That's a dual write. Meanwhile, workers copy older documents across in batches.
== 2. dual write + backfill (simulated) ==
after batch 1, left: 16
update + delete arrived, left: 15
backfill, left: 7
backfill, left: 0
sync: {'missing': [], 'extra': [], 'stale': []}
Sixteen becomes fifteen because the deleted document never needs copying. The total is 24 embeddings for 23 documents, because the update got embedded twice. Before you trust it, compare document ids on both sides, looking for anything missing, extra or out of date. Here all three lists are empty.
Comparing A and B fairly
The same questions go to both indexes, and each index gets a query embedded by its own model, while users still get the old answer. That's a shadow check.
== 3. shadow evaluation == (simulated)
questions: 23, agree: 18
recall@1 v1: 0.83
recall@1 v2: 0.87
The two indexes agree on the top document for 18 of 23 questions. The old one scores 0.83 and the new one 0.87. That gap is one question out of 23. It doesn't prove the new model is better; it only says it isn't obviously broken. So read the five where the top answer changed. For scoring retrieval more fully, see Retrieval Evals.
The flag, and the delete that skipped
The switch itself is one flag. Flip it and users move across; flip it back and they're on the old index, with nothing to embed again. That only works if you keep writing to the old index after the flip, or it's out of date the moment you go back.
Now the failure test. We simulated a bug where deletes skip the new index:
== 5. delete skips v2 (simulated bug) ==
deleted: invoice
sync: {'missing': [], 'extra': ['invoice'], 'stale': []}
cutover refused
forced cutover returns: ['invoice']
The id comparison finds one extra, so the flip is refused. Force it anyway, ask where to find a payment receipt, and back comes the deleted invoice. So the flip has to depend on that check, because a deleted document coming back can be a privacy problem, not just a wrong answer.
The follow-ups
These three are design answers; they weren't simulated.
- What about cached results? They came from the old model, and a semantic cache even stores old query vectors. Put the embedding version in the cache key, and expect a cold cache after the flip. See Caching for AI Apps and Semantic Caching.
- Can the backfill slow down users? Yes, if it shares a quota with search. Give it its own limit and throttle it.
- How long do you keep the old index? Decide up front, and keep writing to it until then, knowing you're paying for two.
| Level | What it sounds like |
|---|---|
| Weak | "Embed everything again and switch over." |
| Good | The models can't mix; two indexes, a backfill and a flag. |
| Strong | Also shows the formula, keeps deletes in step, blocks the flip on an id check, and doesn't oversell a small gap. |
Recap
| Step | Remember |
|---|---|
| Keep them apart | Mixed models: 0.83 to 0.46, and no error (simulated) |
| Size it | Time = chunks / rate; cost = tokens x price; storage for both |
| Backfill | Dual write, copy in batches, compare ids on both sides |
| Compare and flip | Shadow check, read the changed answers, flag with a way back |
Try it yourself
Pick a document store you know. Estimate its chunks, tokens per chunk and an embedding rate, and work out the backfill time and the raw vector storage for both indexes.
Related course on Udemy
Machine Learning Engineer: From First Model to ML Engineer
Learn machine learning in Python, then ship it: data analysis, regression, classification, tuning, deployment and MLOps.View on Udemy ↗: Machine Learning Engineer: From First Model to ML Engineer (paid course, opens in a new tab)Related posts
The RAG Pipeline Explained: 7 Stages From Documents to a Cited Answer
Ingest, chunk, embed, store, retrieve, rerank and generate, in one Python file. Follow one question through a 26-chunk employee handbook to an answer that cites its source.
Similarity Search: Finding the Closest Vectors with NumPy and NearestNeighbors
Ask one question of a whole collection of vectors: which items are closest to this one? Normalise, score every row with one matrix multiply, keep the top k or everything above a threshold, and check it with NearestNeighbors.
Why RAG Exists: The Problem It Solves, Compared With Pasting Everything and Fine-Tuning
A smart model has never read your company's files, and they change every month. Three ways to give it the facts: paste everything, fine-tune, or retrieve only the right pieces. Follow a refund-policy question through all three and see why RAG wins when documents change.
Real Embedding Models: an Open Model or a Provider API, Then Search, Duplicates and Recommendations
Swap a toy embedder for a model that learned. Two roads (all-MiniLM-L6-v2 on your machine, or OpenAI's API with your key), three sizes (384, 1,536 and 3,072 numbers), one rule, and three use cases built on the same rank function, with real scores.