Skip to main content
PreparationZone Logo
PrepZone

Caching Strategies

Cache-aside, write-through and TTL policies that turn slow reads into millisecond responses.

Pass checkpoint below

Read these first

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

1. GET2. miss3. SET+TTLCOMPUTE
EKS API podstreamhub-api
DATABASE
RDS Postgressource of truth
DATABASE
ElastiCachesub-ms reads
App checks Redis first; on miss loads RDS and populates cache with TTL.

Core cache patterns

PatternWrite pathBest for
Cache-aside (lazy load)App reads cache; on miss, loads DB and populatesGeneral read-heavy workloads
Read-throughCache library fetches from DB on miss transparentlyWhen app code should stay cache-agnostic
Write-throughEvery write updates cache and DB synchronouslyStrong consistency on reads after writes
Write-backWrite to cache; async flush to DB laterVery high write throughput; brief loss risk
Write-aroundWrite directly to DB, skip cacheWrite-heavy, rarely read data (audit logs)
  • Cache-aside (lazy load)

    Write pathApp reads cache; on miss, loads DB and populates
    Best forGeneral read-heavy workloads
  • Read-through

    Write pathCache library fetches from DB on miss transparently
    Best forWhen app code should stay cache-agnostic
  • Write-through

    Write pathEvery write updates cache and DB synchronously
    Best forStrong consistency on reads after writes
  • Write-back

    Write pathWrite to cache; async flush to DB later
    Best forVery high write throughput; brief loss risk
  • Write-around

    Write pathWrite directly to DB, skip cache
    Best 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.

Java
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.