MeanCache: User-Centric Semantic Caching for LLM Web Services

Waris Gill, Mohamed Elidrisi, Pallavi Kalapatapu, Ammar Ahmed, Ali Anwar, Muhammad Ali Gulzar

TL;DR

This paper introduces MeanCache, a novel user-centric semantic caching system designed to reduce the computational cost and latency of Large Language Model (LLM) web services. By leveraging federated learning for privacy-preserving training of a compact embedding model and incorporating contextual query handling, MeanCache significantly improves cache hit rates and accuracy compared to existing methods.

What Problem Does This Paper Address?

Large Language Models (LLMs) like ChatGPT and Llama have revolutionized natural language processing, but their immense computational cost presents a significant challenge. Inference for these models requires billions of floating-point operations, leading to high operational costs, service provider load, and environmental impact. Caching is a natural solution to mitigate these costs by serving repeated queries from a cache instead of re-querying the LLM. However, traditional caching methods, which rely on keyword matching, fail to capture semantic similarities between queries. Existing semantic caching solutions often suffer from privacy concerns due to centralized data storage, scalability issues with large central caches, and an inability to effectively handle contextual queries, where a query's meaning depends on previous interactions.

Methods

MeanCache tackles these problems with a multi-pronged approach:

  • User-Centric, Local Caching: Each user has a local cache, ensuring privacy as no query data leaves the user's device.
  • Compact Embedding Models: Instead of resource-intensive LLMs, MeanCache uses smaller, more efficient embedding models (like MPNet or Albert) to generate query embeddings locally. These models are trained collaboratively.
  • Federated Learning (FL) for Privacy-Preserving Training: MeanCache employs FL to train a shared embedding model across multiple users without centralizing their data. This allows the model to learn from diverse query patterns and improve semantic matching accuracy while upholding user privacy.
  • Contextual Query Handling: MeanCache explicitly encodes and verifies the "context chain" of queries. This allows it to differentiate between semantically similar queries that require different responses due to their conversational history.
  • Embedding Compression with PCA: To further reduce storage and accelerate search, MeanCache uses Principal Component Analysis (PCA) to compress the embedding vectors, significantly shrinking their dimensionality.
  • Adaptive Cosine Similarity Threshold: An adaptive threshold for cosine similarity is collaboratively computed using FL, optimizing the balance between true hits and false misses.

Datasets

The primary dataset used for evaluation is the GPTCache dataset, which is partitioned into training, testing, and validation subsets. For evaluating contextual queries, the authors generated a synthetic dataset using GPT-4, comprising 450 queries including duplicates, non-duplicates, and contextual queries.

Key Insights and Ideas

  • Privacy and Efficiency are Not Mutually Exclusive: MeanCache demonstrates that it's possible to achieve high performance in semantic caching without compromising user privacy, by leveraging federated learning and local caching.
  • Smaller Models Can Be Powerful: Contrary to the trend of using massive LLMs for embeddings, MeanCache shows that well-trained, compact embedding models can achieve comparable or even superior performance for semantic matching tasks.
  • Context is King for LLM Queries: Simply matching semantic similarity is insufficient for LLM queries. Understanding the conversational context is crucial for accurate response retrieval, and MeanCache's context chain mechanism addresses this effectively.
  • Federated Learning is Key to Personalization and Privacy: FL enables the collaborative training of a robust embedding model that benefits from the collective intelligence of users while keeping their data local and private.
  • Compression Doesn't Always Hurt: While embedding compression can slightly reduce F-score, the significant gains in storage and speed make it a worthwhile trade-off, especially for user-side deployment.

Key Results

  • Improved Performance Metrics: MeanCache achieves a 17% higher F-score and a 20% increase in precision compared to the baseline GPTCache on standalone queries.
  • Superior Contextual Query Handling: For contextual queries, MeanCache shows even more impressive gains, with a 25% higher F-score and accuracy, and a 32% higher precision over the baseline.
  • Reduced Storage and Faster Search: Embedding compression using PCA reduces storage requirements by 83% and accelerates semantic cache hit-and-miss decisions by 11%.
  • Lower False Hits: MeanCache significantly reduces false hits, which are a major source of user frustration and necessitate re-querying the LLM. For contextual queries, MeanCache reports only three false hits compared to 54 for GPTCache.
  • FL Enhances Model Quality: FL training progressively improves the performance metrics (F-score, precision, recall, accuracy) of the embedding model over training rounds.

Limitations and Future Work

  • Llama 2 Infeasibility: The paper concludes that using large models like Llama 2 for embedding generation is infeasible for user-side deployment due to their substantial computational demands, inference time, and storage requirements. While future enhancements might improve Llama 2's performance, these core issues are likely to persist.
  • Dataset Size for FL: The number of clients for FL training was restricted due to the limited size of the GPTCache dataset, which might influence the generalizability of results in scenarios with extremely large numbers of clients.
  • Future Work: The authors suggest exploring further optimizations for embedding compression and investigating more advanced FL aggregation algorithms. They also highlight the potential for MeanCache to reduce LLM query inference costs by up to one-third for semantically similar queries.

Summary updated

BibTeX
@inproceedings{gill2025meancache,
  author = {Waris Gill and Mohamed Elidrisi and Pallavi Kalapatapu and Ammar Ahmed and Ali Anwar and Muhammad Ali Gulzar},
  title = {{MeanCache: User-Centric Semantic Caching for LLM Web Services}},
  booktitle = {IPDPS},
  year = {2025},
  doi = {10.1109/ipdps64566.2025.00117},
}
Figure