The Limitations of Exact String Caching

Traditional HTTP caching keys on exact URL or request body strings. In natural language applications, two users asking "How do I reset my password?" and "I forgot my password, how to reset it?" mean the exact same thing but result in separate expensive LLM inference calls.

Semantic Caching Architecture

Semantic caching embeds the incoming query into a vector, queries an in-memory Redis index for previous queries with cosine distance < 0.10, and immediately returns the cached response in under 20ms.

// Fastify / Node.js Semantic Cache Verification
async function queryWithSemanticCache(query) {
  const queryVector = await generateEmbedding(query);
  const cached = await redis.ft.search('idx:llm_cache', '*=>[KNN 1 @vector $V AS dist]', {
    PARAMS: { V: Buffer.from(new Float32Array(queryVector).buffer) },
    RETURN: ['dist', 'response'],
    DIALECT: 2
  });

  if (cached.total > 0 && parseFloat(cached.documents[0].value.dist) < 0.12) {
    return { source: 'cache', response: cached.documents[0].value.response };
  }

  const freshResponse = await callLlmProvider(query);
  await saveToSemanticCache(query, queryVector, freshResponse);
  return { source: 'llm', response: freshResponse };
}

Enterprise Results

In our production customer support chatbot deployments, semantic caching answered 42% of common repeat queries directly from Redis, reducing average response latency from 1,850ms down to 35ms.