Reliability in a managed vector index means something different from the rest of this programme. There is no cluster to fail over, no replica lag to monitor, no node to replace and no repair to schedule. Pinecone handles availability. What remains yours is the data and the pipeline that produced it - and specifically, the fact that your vectors are derived data.
That framing is the module's foundation: every vector in your index is the output of (a document, a chunking strategy, an embedding model). If you still have the documents and the pipeline, you can always rebuild the index, and your real RPO is how fresh your document store is, not how recently Pinecone was backed up. A vector deployment whose team cannot rebuild the index from source is in a much worse position than one that can, regardless of backups.
Backups and collections. Pinecone supports creating a backup (historically a "collection") - a static snapshot of an index that you can restore into a new index. It is useful for a point-in-time copy before a destructive change, for moving between configurations, and for keeping a known-good state during a migration. The limits: a restore creates a new index, so there is a cutover step; the backup is a snapshot rather than continuous, so the RPO is the backup interval; and availability varies by plan and index type, so check what your account actually supports rather than assuming.
Reindexing is routine here in a way it is not elsewhere. You reindex when you change chunking, when you change the metric, when you add a metadata field you want to filter on, when your corpus changes shape - and most consequentially, when you change embedding model.
The embedding model migration is the operational event that defines this course's reliability module. Changing model changes the dimension, which means a new index, which means re-embedding every chunk. For a large corpus that is hours of GPU time and a meaningful cost. The pattern that works is blue-green:
- Create the new index with the new dimension and the correct metric for the new model.
- Re-embed the corpus into it, in batches, from the document store - not from the old index, because you cannot un-embed a vector.
- Evaluate the new index against the same ground truth. This is the step that justifies the migration, and occasionally the step that cancels it, because a newer model is not automatically better on your corpus.
- Shift read traffic gradually, comparing live metrics.
- Keep the old index until you are confident, then delete it.
Two practical points. Dual-write during the transition if documents are changing, so the new index does not fall behind. And the ground-truth ids change if the chunking changed, so a migration that alters both model and chunking needs the ground truth re-mapped - which is an argument for changing one thing at a time.
Retry behaviour. The client retries some failures; you must handle rate limits (back off), transient errors (retry), and partial batch failures (retry the failed items). Because upsert is idempotent with deterministic ids, retrying is safe - which is one of the quieter benefits of the id discipline from Module 2.
Laboratory exercise 5 - rebuild it, then migrate it.
Establish the rebuild capability first: delete your index entirely and rebuild it from the document store and the pipeline. Time it, and record that number as your true RTO. Then state your RPO as the freshness of the document store.
Create a backup, make a destructive change, and restore from it. Record RPO and RTO for that path too, and compare with the rebuild.
Perform the embedding model migration as a blue-green operation. Choose a second model with a different dimension. Create the new index, re-embed, evaluate both against the same ground truth, and record the comparison. If the new model is worse on your corpus, say so - that is a valid and instructive outcome.
Demonstrate the dual-write requirement: ingest new documents during the migration without dual-writing and show the new index missing them. Then implement dual-write.
Demonstrate the ground-truth remapping problem: change the chunking at the same time as the model and show that the ground-truth ids no longer match anything.
Implement correct retry handling and demonstrate it against injected rate limits and transient failures.
Deliverable: the rebuild with a measured time stated as the RTO, the backup and restore path with its own RPO and RTO, the full blue-green migration with the two-model evaluation comparison, the dual-write demonstration, the remapping problem, and the retry implementation with evidence.
# ==================================================================
# *** THE FRAMING: YOUR VECTORS ARE DERIVED DATA. ***
# ==================================================================
# Every vector is the output of:
# (a document, a chunking strategy, an embedding model)
#
# *** IF YOU STILL HAVE THE DOCUMENTS AND THE PIPELINE, YOU CAN
# *** ALWAYS REBUILD THE INDEX. *** So your real RPO is HOW FRESH
# YOUR DOCUMENT STORE IS, not how recently Pinecone was backed up.
#
# A team that cannot rebuild the index from source is in a much
# worse position than one that can, regardless of backups. SO PROVE
# THE REBUILD, FIRST, BEFORE ANYTHING ELSE IN THIS MODULE.
# ------------------------------------------------------------------
# ESTABLISH THE REBUILD CAPABILITY. This number is your RTO.
# ------------------------------------------------------------------
import time
t0 = time.perf_counter()
pc.delete_index("docs-v1")
pc.create_index(name="docs-v1", dimension=DIM, metric=METRIC,
spec=ServerlessSpec(cloud="aws", region="us-east-1"))
while not pc.describe_index("docs-v1").status["ready"]:
time.sleep(2)
index = pc.Index("docs-v1")
for ns, docs in documents_by_tenant().items(): # FROM THE
upsert_batched(index, # DOCUMENT STORE
(v for d in docs
for v in build_vectors(d, model, ns)),
namespace=ns, batch=100)
print("REBUILD RTO:", (time.perf_counter() - t0) / 60, "minutes")
# -> "RTO: 47 minutes, measured, for 11,208 vectors. RPO: the
# freshness of the document store, currently 5 minutes."
# That is an engineering statement. "Pinecone is managed" is not.
# ------------------------------------------------------------------
# BACKUPS: a snapshot, restored into a NEW index.
# ------------------------------------------------------------------
# *** CHECK WHAT YOUR ACCOUNT AND INDEX TYPE ACTUALLY SUPPORT
# *** RATHER THAN ASSUMING - availability varies by plan. ***
#
# backup = pc.create_backup(index_name="docs-v1",
# backup_name="docs-v1-pre-migration")
# pc.list_backups()
# pc.create_index_from_backup(backup_id=backup.id,
# name="docs-v1-restored")
#
# THE LIMITS:
# * a restore creates a NEW INDEX -> there is a CUTOVER step
# * it is a SNAPSHOT, not continuous -> the RPO is the backup
# interval
# * so for a derived index it is usually FASTER AND SIMPLER TO
# REBUILD than to restore - which is why the rebuild time is
# the number that matters. Measure both and compare.
# ==================================================================
# *** THE EMBEDDING MODEL MIGRATION. The event that defines this
# *** module. ***
# ==================================================================
# Changing model changes THE DIMENSION -> a NEW INDEX -> RE-EMBEDDING
# EVERY CHUNK. For a large corpus that is hours of compute and a
# meaningful cost. BLUE-GREEN is the pattern:
NEW_MODEL = "sentence-transformers/all-MiniLM-L12-v2"
new_model = SentenceTransformer(NEW_MODEL)
NEW_DIM = new_model.get_sentence_embedding_dimension() # 384
NEW_METRIC = "cosine" # READ THE NEW MODEL'S DOCUMENTATION.
# Do not carry the old metric across.
# STEP 1: the new index.
pc.create_index(name="docs-v2", dimension=NEW_DIM, metric=NEW_METRIC,
spec=ServerlessSpec(cloud="aws", region="us-east-1"))
new_index = pc.Index("docs-v2")
# STEP 2: re-embed FROM THE DOCUMENT STORE.
# *** NOT FROM THE OLD INDEX - YOU CANNOT UN-EMBED A VECTOR. ***
# This is why the document store is the source of truth and the
# index never is.
for ns, docs in documents_by_tenant().items():
upsert_batched(new_index,
(v for d in docs
for v in build_vectors(d, new_model, ns)),
namespace=ns, batch=100)
# STEP 3: *** EVALUATE BOTH AGAINST THE SAME GROUND TRUTH. ***
old = evaluate(Config("docs-v1", MODEL_NAME, CHUNKING_VERSION, "cosine"),
GROUND_TRUTH)
new = evaluate(Config("docs-v2", NEW_MODEL, CHUNKING_VERSION, "cosine"),
GROUND_TRUTH)
print(old["recall@5"], old["mrr"], "->", new["recall@5"], new["mrr"])
#
# docs-v1 (mpnet, 768) recall@5 0.91 mrr 0.84 query_p95 14 ms
# docs-v2 (MiniLM, 384) recall@5 0.87 mrr 0.79 query_p95 8 ms
#
# *** THIS IS THE STEP THAT JUSTIFIES THE MIGRATION - AND
# *** OCCASIONALLY THE STEP THAT CANCELS IT. A NEWER OR LARGER MODEL
# *** IS NOT AUTOMATICALLY BETTER ON YOUR CORPUS. *** Here the
# smaller model is faster and measurably worse; whether that trade is
# right is YOUR decision, and now it is an informed one.
# IF THE NEW MODEL IS WORSE, SAY SO. That is a valid and instructive
# laboratory outcome.
# STEP 4: shift read traffic GRADUALLY, comparing live metrics.
# 5% -> 25% -> 50% -> 100%, with the evaluation harness run against
# live queries at each step.
# STEP 5: keep docs-v1 until confident, THEN delete it.
# pc.delete_index("docs-v1")
# ------------------------------------------------------------------
# *** DUAL-WRITE DURING THE TRANSITION. ***
# ------------------------------------------------------------------
def ingest(doc):
# If documents change during a migration that takes hours, the
# new index FALLS BEHIND - and the gap is invisible until you
# cut over and users cannot find recent documents.
for v in build_vectors(doc, model, doc["tenant"]):
index.upsert(vectors=[v], namespace=doc["tenant"])
for v in build_vectors(doc, new_model, doc["tenant"]):
new_index.upsert(vectors=[v], namespace=doc["tenant"])
# Demonstrate it BOTH ways: ingest during the migration without
# dual-writing and show the new index missing those documents.
# ------------------------------------------------------------------
# *** THE GROUND-TRUTH REMAPPING PROBLEM. ***
# ------------------------------------------------------------------
# The ground truth records CHUNK IDS. If you change the CHUNKING,
# those ids no longer exist:
# ground truth says: "doc-0001#c0003"
# new chunking makes: "doc-0001#c0000", "doc-0001#c0001"
# -> recall@5 = 0.0, and it looks like a catastrophic regression
#
# *** WHICH IS A STRONG ARGUMENT FOR CHANGING ONE THING AT A TIME. ***
# If you must change both, re-map the ground truth to DOCUMENT ids
# and evaluate at document granularity:
def recall_at_k_by_doc(hits, relevant_docs, k=5):
got = {h["metadata"]["doc_id"] for h in hits[:k]}
return len(got & relevant_docs) / max(1, len(relevant_docs))
# Document-level ground truth is more robust to re-chunking and is
# worth maintaining alongside the chunk-level one for exactly this
# reason.
# ==================================================================
# RETRY BEHAVIOUR: and why the id discipline pays off here.
# ==================================================================
import random
def with_retry(fn, *args, attempts=5, **kwargs):
for i in range(attempts):
try:
return fn(*args, **kwargs)
except Exception as e:
msg = str(e).lower()
retryable = any(s in msg for s in
("rate limit", "429", "503", "timeout", "unavailable",
"too many requests"))
if not retryable or i == attempts - 1:
raise
time.sleep(min(2 ** i * 0.25, 8) * (0.5 + random.random() / 2))
# *** BECAUSE UPSERT IS IDEMPOTENT WITH DETERMINISTIC IDS, RETRYING
# *** IS SAFE. *** That is one of the quieter benefits of the id
# discipline from Module 2 - a retried batch rewrites the same
# vectors rather than duplicating them, so you can retry
# aggressively without thinking about it.
#
# Handle THREE classes separately:
# RATE LIMIT back off; the request was refused, not applied
# TRANSIENT retry; may or may not have applied, and
# idempotency makes that not matter
# PARTIAL BATCH retry THE FAILED ITEMS, not the whole batch
#
# Demonstrate each against injected failures.
Open this example in the editor and change it. Nothing you type is saved.
Discussion
No questions on this topic yet. Sign in to ask the first one.