Verified Public Note•Nov 1, 2026•2 views

Caching Strategies for RAG Systems

Share this NoteDrive viral discussions across engineering channels

Caching Strategies for RAG Systems

Cache Types and Their Contents

  • Query Cache: Query embedding
  • Retrieval Cache: Retrieved document chunks
  • Semantic Cache: Answers to similar queries
  • LLM Response Cache: Generated answer for an exact input/context
  • Document Cache: Parsed document content

Important Note

A cached answer and a cached retrieval result are not the same thing. Even if the answer cache is invalidated, stale chunks in the retrieval cache can still produce an incorrect answer.

Tying Cache Keys to Document Versions

Problem Understanding

Consider a bank with the document:

  • home_loan_policy.pdf

Version 1 states:

  • Home loan interest rate: 8.5%

A user asks:

  • "What is the current home loan interest rate?"

Your RAG system generates:

  • The current home loan interest rate is 8.5%.

You cache the response in Redis:

PLAINTEXT
Key: home_loan_interest_rate
Value: The current home loan interest rate is 8.5%

Tomorrow, the bank updates its document:

  • Home loan interest rate: 9%

However, Redis still contains the previous answer. Your RAG system returns 8.5%. This is called a stale cache problem.

Solution

Instead of using only the query as the cache key, include the document version. The new version gets a different cache key.

Active Document Version

To know which document version is currently active, you need a source of truth, such as PostgreSQL:

  1. Before looking up the answer, your application resolves the current document version.
  2. Then it constructs the cache key:
    JAVA
    String cacheKey = "rag:answer:loan:version:" + currentVersion;
    

This way, an old cache entry cannot accidentally be used for a new document version.

Multiple Document Dependencies

If you have multiple documents contributing to an answer, your cache key must account for all their versions, not just one.

Dependency Management in Caching

Imagine you have 10,000 cached answers, each depending on multiple documents. For example:

Cached AnswerSource Documents
Home loan eligibilityLoan policy, income policy
Home loan interest rateInterest rate policy
Personal loan eligibilityPersonal loan policy, KYC policy
KYC requirementsKYC policy

If the bank updates only its KYC policy, should you delete all 10,000 cached answers? Obviously not. You should invalidate only the answers that depend on the KYC policy.

Solution: Build a Dependency Graph

Think of it like dependency tracking in a build system:

  • Source code changes.
  • The build system identifies which modules depend on that source.
  • Only affected modules are rebuilt.

Time To Live (TTL) in Redis

TTL stands for Time To Live. Redis allows you to set an expiration time on a key.

Example

PLAINTEXT
SET rag:answer:loan "Interest rate is 8.5%" EX 300

EX 300 means the key expires after 300 seconds (5 minutes).

Freshness Check

Your cache key might still look valid. To prevent returning the wrong answer, perform a freshness check.

Freshness Check Steps

  1. Read the cached answer.
  2. Check which document versions it depends on.
  3. Check the current document versions in the source of truth.
  4. Compare the versions.
  5. If they match, return the cached answer.
  6. If they don't match, invalidate the entry and run retrieval again.
Share this NoteDrive viral discussions across engineering channels
Accelerate Your Tech Interview Preparation

Prepare for Senior Engineering Rounds with PrepTrack

Track 518+ curated DSA problems, Low-Level Design scenarios, and High-Level Distributed Architecture blueprints. Build your own central notes vault with automatic GitHub synchronization.