This article discusses the architectural challenge of designing robust retrieval-augmented generation (RAG) systems by highlighting the limitations of pure vector or keyword search. It proposes a hybrid search approach using Reciprocal Rank Fusion (RRF) to combine both sparse (BM25) and dense (vector) retrieval methods. This pattern enhances retrieval accuracy by mitigating failure modes associated with single search strategies, crucial for production-grade AI systems.
Read original on Dev.to #systemdesignIn the realm of Retrieval-Augmented Generation (RAG) systems, effective document retrieval is paramount. However, relying solely on a single search mechanism, either semantic vector search or traditional keyword-based (BM25) search, introduces significant limitations. Vector search excels at conceptual understanding but struggles with exact matches like product IDs or error codes. Conversely, BM25 is strong with keywords but fails when users paraphrase queries, leading to 'blind spots' in retrieval.
To overcome these challenges, a robust system design for RAG pipelines incorporates a hybrid search architecture. This involves executing both sparse (e.g., BM25) and dense (e.g., vector) searches concurrently. The key architectural component for merging their results is Reciprocal Rank Fusion (RRF).
┌───> [ BM25 Keyword Search ] ───> Sparse Ranked List ───┐
[ User Query ] ──────┤ ├───> [ RRF Fusion ] ───> Top-K Documents ───> LLM
└───> [ Dense Vector Search ] ───> Dense Ranked List ───┘Why RRF for Fusion?
RRF is particularly effective because it sidesteps the complex problem of score normalization. Different search algorithms produce scores on vastly different scales (e.g., unbounded positive floats for BM25 vs. cosine similarities between -1 and 1 for vector search). RRF simplifies this by focusing purely on the relative rank of documents from each individual search system, making the fusion process more stable and less reliant on fragile heuristic scaling factors.
The RRF score for a document $d$ is calculated using the formula:$$RRF(d) = \sum_{m \in M} \frac{1}{k + r_m(d)}$$Where $M$ is the set of retrieval systems (e.g., BM25 and Vector Search), $r_m(d)$ is the 1-based rank position of document $d$ in system $m$, and $k$ is a smoothing constant (typically 60) that prevents low-ranking outliers from disproportionately influencing the final score. This method ensures that a document ranked highly by even one system contributes significantly to its final RRF score, improving overall recall and precision.
This architectural pattern provides resilience, ensuring that whether a user inputs a precise technical term or a high-level conceptual query, the RAG system can effectively retrieve the most relevant documents, thereby improving the quality of downstream LLM generations.