Backendcore
Caching, including semantic caching
Exact caches for identical requests, semantic caches for similar ones — keyed per tenant.
What it is and why it exists
Exact-match caches return stored responses for identical inputs (normalised prompt + model + parameters). Semantic caches embed the question and return a stored answer when a previous question is similar enough. Both skip the model call entirely; semantic caching risks returning a wrong answer to a subtly different question.
Quick prototype
functools.lru_cache on the prompt string.
In production, at scale
Cache keys including tenant, user permissions, model and prompt version; similarity thresholds tuned on labelled pairs; TTLs tied to data freshness; never cache personalised or permissioned answers in a shared namespace; hit rate and wrong-hit rate monitored.
Vendors and tools
| Redis (incl. vector search) / GPTCache | Semantic caching |
|---|---|
| LiteLLM / Portkey / Helicone | Gateway-level caching |
On AWS: ElastiCache (Valkey/Redis OSS) or MemoryDB with vector search for semantic caches.
How it fails
- Tenant A’s answer served to tenant B
- “Cancel my order” matched to “Can I order?”
- Stale answers after a policy change
What the interviewer asks next
When is a semantic cache a bad idea?
When answers depend on who asks (permissions, account data), on time, or on small wording differences. Use it for public, stable FAQ-style content.