Choosing a Caching Strategy
Matching cache type to traffic shape
Exact, semantic, prefix and sub-call caches serve different traffic shapes. Strategy is a portfolio: cache the repeatable core exactly, near-duplicates semantically, and long prefix chrome structurally. Choose by how your requests cluster.
What you will learn
- Classify traffic into cacheable families
- Map family to cache technique
- Build a layered cache portfolio
What is this?
A caching strategy is a policy for which requests to serve from where: exact-match keys for byte-identical repeats, semantic gates for paraphrases, prefix caching for shared-foreground prompts, and sub-call caches inside pipelines (RAG fetches, tool results). Real apps layer several simultaneously.
Why do you need it?
- Single-technique caches either miss too much (exact-only) or risk correctness (semantic-only).
- The traffic mix decides: FAQ shapes love semantic; batch jobs love exact/full; agents love prefix.
- A layered portfolio converts the most expensive requests with the least risk.
Mental model
Strategy
A hospital's triage
Not every patient needs the ICU: triage routes to ER, ward or at-home care — matching response to need and cost.
How it works
- Sample requests, cluster them by shape (identical, near-dup, shared prefix, unique).
- Exact hit first (free, safe), semantic second (families), prefix third (structural reuse).
- Decide eviction/TTL per family; unique traffic is written off.
- Re-measure hit rate and staleness quarterly as the mix drifts.
Multi-tier gate order.
In production
- A customer-support app: exact top-100 + semantic family cache + prompt prefix caching.
- A code assistant caches exact and near-dup queries but never prefixes with live code context.
- A document QA caches RAG sub-calls (chunk fetches) plus final answer exact keys.
Best practices
- Measure the traffic mix before designing; cache for the share that repeats.
- Layer gates cheapest-first and most-correct-first.
- Set explicit TTL/per-family and review per launch.
- Keep the model path instrumented so strategy changes are provable.
Common mistakes
- Building the fanciest cache for a traffic shape that's mostly unique.
- Semantic cache before exact; you pay embeddings for byte-identical hits.
- No re-evaluation: months later the mix changed but the strategy didn't.
Debugging tips
- Semantic catches near-dups but exact rate should still lead; if not, exact key is too noisy.
- Prefix cache hits with high TTFT → engine-level, check KV pool size.
- Model spend flat despite high hit rate → hits are the cheap calls, misses the expensive ones.
Interview checkpoint
Quick quiz
The cheapest cache layer to check first is usually?
One carefully chosen cache technique fits all traffic.