teluzia.Cloud Engineer · Day 142
CHAPTER 21 · CLOUD SYSTEM DESIGN

Caching and CDNs

4–5 hours including independent practice3 topics
LET’S MAKE THIS SIMPLE

What you’re learning today

Cache behavior is today's starting point. Cache product p42 for 60 seconds and invalidate after a price update. A stale description may be tolerable; an authorization decision needs stricter freshness and should not share the same cache policy. You will connect this with cdn distribution and consistency and stampedes. The lesson's artifact brings these decisions together: Write a cache policy for static images, product descriptions and private account pages; simulate simultaneous expiry and choose a mitigation.

Think of it this way

A cache is a frequently used reference copy; a CDN distributes copies nearby, so freshness and whose copy may be shared matter.

TOPIC 01 / 03Step by step

Cache behavior

A cache stores reusable results for faster retrieval. Specify key, expiry and invalidation; stale data may be acceptable for a catalog but dangerous for authorization decisions. Cache product p42 for 60 seconds and invalidate after a price update. A stale description may be tolerable; an authorization decision needs stricter freshness and should not share the same cache policy.

WORKED EXAMPLE
Cache product p42 for 60 seconds and invalidate after a price update. A stale description may be tolerable; an authorization decision needs stricter freshness and should not share the same cache policy.

A cache key identifies a reusable result; time-to-live limits freshness; invalidation removes a result before normal expiry. Imagine a search result depends on query, locale and permissions, but the key includes only the query. Users can receive wrong-language or unauthorized results even while the hit ratio looks excellent. Enumerate every input that changes the response, then decide which belongs in the key and which makes caching unsafe. Write behavior for misses and origin failures, not only hits. Updates must either invalidate affected entries or accept a defined stale window. Caching reduces repeated work but adds another data lifecycle. Choose freshness from the user contract and sensitivity of the decision, then monitor correctness as well as latency improvement. Test an identity-changing request explicitly.

Why this matters

A stale description may be tolerable; an authorization decision needs stricter freshness and should not share the same cache policy.

A common trap

Authorization responses can safely share the same public cache key.

Read all topics in this lesson

A complete reference to revisit alongside the guided steps above.

Cache behavior

A cache stores reusable results for faster retrieval. Specify key, expiry and invalidation; stale data may be acceptable for a catalog but dangerous for authorization decisions. Cache product p42 for 60 seconds and invalidate after a price update. A stale description may be tolerable; an authorization decision needs stricter freshness and should not share the same cache policy.

WORKED EXAMPLE
Cache product p42 for 60 seconds and invalidate after a price update. A stale description may be tolerable; an authorization decision needs stricter freshness and should not share the same cache policy.

A cache key identifies a reusable result; time-to-live limits freshness; invalidation removes a result before normal expiry. Imagine a search result depends on query, locale and permissions, but the key includes only the query. Users can receive wrong-language or unauthorized results even while the hit ratio looks excellent. Enumerate every input that changes the response, then decide which belongs in the key and which makes caching unsafe. Write behavior for misses and origin failures, not only hits. Updates must either invalidate affected entries or accept a defined stale window. Caching reduces repeated work but adds another data lifecycle. Choose freshness from the user contract and sensitivity of the decision, then monitor correctness as well as latency improvement. Test an identity-changing request explicitly.

Why this matters

A stale description may be tolerable; an authorization decision needs stricter freshness and should not share the same cache policy.

A common trap

Authorization responses can safely share the same public cache key.

CDN distribution

A CDN serves cacheable content near clients. Cache keys, headers and origin behavior determine what can be shared; personalized responses require deliberate isolation. A public logo can use a long-lived versioned path such as logo.v3.png. A private account response requires deliberate cache controls and identity isolation so one user's data is not served to another.

WORKED EXAMPLE
A public logo can use a long-lived versioned path such as logo.v3.png. A private account response requires deliberate cache controls and identity isolation so one user's data is not served to another.

An edge location serves nearby requests; an origin holds authoritative content; a cache-control policy defines reuse behavior. Imagine a static asset changes but retains the same URL and a long cache lifetime. Some clients continue receiving the old content even after the origin update. Use versioned content identifiers where appropriate and plan invalidation when identifiers cannot change. Distinguish public shared responses from personalized or authenticated data; an edge must not accidentally reuse one account's page for another. Check response headers and the cache key at both origin and distribution boundaries. Broad caching improves latency and origin capacity but can spread an incorrect response widely. Validate one expected hit, one intentional miss and one private-response case before considering the delivery policy correct.

Why this matters

A private account response requires deliberate cache controls and identity isolation so one user's data is not served to another.

A common trap

Every personalized response should be cached identically for all users.

Consistency and stampedes

Cache misses can concentrate origin load when many keys expire together. Consider jitter, request coalescing and safe stale responses; measure hit ratio alongside origin traffic. If 1000 keys expire together, 1000 misses can reach the origin at once. Add expiry jitter or coalesce requests for the same key, then inspect origin load as well as hit ratio.

WORKED EXAMPLE
If 1000 keys expire together, 1000 misses can reach the origin at once. Add expiry jitter or coalesce requests for the same key, then inspect origin load as well as hit ratio.

A stampede is simultaneous miss traffic; request coalescing lets one origin fetch serve multiple waiters; stale-while-revalidate permits a bounded old value while refreshing. Imagine a popular key expires during a surge. Rather than letting every worker fetch independently, allow one refresh and share the result with other callers. Use expiry jitter across independent keys so a scheduled event does not synchronize the whole cache. Decide whether stale content is safe for the particular operation; a catalog image and an account revocation have different requirements. Mitigation can trade temporary freshness or waiting time for lower origin load. Measure the backend requests avoided and whether callers still receive acceptable data, not merely whether the cache hit percentage increased. Bound how long refresh waiters remain queued.

Why this matters

Add expiry jitter or coalesce requests for the same key, then inspect origin load as well as hit ratio.

A common trap

Simultaneous expiry never affects origin load.

KEEP THESE WITH YOU

Today’s takeaways

Cache behavior: A cache stores reusable results for faster retrieval.

CDN distribution: A CDN serves cacheable content near clients.

Consistency and stampedes: Cache misses can concentrate origin load when many keys expire together.

CLOSE THE LOOP

Ready to complete this day?

Verify the practical steps and pass the knowledge check (at least two thirds correct).

Lab steps verifiedKnowledge check passed