Build a Sparse-Dense Hybrid Index (SPLADE + Dense) for Semantic Search
databases scalability performance
AI System Design Deep Dive
Sparse-Dense Hybrid Index
Dense vectors miss exact terms; learned sparse weights catch them. Fuse the two on one scale.
A support-search product answers 6,000 queries per second at peak against 60 million passages: knowledge-base articles, ticket resolutions, API docs. The p99 budget is 40ms for retrieval. Half the queries are paraphrases (“my card keeps getting declined overseas”) and the other half hide an exact token that must match (“ERR_4417”, “SKU-88213”, “RFC 7231”). One retrieval path cannot serve both.
Think of a library with two catalogs. The subject catalog files a book under what it is about, so “declined overseas” finds the foreign-transaction chapter even though neither word appears in it. The index at the back of every book files it under the exact words printed on the page, so “ERR_4417” finds the one troubleshooting table that mentions it. A good librarian consults both and has to decide whose opinion counts more when they disagree. The hard part is not running two searches. It is that the two catalogs speak in different units.
A dense bi-encoder is the subject catalog: one 384-dimension vector per passage, compared by cosine similarity. A learned sparse model such as SPLADE is the book index with a brain: a 30,522-dimension vector per passage (one dimension per BERT wordpiece), almost all zeros, where the nonzero weights include terms the passage never contains. A passage about declined cards gets weight on payment, refused and bank because the model learned the expansion. Scoring is a dot product over an inverted index, so exact-term behavior comes for free. SPLADE-v3 reports 40.2 MRR@10 on MS MARCO dev and 51.7 average nDCG@10 across 13 BEIR datasets on its model card, which is why it is the sparse side people reach for.
I replayed the idea on a synthetic corpus of 150,000 documents (30,522-term vocabulary, 80.6 nonzero terms per document, 28 terms per expanded query, 64-dimension dense vectors, 600 queries split into paraphrase, exact-identifier and mixed). Dense search alone scored nDCG@10 of 0.383 overall and 0.001 on identifier queries, because a dense vector blurs a rare token into its neighbourhood. Sparse alone scored 0.951, but only 0.869 on paraphrases, because the query’s literal terms did not cover the passage. The obvious fusion, z-scoring each list and adding with equal weight, scored 0.675 and made identifier queries worse than sparse alone (0.063). A fusion on one fixed scale scored 0.996.
| Strategy | nDCG@10 all | Paraphrase | Identifier | Mixed | Recall@100 all |
|---|---|---|---|---|---|
| Dense only (HNSW) | 0.383 | 1.000 | 0.001 | 0.065 | 0.528 |
| Sparse only (learned weights) | 0.951 | 0.869 | 0.998 | 0.998 | 0.963 |
| Per-list z-score fusion, alpha 0.5 | 0.675 | 0.999 | 0.063 | 0.968 | 0.680 |
| Fixed-scale fusion, alpha 0.9 | 0.996 | 0.998 | 0.989 | 1.000 | 0.995 |
Method note: synthetic generator in numpy, 1,500 latent concepts that each own 64 related vocabulary terms (the stand-in for SPLADE term expansion), identifier terms drawn from 20,000 rare wordpieces, dense vectors as noisy concept mixtures. Relevance is the set of documents sharing the query’s concept (paraphrase), its identifier (identifier) or both (mixed). hnswlib with M=16, efConstruction=200, ef=128. HuggingFace was unreachable from the sandbox, so no real SPLADE weights were used. The absolute scores are properties of my generator. The shapes are the claim: dense is blind to rare tokens, sparse is literal, and the fusion weight is a unit conversion, not a taste.
We need to solve for term-expansion cost on the inverted index, a single candidate pool scored on both signals, and a fusion scale that stays stable across shards and encoder upgrades, all at 6,000 queries per second.
This post is about the part post 24 left alone. Reciprocal rank fusion throws away score magnitudes and needs no calibration, which is why it works with BM25. Learned sparse scores carry real magnitude, so we can fuse on scores, but only if the scale is calibrated and identical on every shard.
Requirements and Constraints
Functional Requirements
- Accept a text query, optional metadata filters (product, locale), and return the top 10 passages with a fused score and the top contributing terms for debugging.
- Retrieve with both a learned sparse index and a dense HNSW index, then rank every candidate on both signals, not only on the list it came from.
- Make new and updated passages searchable within 5 minutes through a small delta segment, folded into the main segments nightly.
- Support an encoder upgrade (new SPLADE or new dense model) as a versioned, shadow-evaluated segment build with no query downtime.
- Return an
explainpayload with the five heaviest matching terms, because “why did this rank first” is the first support ticket anyone files against a learned sparse system.
Non-Functional Requirements
- Latency: p50 under 18ms and p99 under 40ms for the whole path, including query encoding (assumed 4ms on a batched L4 GPU) and 4ms of network. Stage budget: encode 4ms, shard retrieval 6ms, merge 1ms. No generation on this path, so there is no TTFT, but the downstream LLM that reads the 10 passages has a 400ms TTFT budget this stage must not spend.
- Throughput: 2,500 queries per second average and 6,000 at peak, about 6.6 billion queries a month. Query encoder throughput must cover peak with one GPU lost.
- Quality: nDCG@10 and recall@100 within 0.01 of the previous production index on each query stratum (paraphrase, identifier, mixed), and recall@100 of the fused candidate pool at least 0.99 on the offline golden set.
- Cost: under $2 per million queries, GPU and CPU together.
- Capacity: 7.2 billion postings (120 nonzero terms per passage after pruning, an assumption to measure on your own encoder), 12.5 GB of hot RAM per shard, 5.4 GB of forward index on NVMe per shard.
Constraints
- Sparse model: a SPLADE-v3 class encoder over the 30,522-term BERT vocabulary, served on L4 GPUs. Dense model: a 384-dimension bi-encoder with int8 codes.
- Hardware:
r7g.4xlarge(16 Graviton3 vCPUs, 128 GB RAM, $0.857 per hour) for shards andg6.xlarge(one L4, $0.8048 per hour on demand) for encoders. - Out of scope: cross-encoder re-ranking, access control lists, multilingual routing, and BM25 as a third signal.
The requirement that shapes the design is the encoder upgrade. A fusion weight tuned for one SPLADE version is wrong for the next, so calibration data has to live with the index version, not in a config file.
High-Level Architecture
Six components carry a query. The Query Router caches, parses and fans out. The Sparse Encoder runs the SPLADE query tower and returns about 28 weighted terms. The Dense Encoder returns a 384-dimension unit vector. The Sparse Index on each shard holds impact-ordered postings and answers with MaxScore. The Dense Index on each shard is an HNSW graph over int8 codes. The Fusion Stage scores the union of both candidate lists on both signals, and the router merges four shard lists into the final ten.
A request enters the router, which checks a query-result cache and sends the text to both encoders in one batched GPU call. The two query representations go to all four document shards in parallel. Each shard runs MaxScore over its postings and an HNSW search over its vectors, takes the union of the two top-100 lists, computes both scores for every id in the union, fuses them, and returns its top 10.
The shards own disjoint document ranges, so the merge is a four-way top-10 of already-fused scores. That only works if every shard computes the score on the same scale, which is the single place this design is easy to get subtly wrong and is the subject of the fusion section.
Candidates come from two generators, but ranking comes from one scorer. A document found only by the sparse index still gets a cosine score computed from its stored vector, and vice versa. Fusing the two lists directly would rank a document on the signal that happened to retrieve it.
The Sparse Encoder
The sparse encoder turns text into at most 32 weighted vocabulary terms, on the query side in 4ms and on the document side in an offline batch job.
Think of it as a translator who rewrites your question in the vocabulary your documents were written in. “Card declined overseas” comes back as card, declined, foreign, transaction, bank, refused, each with a weight. SPLADE gets there with a masked language model head: every input token produces logits over the whole 30,522-term vocabulary, log(1 + relu(logit)) squashes them, and a max over token positions keeps the strongest evidence for each term. Expansion is a side effect of the language model head predicting plausible neighbours for each token.
The non-obvious cost is on the query side. A BM25 query for that sentence touches 4 or 5 posting lists. The expanded query touches 28 in my simulation, and every extra term is a posting list to walk. The naive fix is to cut the query to its heaviest 8 or 16 terms. I measured it, and it is a bad trade when the weights are flat:
| Query terms kept | Postings scanned (mean) | nDCG@10 | Recall@100 |
|---|---|---|---|
| 8 | 3,883 | 0.653 | 0.743 |
| 16 | 7,848 | 0.812 | 0.887 |
| 24 | 11,852 | 0.935 | 0.955 |
| All (28 on average) | 14,024 | 0.957 | 0.966 |
Premium Content
Unlock the full article along with everything else in the archive — all in one place.