Charlie Roffe

Principal Architect & Engineering Director · Cloud & AI

Madrid, Spain · LinkedIn · GitHub

ADR-007: Retrieval Store & Two-Tier Caching (In-Memory + Firestore)

Context

The RAG Q&A showcase uploads custom documents, slices them into chunks, embeds them, and grounds Gemini with the most relevant segments — showing similarity scores transparently. The same decision must settle the platform persistence question: ADR-013 routes generation through the Vertex AI `eu` multi-region endpoint and embeddings through europe-southwest1 (Madrid). Quotas are granted per location and per model, and embedding calls face a tight requests-per-minute ceiling. Generation has no quota override; its spend is bounded by the app (30 requests / 15 min per IP and 300 model calls per day site-wide). Public traffic must keep being accepted within those regional limits, on stateless scale-to-zero compute, with zero standby budget.

Decision

Three coupled decisions:
1. A transient, in-request retrieval engine: chunking on blank lines (paragraphs of 20 characters or fewer dropped, no overlap), batched Vertex AI embeddings (gemini-embedding-001 at 768 dimensions since 2026-10-06; originally text-embedding-004) and in-memory cosine ranking, with the top 3 passages placed in the prompt — no external vector database.
2. Firestore (Native mode, europe-southwest1) as the platform single zero-standby persistence layer: billed per operation, EU-resident in the same region as compute and embeddings, accessed via keyless IAM.
3. A generic two-tier cache for every expensive artifact: a per-instance in-memory LRU (L1) with in-flight request coalescing, backed by Firestore (L2) shared across instances and surviving scale-to-zero. Keys are SHA-256 content hashes including model and prompt version, so stale results are structurally impossible. L2 documents expire after 30 days through a Firestore TTL policy.

Consequences

Benefits:
* Quota absorption: repeat traffic is served from cache without touching Vertex AI, so the site keeps accepting visitors within the per-location quota ceilings; a cache hit turns the ~20s embedding step into near zero.
* Zero standby infrastructure: Firestore rounds to $0.00 at portfolio traffic — no Redis, no vector database.
* EU residency preserved: the cache lives in the same Madrid region as compute (ADR-013).
* Stateless compute, stateful performance: every replica shares the warm cache across cold starts.

Trade-offs:
* Derived artifacts (content-hashed chunk vectors) are persisted — raw uploads never are.
* Best-effort L2: a Firestore outage degrades to fresh, slower, quota-consuming computation.

Alternatives considered

1. No cache (re-embed every request): Rejected — repeat traffic would burn the embedding quota, trigger slow 429 backoffs, and pay ~20 seconds per question.
2. Persistent vector database (Pinecone / Vertex Vector Search): Rejected — high fixed monthly costs for session-scoped documents.
3. Memorystore / Redis: Rejected — a ~$35/month standby charge violates the zero-budget baseline.
4. Keyword-only tf-idf ranking: Rejected — quota-free but markedly worse retrieval quality than semantic embeddings.

Amended (ADR-025): the generation side of the Q&A pipeline evolved into Corrective RAG — a retrieval-quality grader routes insufficient retrievals to Google Search grounding. The retrieval engine, embedding batching, and two-tier cache decided here are unchanged.

Architecture Decision Records