Skip to content

Lesson 5 of 9 · 25m

Read Paths: Caching, Fan-Out, and the Cost of Freshness

Design low-latency read paths with cache placement, invalidation rules, fan-out control, and degradation behavior that match user expectations.

Caching is easy to say and brutal to operate. Every experienced engineer has shipped the "just add Redis" design and then spent a week chasing stale permissions, stampedes, and memory eviction behavior. A read path is a negotiation among latency, freshness, cost, and correctness. Make the negotiation explicit.

Draw the read path before choosing cache

A typical path has several layers:

client cache -> CDN -> edge cache -> load balancer -> service cache
-> shared cache -> database replica -> primary/storage

Each layer has different keys, TTLs, invalidation authority, and failure behavior. A CDN is excellent for immutable public assets and poor for private per-user data unless carefully keyed. A database replica helps read throughput but may serve stale data after writes.

Cache keys are authorization logic

If a key omits tenant, role, locale, or experiment state, the cache can leak or corrupt behavior:

bad:   profile:42
better: tenant:881:profile:42:viewer:17:role:member

The correct key depends on what varies in the response. Do not cache an owner view and serve it to a member because both are "the same document." Authorization-sensitive caching needs the same rigor as access control itself.

Choose an invalidation strategy honestly

There are three common patterns:

TTL only           simple, bounded staleness, weak correctness
write-through      write updates store and cache together, more coupling
invalidation event write publishes change, consumers evict/update, eventual

TTL is often the right first answer when staleness is acceptable. For moderation or permission changes, use explicit invalidation plus short TTL as a backstop. "Invalidate on write" sounds trivial until writes span several services and cache keys are shaped by viewer context.

Control fan-out and stampedes

Fan-out happens when one user request becomes many downstream calls. A home feed may fetch profile, posts, media metadata, reactions, and recommendations. Multiply that by concurrency and your service becomes a denial-of-service generator.

Use request collapsing for hot keys:

if cache miss:
    acquire per-key lock or use singleflight
    first requester loads database
    concurrent requesters wait or serve stale copy

Stale-while-revalidate is often better than a hard miss for expensive but tolerably stale data.

Plan degradation before the incident

A cache outage should not mean database death. Protect the fallback path with timeouts, concurrency limits, load shedding, and reduced feature modes. If recommendations disappear but the core feed still loads, users experience a degraded product instead of a total outage.

Measure hit ratio, but do not worship it

A 98% hit ratio can still be bad if the missing 2% are hot keys or expensive authorization checks. Track hit ratio by class, miss cost, eviction rate, staleness, invalidation lag, and downstream request amplification.

Field notes

  • Cache consistency problems often appear as security incidents when permission changes propagate slowly.
  • Never let cache misses retry instantly in a tight loop; retries amplify the exact overload you are trying to survive.
  • Serialization format and versioned keys matter during deploys. Old workers may misread new payloads.
  • In interviews, saying what may be stale — and for how long — is stronger than claiming a cache makes everything fast.

Was this lesson helpful?

Sign in to rate