NoSQL & Cache
Every system in this course leans on one truth: the fastest query is the one you never run. Redis makes state fast; document databases make state flexible. The last two tools in the belt.
▶ Watch this reelWhat you'll learn
- Redis: memory-speed state
- Redis as infrastructure
- Document databases
- Choosing your store
Remember this
- Redis is a remote data-structure server: strings for cache (always with TTL), sets for dedupe, sorted sets for rate limits — sub-millisecond, volatile by design
- Cache-aside (check → compute → store) plus pipelines, locks with token-checked release, and sliding-window limiters cover Redis's 80%
- Document databases (MongoDB / Cosmos DB) keep JSON whole — flexible schema, aggregation pipelines share Polars' vocabulary, indexes still mandatory
- Storage policy in one breath: Postgres is the record, Redis the accelerator, documents the escape hatch — store sprawl is the enemy
Redis
- Data-structure server in RAM: strings, hashes, sets, sorted sets.
- Cache-aside: get → compute → set EX. TTLs always.
- SADD = idempotency/dedupe (PY-14, AG-19). ZADD pipeline = sliding-window rate limit (AG-25, AG-23).
- SET NX EX = distributed lock; release with token check.
- Pub/sub = WebSocket fanout backbone (PY-14); fire-and-forget — signals, not state.
- Never the system of record.
Document DBs (MongoDB / Cosmos DB)
- JSON whole; flexible schema; upsert without migrations.
- Aggregation pipelines = match/group/sort — Polars vocabulary (PY-16) in JSON.
- Index what you filter on — flexible schema ≠ no indexes.
- Cosmos: MongoDB API + turnkey multi-region on the GA-29 stack.
Storage policy
Postgres = record (start here) · Redis = accelerator · Documents = escape hatch. Store sprawl is the enemy; exotic options earn their slot via benchmark (GA-20 discipline).
Course close
80 reels: PY (01-25), GA (01-30), AG (01-25). Core idea: probabilistic models → trust through evals, guardrails, budgets, gates. Keep the reels as a cross-linked reference; practice by rebuilding without looking.
Cross-links
GA-10 (Postgres/pgvector), GA-29 (Cosmos on Azure), AG-09 (guardrails), AG-14 (agent loop), AG-17 (memory), AG-19 (queues/dedupe), AG-21 (eval logs), AG-23 (rate limits/budgets), AG-25 (platform), PY-14 (pub/sub fanout, webhook dedupe), PY-16 (aggregation kinship).
Code: The last snippet of the course
# Reel 80 of 80 — the pattern that ties the course together:
# every fast system in these reels has the same skeleton.
async def serve(request):
await guardrails.check(request) # AG-09
if not await rate_limit.allow(request.tenant): # this reel
return slow_down()
cached = await redis.get(key(request)) # this reel
if cached:
return cached
async with lock("rebuild"): # this reel
result = await agent.answer( # AG-14
request.text,
memory=await mongo.load(request.user), # AG-17
eval_log=True, # AG-21
)
await redis.set(key(request), result, ex=300)
return result
# COURSE COMPLETE. Now go build the one only you can build.