Scaling Production RAG Pipelines: Hybrid Vector Search, Tenant Isolation, and LLM Fallback Routing
Shafikul Islam
Backend & Cloud Infrastructure Engineer · Infrastructure & Cloud Team Lead

Why 90% of Prototype RAG Pipelines Fail in Production
A basic Retrieval-Augmented Generation (RAG) tutorial embeds chunks into a vector database, runs cosine similarity, and sends the top 5 chunks to a single OpenAI endpoint.
In production B2B and healthcare SaaS (emmai.ca), that naive pipeline breaks in three ways:
- Pure Cosine Similarity Misses Exact Identifiers: Dense embeddings are great for semantic concepts, but terrible at matching exact medical ICD-10 codes, drug dosages, or part numbers.
- Cross-Tenant Context Leakage: Filtering tenant permissions after vector retrieval risks exposing another customer's document chunks if the top-K filter is misconfigured.
- Upstream LLM Rate Limits and HTTP 529 Brownouts: Relying on a single model endpoint causes user-facing timeouts whenever the provider experiences regional capacity spikes.
1. Hybrid Retrieval: Reciprocal Rank Fusion (Dense + BM25 Sparse)
To capture both semantic meaning and exact clinical/technical terminology, we combined dense embeddings with sparse BM25 keyword matching and merged the results using Reciprocal Rank Fusion (RRF):
from typing import List, Dict
def reciprocal_rank_fusion(
dense_hits: List[str],
sparse_hits: List[str],
k: int = 60,
) -> List[str]:
scores: Dict[str, float] = {}
for rank, doc_id in enumerate(dense_hits, start=1):
scores[doc_id] = scores.get(doc_id, 0.0) + 1.0 / (k + rank)
for rank, doc_id in enumerate(sparse_hits, start=1):
scores[doc_id] = scores.get(doc_id, 0.0) + 1.0 / (k + rank)
return sorted(scores.keys(), key=lambda d: scores[d], reverse=True)
2. Hard Pre-Filtering by Tenant ID at the Vector Index Layer
In a multi-tenant system, security boundaries must be enforced inside the vector index query itself, never in application memory after retrieval:
vector_results = await vector_index.query(
vector=query_embedding,
top_k=12,
filter={
"tenant_id": {"$eq": authenticated_tenant_id},
"document_status": {"$eq": "ACTIVE"},
},
include_metadata=True,
)
3. Deterministic LLM Circuit Breaking and Provider Failover
We wrapped our FastAPI completion client in a 3.5-second time-to-first-token (TTFT) deadline with automatic fallback routing across primary and secondary model providers. When the primary LLM returns HTTP 429 or exceeds the 3.5s TTFT threshold, the router transparently switches to the fallback provider, maintaining 99.94% RAG endpoint availability.