Text size
Dark mode
RecallRunGuides & benchmarks
Learn to build real systemsGuides•Benchmarks•Experiments•Community
Search the site/Free checklist

Blog/rag

Caching RAG answers: a 0.95 semantic cache hit 86% but served 16% wrong answers

On 20,000 simulated queries, exact-match caching hit 74.9% with zero wrong answers. A 0.95 semantic threshold hit 85.8% but 15.96% of queries got the wrong cached answer.

In this post

What this post covers
  1. The setup
  2. Results: hit rate vs wrong hits
  3. What the numbers mean
  4. Lookup cost
  5. Staleness
  6. Caveats
  7. Rule of thumb
How this was testedhardware, versions, data

2 vCPU GitHub runner, Python 3.12.16, numpy 2.5.5, fastembed 0.9.1 with BAAI/bge-small-en-v1.5 (384 dim, CPU). 1,132 intents taken from FastAPI docs headings; 20,000 simulated queries (Zipf popularity, 9 phrasing templates, 10% upper-cased). Cache is a numpy brute-force scan in memory. Not tested: Redis itself, real user query logs, other embedding models, LLM-generated paraphrases, answer-quality judging.

Should a RAG app cache answers, and if so, by exact string or by embedding similarity? A semantic cache promises more hits. The question is what it costs in wrong answers.

Short answer: in this simulation, exact-match caching (after lowercasing and trimming) already hit 74.9% of queries with no wrong answers. A semantic layer added hits, but every point of threshold you relax buys more wrong answers. Even at 0.98 similarity, wrong hits were as large as the gain.

The setup

I had no real query log, so I simulated one. Each “intent” is a heading from the FastAPI docs (bash experiments/fetch_corpus.sh downloads the corpus). A query is an intent wrapped in one of 9 templates (“how do I … in FastAPI”, “… example”, “docs on …”). Intents are drawn with Zipf popularity, so a few are asked often. 10% of queries are upper-cased.

A hit is wrong when the cached entry the query matched belongs to a different intent. That is a strict rule: two headings can sometimes have the same answer. Treat the wrong-hit numbers as an upper bound on that effect, not a measured user-visible error rate.

The cache checks the normalised string first, then, if enabled, the cosine similarity of the query embedding against every cached query (bge-small-en-v1.5 via fastembed). Misses are stored. For background on how embeddings are used in retrieval, see the retrieval guide.

Results: hit rate vs wrong hits

Policy Hit rate Wrong hits (count) Wrong, % of hits Wrong, % of all queries
exact only 74.9% 0 0.0% 0.00%
semantic ≥ 0.98 78.1% 647 4.1% 3.23%
semantic ≥ 0.95 85.8% 3,192 18.6% 15.96%
semantic ≥ 0.92 90.6% 5,323 29.4% 26.61%
semantic ≥ 0.90 92.7% 6,161 33.2% 30.80%
semantic ≥ 0.85 95.9% 7,577 39.5% 37.88%
semantic ≥ 0.80 97.5% 10,224 52.4% 51.12%

Compared with exact-only, 0.98 added 3.2 points of hit rate and 3.23% of queries got a wrong answer. At 0.95 the gain was 10.9 points and wrong answers hit 15.96% of queries. The wrong answers outgrow the extra hits at every threshold I tried.

What the numbers mean

The surprise is how high the safe threshold is. bge-small scores most pairs of technical, same-domain text high, so 0.90 is not “very similar” here. Headings from one documentation set look alike to the model (“Query Parameters” and “Path Parameters” share most of their meaning). The threshold that looks conservative on a 0-to-1 scale was not. The likely reason is the narrow domain: a general chat cache sees more dissimilar queries. Other published work also finds that correct and incorrect semantic hits have heavily overlapping similarity scores and that thresholds vary by embedding model (vCache paper, reported by the authors; I did not reproduce it). So tune the threshold per model, on your own labelled pairs.

Even 0.98 returned wrong hits. Likely cause: several docs headings are near-duplicates once wrapped in a template. I did not break this down further.

Lookup cost

Semantic lookup is an embedding call plus a similarity search. Measured on this runner:

Step Time
Embed one query (bge-small, CPU, batch average) 13.81 ms
Brute-force scan, 1,000 cached queries 0.021 ms
Brute-force scan, 10,000 0.192 ms
Brute-force scan, 100,000 13.283 ms

Below roughly 10,000 entries the scan is free next to the embedding. An exact-match lookup needs neither, which is a Redis GET on a hash of the normalised query. The embedding time is a batch average on a small CPU model; a hosted embedding API will differ.

Staleness

I changed 5% of intents at query 10,000 (their source doc changed) and counted cached answers older than the change that were still served.

Policy Hit rate Stale answers served % of post-update queries
exact, no TTL 74.9% 470 4.70%
exact, TTL 2,000 queries 50.9% 21 0.21%
exact, TTL 500 queries 34.1% 5 0.05%
semantic 0.92, no TTL 90.6% 565 5.65%
semantic 0.92, TTL 2,000 queries 58.2% 22 0.22%
semantic 0.92, TTL 500 queries 36.7% 5 0.05%

TTL cut stale answers a lot, and it also cut the hit rate: exact-match fell from 74.9% to 50.9% at a 2,000-query TTL. Because the TTL here is counted in queries, not seconds, translate it into your own traffic rate. A better fix is invalidating on re-index: key entries by a corpus version and bump it when documents change. I did not simulate that; it removes stale answers entirely at the cost of a cold cache after each re-index.

Caveats

  • Paraphrases are templates, not human wording. Real users vary more, which probably helps semantic caches find true matches and also creates more near-misses. Direction unknown.
  • “Wrong” means a different heading, not a different answer.
  • Zipf popularity with 1,132 intents is an assumption. A flatter distribution lowers every hit rate.
  • No Redis server was involved; the cache is in-process numpy.
  • The cache scope matters: for multi-tenant apps, include tenant and permission scope in the key, or a hit can leak another user’s answer.

Rule of thumb

  1. Start with an exact cache on a normalised query, keyed by corpus version, tenant and prompt version.
  2. Add a semantic layer only if you have labelled pairs of “same question” and “different question” from your own logs, and pick the threshold from them, not from a default.
  3. If you cache semantically, start at the top of the range (0.98 or higher for a small embedding model like this one) and watch wrong hits.
  4. Skip caching answers to queries that depend on fresh data, and set a TTL for the rest.

Caching also changes your cost model; see the per-query cost calculator to estimate what a given hit rate saves.

Learn it step by step · free

Go deeper in the RecallRun guides

All guides →

Experiment files

The exact script and raw output behind every number in this post. Download and rerun them.

Get the free RAG production checklist

25 checks to run before real users see your RAG feature: exact-match misses, invented citations, prompt injection through documents, quality that slips after a prompt change. Confirm your email and the PDF comes with the welcome email, plus each new experiment.

One email to confirm. Unsubscribe any time. What's in it · Privacy

Want the production version of this? See ShipRAG: A production RAG + MCP starter kit for FastAPI: hybrid search, validated citations, evals in CI, Docker, 32 tests.

Written and measured by

Alby Tomy

Python/FastAPI Engineer | Data Engineer | AI Engineer building RAG and MCP systems.

X · GitHub · Contact & enquiries