When StreamHub's catalog API crossed 10K reads per second, Postgres became the bottleneck long before CPU did. A Redis layer in front of stream metadata cut p99 latency from 180 ms to under 5 ms — but only after picking the right cache pattern and invalidation strategy.
Why caching matters at scale
What a cache buys you
- Latency: Memory reads are microseconds; disk-backed DB reads are milliseconds.
- Throughput: Offloading reads frees database connections for writes.
- Cost: Fewer DB replicas needed when 80–90% of reads hit cache.
- Resilience: A warm cache absorbs traffic spikes during viral moments.
Cache-aside with ElastiCache
Core cache patterns
| Pattern | Write path | Best for |
|---|---|---|
| Cache-aside (lazy load) | App reads cache; on miss, loads DB and populates | General read-heavy workloads |
| Read-through | Cache library fetches from DB on miss transparently | When app code should stay cache-agnostic |
| Write-through | Every write updates cache and DB synchronously | Strong consistency on reads after writes |
| Write-back | Write to cache; async flush to DB later | Very high write throughput; brief loss risk |
| Write-around | Write directly to DB, skip cache | Write-heavy, rarely read data (audit logs) |
Cache-aside (lazy load)
Write pathApp reads cache; on miss, loads DB and populatesBest forGeneral read-heavy workloadsRead-through
Write pathCache library fetches from DB on miss transparentlyBest forWhen app code should stay cache-agnosticWrite-through
Write pathEvery write updates cache and DB synchronouslyBest forStrong consistency on reads after writesWrite-back
Write pathWrite to cache; async flush to DB laterBest forVery high write throughput; brief loss riskWrite-around
Write pathWrite directly to DB, skip cacheBest forWrite-heavy, rarely read data (audit logs)
Pick the pattern that matches your read/write ratio and consistency needs.
StreamHub uses cache-aside for stream metadata: the API checks Redis first, falls back to Postgres on miss, and sets a TTL of 300 seconds.
def get_stream(stream_id: str) -> StreamMeta:
cached = redis.get(f"stream:{stream_id}")
if cached:
return StreamMeta.parse(cached)
row = db.query("SELECT * FROM streams WHERE id = %s", stream_id)
redis.setex(f"stream:{stream_id}", 300, row.to_json())
return row
The rest of this guide is part of Pro
Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.
Still to come · 4 sections
- Cache layer topology
- Eviction and TTL policies
- Cache invalidation strategies
- StreamHub cache-aside in production
Test yourself
Answer these before moving on — recall is what makes it stick.
- Explain caching strategies — cache-aside, read/write-through, write-behind, TTL, invalidation.
- Given an access pattern, walk through choosing between SQL / KV / Document / Wide-column / Search / Graph / Vector.
- Walk through the scalability journey — vertical, horizontal, sharding, replication, async, CDN.