AWS Builder Center

Your RAG App Is Slow: Should You Cache the Answer or the Retrieval?

Caching can make a RAG application faster and cheaper, but caching the wrong thing can return outdated information or even expose one user's data to another. Let's explore three caching strategies for RAG systems, when each makes sense, and how to choose the right one.

The Problem: Your RAG Pipeline Does Too Much Work

Imagine a user asks your document chatbot:
"What is the refund policy?"
Your RAG pipeline might perform these steps:
  1. Convert the question into an embedding.
  2. Search the vector database for relevant chunks.
  3. Retrieve and rerank the results.
  4. Send the context to an LLM.
  5. Generate the final answer.
Now imagine 100 users ask almost the same question.
Do you really need to repeat every expensive operation each time?
Not necessarily. But where you cache matters more than simply adding a cache.

1. Response Caching: Cache the Final Answer

Store the generated answer against a normalized question or a suitable cache key.
If the same question comes up again, return the cached response instead of calling the retrieval pipeline and LLM.
Best for:
  • Frequently asked, relatively stable questions.
  • Public documentation and FAQs.
  • Repeated queries with identical context and permissions.
The catch: What if the refund policy changes tomorrow? Your cache could keep returning yesterday's answer.
You need a TTL (time to live), invalidation strategy, or a version-aware cache key. For personalized or permission-restricted content, the key must also account for the relevant user or authorization context.

2. Retrieval Caching: Cache the Retrieved Context

Instead of caching the final answer, cache the document chunks retrieved for a query.
When a similar question arrives, you may be able to reuse the retrieved context while still generating a fresh answer.
Best for:
  • Repeated queries about the same documents.
  • Applications where answer wording should adapt to the conversation.
  • Reducing repeated retrieval or reranking work.
The catch: The underlying documents might have changed, or a similar query might need different context. You need an appropriate cache key and an invalidation strategy when the source data or index changes.
Also, retrieval caching does not automatically eliminate embedding generation or vector search unless you cache those results too.

3. Semantic Caching: Cache Similar Questions

This is where things get interesting.
A normal cache might treat these as different keys:
  • "How do I reset my password?"
  • "I forgot my password. How can I change it?"
A semantic cache compares the meaning of queries, often using embeddings, and can reuse a cached result when a new query is sufficiently similar.
Best for:
  • Support chatbots.
  • FAQ-heavy applications.
  • Repeated questions expressed in different words.
The catch: Similar wording does not always mean the same intent. "How do I cancel an order?" and "Can I cancel an order after it ships?" are related, but their answers may differ.
A similarity threshold that is too low can return incorrect answers. A threshold that is too high may provide few cache hits.
For dynamic or sensitive queries, bypassing the semantic cache may be safer.

So, Which One Should You Use?

StrategyWhat it avoidsMain risk
Response cacheRetrieval + LLM generationStale or contextually incorrect answers
Retrieval cacheRepeated retrieval workOutdated or irrelevant document chunks
Semantic cacheRepeated processing of similar queriesFalse matches between different intents
These approaches can also be combined. For example, you could cache retrieved chunks for a short period and cache answers only for stable, frequently asked questions.

Where Does AWS Fit In?

For an AWS-based architecture, Amazon ElastiCache can serve as a low-latency cache for application data and suitable retrieval or response results. The application still needs to decide what belongs in the cache, how keys are constructed, when entries expire, and how stale data is invalidated.
The important distinction is that a cache is not automatically RAG-aware. You must design the caching logic around your documents, authorization rules, and freshness requirements.

The Takeaway

Caching is not simply about making an LLM application faster. It is about deciding which computation is safe to skip.
Before adding a cache to your RAG application, ask three questions:
  1. Is this result expensive to compute?
  2. Can I safely reuse it for this particular request?
  3. What happens when the underlying data changes?
A faster answer is useful. A faster wrong answer is not.
What would you cache first in a production RAG application: the retrieved context or the final answer?
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article