Caching and CDN Basics
Lesson 16Beginner1h 18mAssessment-backed

Caching and CDN Basics

Learn how caches and CDNs reduce repeated work, improve latency, protect origins, and introduce freshness and invalidation trade-offs.

What you will be able to do

Explain caching and CDNs in practical system design terms.
Choose what to cache based on read frequency, freshness, and correctness.
Understand cache-aside, TTL, invalidation, stale data, hot keys, and cache stampede basics.
Map caching to AWS, GCP, Azure, application caches, and edge CDNs.
Avoid caching data that should remain strongly correct or private.

A cache stores a faster copy of data so the system does not repeat expensive work. A CDN is a geographically distributed cache, usually for static assets and cacheable web/API content near users.

Why Cache?

Caching can reduce latency, database load, compute cost, and origin traffic. But every cache introduces a freshness question: when is the copy allowed to be stale, and how will it be updated or invalidated?

Browser CDN application cache database caching path
Caches can exist at browser, edge, application, database, and derived-view layers.
Cache layerGood forRisk
Browser cacheStatic assets and user-local reuseHard to invalidate immediately.
CDN/edge cacheImages, scripts, public pages, some API responsesStale public content or accidental private caching.
Application cacheHot database results and expensive computed dataInvalidation and memory pressure.
Database cache/bufferInternal database performanceNot a product-level caching strategy.
Materialized viewPrecomputed summariesFreshness and update correctness.

Scenario: Course Homepage

A course homepage has cover images, course metadata, lesson lists, learner-specific progress, and recommendations. These pieces should not all have the same caching rule.

DataCache decisionReason
Cover imagesCDN with long TTLStatic and shared by many users.
Published course metadataCDN/app cache with moderate TTLChanges infrequently.
Lesson listCache until publish event invalidatesMostly stable, but must update after content release.
Learner progressShort cache or no shared CDN cacheUser-specific and should stay reasonably fresh.
RecommendationsCan be cached per user for minutesPersonalized but not strictly correct.

Cache-Aside and TTL

In cache-aside, the application checks the cache first. On a miss, it reads from the source of truth, stores the result in cache, then returns it. TTL decides how long cached data may live.

ConceptMeaningExample
Cache hitData found in cacheReturn course metadata from Redis.
Cache missData not found in cacheRead database and populate cache.
TTLTime before entry expiresCourse metadata expires after 10 minutes.
InvalidationRemove/update stale cache entryPurge lesson list after publishing new lesson.
Stale-while-revalidateServe stale copy while refreshingFast course page while background refreshes content.

Design principle

Cache data that is expensive to fetch or compute, read often, and safe to be stale for a clearly defined time.

Hot Keys and Stampedes

A hot key receives extreme traffic for one cached item. A cache stampede happens when many requests miss or expire at once and all hit the origin together.

ProblemSymptomMitigation
Hot keyOne course/product/news item gets huge trafficReplicate cache, local cache, request coalescing, edge cache.
StampedeOrigin spikes when cache expiresJitter TTLs, lock/coalesce refreshes, stale-while-revalidate.
Stale sensitive dataUser sees incorrect balance/progressShort TTL, event invalidation, or avoid caching critical value.
Private data leakOne user's data served to anotherCorrect cache keys and avoid shared CDN for private responses.

Cloud Caching and CDN

NeedAWSGCPAzure
CDNCloudFrontCloud CDNAzure Front Door/CDN
Managed Redis/MemcachedElastiCacheMemorystoreAzure Cache for Redis
Object originS3Cloud StorageBlob Storage
Edge securityWAF/ShieldCloud ArmorWAF/DDoS Protection
Invalidation/purgeCloudFront invalidationsCloud CDN invalidationAzure CDN/Front Door purge

Cache Review Checklist

  • What exact data is cached?
  • Who is allowed to see it?
  • What is the source of truth?
  • How stale may it be?
  • How is it invalidated or refreshed?
  • What happens if the cache is empty or down?
  • Can one key become hot?

Beginner Mistakes

  • Caching before knowing the slow query or expensive operation.
  • Caching private user data in a shared CDN.
  • Forgetting invalidation and serving stale product-critical data.
  • Using the cache as the only source of truth.
  • Setting identical TTLs that cause synchronized cache stampedes.

Guided Practice

Practice task

For a product detail page, decide caching rules for product image, product title, inventory count, reviews summary, and personalized discount.

Sample Answer

DataCaching rule
Product imageCDN long TTL with versioned URL.
Product title/descriptionCDN or app cache with invalidation on catalog update.
Inventory countShort TTL or no cache if scarce and checkout-critical.
Reviews summaryCache for minutes; update asynchronously.
Personalized discountPrivate cache only or compute on request; do not share through public CDN.

Module 4 Wrap-Up

You now understand the first scalability toolkit: scale the bottleneck, distribute traffic with load balancers, keep services stateless, and cache safe repeated reads.

Before You Continue

  • You should know vertical and horizontal scaling trade-offs.
  • You should understand load balancer responsibilities.
  • You should recognize why stateless services are easier to scale.
  • You should know caching and CDN freshness risks.
  • You are ready for distributed data: replication, partitioning, consistency models, and conflicts.