Skip to content

RAG as Data Infrastructure, Not Feature

Retrieval-augmented generation fails when it is built as a self-contained chatbot feature and works when it is treated as data infrastructure. Rebuilding one SaaS platform’s system on change-data-capture sync, permission-aware retrieval, hybrid search with reranking, and evaluation made retrieval a platform capability.

01

What problem did this system solve?

A B2B SaaS company serving a large base of enterprise customers had bolted a “chat with your data” feature onto their existing product. The implementation followed the standard tutorial pattern: embed documents, store in Pinecone, retrieve the top chunks, pass to GPT-4 with a system prompt. Within months, support tickets related to incorrect AI answers consumed a significant share of engineering time. Customer trust in the feature dropped sharply based on in-app feedback ratings.

The core problem was architectural, not algorithmic. The RAG system had been designed as a feature: a self-contained module with its own data pipeline, its own embedding logic, and its own retrieval stack. It shared nothing with the platform’s existing data infrastructure. When a customer updated a document in the main application, the RAG index lagged by hours. When a customer’s access permissions changed, the RAG system had no awareness. When product data schemas evolved, the embedding pipeline broke silently.

I was brought in to rebuild the system. The mandate was not “fix the chatbot.” It was “make retrieval a platform capability.”

02

How was the architecture designed?

The redesign treated RAG as a data infrastructure layer with a short set of architectural principles: shared data pipelines, real-time synchronization, permission-aware retrieval, and evaluation-driven iteration.

The first decision was to eliminate the separate embedding pipeline entirely. Instead of a batch job that re-processed documents on a schedule, I integrated embedding generation into the platform’s existing change-data-capture (CDC) stream. Every document mutation (create, update, delete) in the primary PostgreSQL database triggered a Debezium event that flowed through Kafka to an embedding service. The embedding service chunked the document using a hybrid strategy (semantic paragraph boundaries with a token maximum and a fixed overlap), generated embeddings via a locally-hosted BGE-large model, and wrote the vectors to a Qdrant cluster. End-to-end latency from document change to searchable vector: seconds on average.

Permission-aware retrieval was the second major architectural change. The original system retrieved documents regardless of whether the querying user had access to them in the main application. This was both a security vulnerability and a trust problem. I implemented a metadata filtering layer where every vector stored its associated permission scope (tenant ID, team IDs, user-level access flags) as payload metadata. At query time, the retrieval layer constructed a Qdrant filter clause from the user’s active permissions before executing the similarity search. This added a small amount of latency but eliminated an entire class of data leakage issues.

The retrieval pipeline itself was redesigned as a multi-stage process. First, a hybrid search combining dense vector similarity (BGE-large embeddings) with sparse keyword matching (BM25 via Elasticsearch) to handle both semantic and exact-match queries. Second, a cross-encoder reranker (a fine-tuned model trained on labeled relevance pairs from the platform’s domain) that reordered the top candidates. Third, a context assembly stage that formatted the top results with source metadata, confidence scores, and structured section boundaries.

I deployed the embedding service as a horizontally scalable Kubernetes deployment with autoscaling based on Kafka consumer lag. The Qdrant cluster ran on multiple nodes with replication. The entire infrastructure was observable via Prometheus metrics and Grafana dashboards tracking embedding throughput, retrieval latency percentiles, reranker accuracy, and end-to-end query quality scores.

03

What were the measurable outcomes?

The cost reduction came from several sources. First, switching from OpenAI’s hosted embedding API to a self-hosted BGE-large model eliminated per-query embedding costs entirely. The BGE model ran on dedicated A10 GPUs at a fixed monthly cost, processing a high volume of embedding requests every day. Second, the reranker substantially reduced the number of tokens sent to the generation model by selecting more relevant context, which translated directly to lower inference costs. Third, I replaced GPT-4 with Claude 3.5 Sonnet for the generation step after evaluation showed equivalent quality at a lower per-token cost for this specific task.

The accuracy improvement came primarily from real-time synchronization (eliminating stale data), the cross-encoder reranker (markedly improving retrieval precision), and structured context assembly (reducing model confusion from poorly formatted inputs).

04

What would I change in hindsight?

I underinvested in the evaluation infrastructure during the early phase. I built test cases before launch, but the majority were synthetic. I should have invested the time to curate a set of real user queries with human-judged relevance labels from day one. The synthetic test cases caught obvious failures but missed subtle domain-specific accuracy issues that only surfaced after production deployment. It took extended post-launch iteration to close the gap.

I also should have implemented A/B testing infrastructure from the start. When I wanted to compare the BM25+dense hybrid approach against dense-only retrieval, I had to run offline evaluations because there was no mechanism for live traffic splitting. Building the A/B framework cost extra time that could have been saved with upfront planning.

The broader lesson is that RAG systems are not chatbot features. They are data infrastructure. They touch indexing, permissions, real-time synchronization, search, and evaluation. Treating them as a feature leads to the same problems as treating your database as a feature: it works until scale and complexity reveal every shortcut you took. The organizations that will build reliable AI applications are the ones that recognize retrieval as a platform capability and invest in it accordingly.