Skip to content
About Me
----

Enterprise RAG Architecture: Vector Databases, Chunking & Semantic Caching

Architect Key Takeaways

Production guide to Retrieval-Augmented Generation (RAG) infrastructure, covering Milvus/Qdrant vector databases, HNSW index tuning, hybrid sparse/dense retrieval, and Redis semantic caching.

Enterprise RAG Architecture: Vector Databases, Chunking & Semantic Caching

Section titled โ€œEnterprise RAG Architecture: Vector Databases, Chunking & Semantic Cachingโ€

While Large Language Models demonstrate impressive reasoning capabilities, off-the-shelf models suffer from two critical enterprise flaws:

  1. Knowledge Cutoff & Stale Information: Models know nothing about your companyโ€™s private confluence wikis, proprietary APIs, or internal architectural standards.
  2. Hallucinations: When an LLM lacks ground truth, it invents plausibly sounding but false facts.

Retrieval-Augmented Generation (RAG) solves this by retrieving precise, validated private enterprise documents from a vector database and injecting them into the LLMโ€™s prompt context window in real time.


[ Private Documentation (PDFs, Markdown, Confluence) ]
โ”‚
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Ingestion & Indexing Pipeline โ”‚
โ”‚ โ”œโ”€ Smart Chunking (Recursive character splitting) โ”‚
โ”‚ โ”œโ”€ Embedding Generation (e.g. text-embedding-3-large) โ”‚
โ”‚ โ””โ”€ Vector Store (Milvus / Qdrant with HNSW Index) โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ”‚
[ Live User Query ] โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€> [ Semantic Cache (Redis) ]
โ”‚ (Cache Miss)
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Hybrid Retrieval & Re-Ranking Engine โ”‚
โ”‚ โ”œโ”€ Dense Vector Search (Cosine Similarity) โ”‚
โ”‚ โ”œโ”€ Sparse Keyword Search (BM25 for exact codes/IDs) โ”‚
โ”‚ โ””โ”€ Cross-Encoder Re-Ranker (Cohere / BGE-Reranker) โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ”‚
โ–ผ (Top 3 Context Chunks)
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ LLM Generation (vLLM / Claude / OpenAI) โ”‚
โ”‚ "Based on the provided context, answer the query..." โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

2. Choosing & Tuning Vector Databases: Milvus vs Qdrant vs pgvector

Section titled โ€œ2. Choosing & Tuning Vector Databases: Milvus vs Qdrant vs pgvectorโ€
Vector Engine Architecture Best Production Use Case Index Algorithm
Qdrant Rust-based, cloud-native container High-speed semantic search, payload metadata filtering HNSW (Hierarchical Navigable Small World)
Milvus Distributed microservices on Kubernetes Enterprise scale (>100M to Billions of vectors) IVF-PQ, DiskANN, GPU-accelerated indexing
pgvector PostgreSQL extension Small to medium projects (<1M vectors) already using Postgres HNSW / IVFFlat inside relational DB
from qdrant_client import QdrantClient
from qdrant_client.models import Distance, VectorParams, PointStruct
# Connect to production Qdrant cluster on Kubernetes
client = QdrantClient(url="http://qdrant.database.svc.cluster.local:6333")
COLLECTION_NAME = "enterprise_runbooks"
# Create collection with Cosine similarity
client.recreate_collection(
collection_name=COLLECTION_NAME,
vectors_config=VectorParams(size=1536, distance=Distance.COSINE)
)
def search_production_context(query_vector: list[float], team_tag: str, top_k: int = 5):
"""Performs HNSW vector search with strict metadata filtering."""
search_result = client.search(
collection_name=COLLECTION_NAME,
query_vector=query_vector,
query_filter={
"must": [
{"key": "team", "match": {"value": team_tag}}
]
},
limit=top_k
)
return [hit.payload["content"] for hit in search_result]

3. Cost & Latency Optimization: Semantic Caching with Redis

Section titled โ€œ3. Cost & Latency Optimization: Semantic Caching with Redisโ€

In production systems, users repeatedly ask semantically identical questions (e.g., โ€œHow do I connect to the production VPN?โ€ vs โ€œSteps to access prod VPNโ€).

Sending every duplicate query to external embedding models and LLM inference endpoints wastes thousands of dollars. A Semantic Cache computes query similarity and returns cached answers instantly:

import redis
import numpy as np
# Redis Enterprise / Redis Stack with vector capability
r = redis.Redis(host="redis-cache.caching.svc", port=6379)
def get_semantic_cached_response(query_embedding: list[float], threshold: float = 0.96):
"""Checks Redis for previously answered queries with >96% semantic similarity."""
query_bytes = np.array(query_embedding, dtype=np.float32).tobytes()
# Query Redis vector index
q = "*=>[KNN 1 @vector $vec AS score]"
res = r.ft("cache_idx").search(q, query_params={"vec": query_bytes})
if res.docs:
top_doc = res.docs[0]
similarity = 1.0 - float(top_doc.score)
if similarity >= threshold:
print(f"[CACHE HIT] Returning instant 5ms cached LLM response (Similarity: {similarity:.3f})")
return top_doc.answer
print("[CACHE MISS] Executing full RAG retrieval pipeline...")
return None

  1. Avoid Naive Fixed-Character Chunking: Chopping text arbitrarily at 500 characters breaks code blocks and sentence semantics in half. Use Semantic Chunking or recursive markdown header splitting (MarkdownHeaderTextSplitter) so sections stay cohesive.
  2. Always Implement Hybrid Search: Vector embeddings are great at high-level semantic meaning, but terrible at matching specific UUIDs, error codes (ERR_CONN_REFUSED_503), or exact server hostnames. Always combine dense vector search with sparse BM25 keyword search.
  3. Monitor Context Window Cost: Calculate and log token consumption using Prometheus. Injecting 50 documents into an LLM context window causes severe latency degradation and skyrocketing token costs.