Skip to main content

Design a two-tier exact + semantic cache with cost guardrails

Cut LLM API spend by caching identical prompts in Redis and near-duplicate queries via embeddings.

Official Reference

Model Answer & Explanation

Tier 1: hash normalized (model, system_prompt_version, user_query) in Redis for O(1) exact hits. Tier 2: embed standalone queries and check pgvector/Redis cosine similarity >= 0.96 (skipping multi-turn stateful chats or user-private queries unless tenant-scoped!). Track per-user token budgets in Redis before dispatching calls.

Follow-up Interview Question · When is semantic caching unsafe or likely to return the wrong answer?

Module Context & References

Applied AI · Practice engineering drills Hands-on RAG, structured extraction, eval harness and agent reliability drills
Personal Notes · Markdown

Technologies & Interview Topics Covered

Switch between short-term interview tracks and long-term 80/20 engineering depth to practice JavaScript internals, React rendering and hooks, timed frontend machine coding UI challenges, MeetaPro FG300, Blind 75 & NeetCode 150 DSA patterns, Python & FastAPI APIs, PostgreSQL query optimization & MVCC, Redis caching, Applied AI RAG pipelines & evaluations, and distributed system design.