← Building blocks

Serve static bytes from the edge

Latency & performancefundamental

Users far from your servers wait on round trips for images, scripts, video segments.

Say it like this

Latency to users is dominated by distance, so I serve every static and cacheable byte from a CDN edge near them and let the origin handle only misses and dynamic requests.

Basic solution

Put a CDN in front of the origin. Content is cached at points of presence near users; the origin only sees misses.

CloudflareCloudFrontAkamaiFastly

Edge, shield and origin

Each hop caches. A user hits the nearest edge; edge misses go to one regional shield; only shield misses reach the origin, so a popular object is fetched from origin about once per shield per TTL.

How it works

  1. DNS for the site resolves to the CDN rather than to your servers, and the CDN routes each user to its nearest point of presence (PoP), either by announcing the same IP from every PoP (anycast) or by answering DNS with the closest PoP's address.
  2. The edge server at that PoP builds a cache key from the request (the URL plus whichever headers or query parameters you told it matter) and looks that key up in its local cache.
  3. On a hit it reads the bytes from its own disk or memory and returns them, typically within 10 to 30 ms of the user, because the request never left the user's region.
  4. On a miss it fetches the object from your origin (or first from a regional shield cache that sits between the edges and the origin), stores the response for as long as the Cache-Control header allows, and returns it to the user.
  5. Every later request for that object from anyone near that PoP is a hit, until the TTL expires or you purge the object explicitly.

What the origin sees

  • One request per object per PoP (or per shield) per TTL, instead of one request per user. For a popular video segment that is a 1000x reduction in origin traffic.

Worked example

Video streaming platform

  • 50M daily viewers, average 1 hour at 2.5 Mbps ≈ 1.1 GB per viewer per day ≈ 55 PB/day ≈ 5 Tbps average egress.
  • Top 20% of videos take 80% of views. The hot set of segments fits in edge SSDs (a few hundred TB per PoP) and hits at 95%+.
  • Origin egress with a 95% edge hit rate and a regional shield: 5 Tbps × 5% ≈ 250 Gbps from object storage, which is what a single region of S3 serves comfortably.
  • Start-up latency: first segment from an edge 20 ms away vs 150 ms cross-continent from origin; with a 4-second segment the player buffers two segments and starts in under a second.
  • Cost: CDN egress at roughly a tenth of cloud origin egress per GB; the 95% hit rate is the whole business case.

Variants and when each wins

Pull CDN (origin on miss)

  • The edge fetches an object from your origin the first time a PoP sees it and caches it from then on. It wins for almost everything because nothing has to be published ahead of time; the cost is one slow first view per PoP for cold content.

Push CDN (pre-publish)

  • You upload objects to the CDN before anyone requests them. It wins for a scheduled premiere or a large release where the first view must be fast, and it is wasted effort for content nobody may watch.

Tiered caching with an origin shield

  • Edge misses go to a regional cache, and regional misses go to one shield cache in front of the origin. It wins whenever you have many PoPs, because 300 edge misses for the same object become one origin fetch.

Edge compute

  • Small functions run at the PoP itself: authentication checks, A/B bucketing, image resizing, redirect decisions. It wins for logic that is cheap but must run per request, because it keeps that work off the origin.

Recommendation: pull CDN with an origin shield; pre-warm only for known events; cache HTML only if it is the same for everyone.

Failure modes

Serving private content from a shared cache

  • You notice one user sees another user's account page. It happens because a personalised response was cached under a key that does not include the user, so the next person with the same URL got it. Fix it by marking anything user-specific Cache-Control: private or no-store, and varying the cache key on the auth cookie only when you intend to cache per user.

Stale assets after a deploy

  • You notice pages break right after a release: new HTML loads old JavaScript, or old HTML loads new JavaScript. It happens because assets were cached for a long time under URLs that did not change when their contents did. Fix it by putting a content hash in every asset filename and caching those forever, while the HTML that references them gets a short TTL or no caching.

Origin overload on a cold PoP

  • You notice origin traffic spikes when a new region comes online or after a purge. It happens because every edge in that region misses on everything at once. Fix it with tiered caching behind a shield, request collapsing so one miss triggers one origin fetch, and purging by surrogate key (a tag on related objects) instead of purging everything.

Cache key explosion

  • You notice a low hit rate on content that should be hot. It happens because query strings, tracking parameters or Vary headers make every request look unique to the cache. Fix it by normalising the cache key: strip parameters that do not change the response and vary only on headers that do.

Purge lag

  • You notice a takedown or correction keeps appearing for a minute after you removed it. It happens because a purge has to reach hundreds of PoPs. Fix it with short TTLs on content that may need urgent removal, and versioned URLs so the old object is simply never linked again.

Interviewer follow-ups

Answer out loud before you open each one.

What can you cache on a CDN besides images and video?

Anything identical for every requester with the same key: API responses for public data, anonymous HTML, redirect responses, even 404s. The test is whether a response can be shared. If it depends on the user, keep it private or cache only the shared fragments.

How do you pre-warm the CDN for a premiere?

Push the first few minutes of every rendition to every PoP ahead of time, or have a job request those segments through each region so the shield and edges populate. Later segments can stay pull-based because viewers reach them at different times.

What is an origin shield and why use one?

A single cache layer between all the edges and your origin. Every edge miss goes to the shield; the shield fetches from origin once per object. It turns worldwide misses into one origin fetch and protects the origin during purges and cold starts.

Should the URL shortener redirect itself be served from the CDN?

Only if slow invalidation is acceptable. A cached 302 keeps redirecting to a deleted or changed target until TTL or purge. Most designs keep the redirect at the application layer, under a millisecond with Redis, and use the CDN for static assets and TLS termination.

Connections

In study paths
Scaling reads · Video from upload to play
Pairs with
Let browsers and proxies cache · Stream video to any connection · Keep large blobs out of the database · Terminate TLS at the edge · Route users to the nearest healthy site
Pulls against
Invalidate the cache when data changes
Used in
Video Streaming Platform (YouTube / Netflix) · URL Shortener (TinyURL / bit.ly)

The same parts on other clouds

The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.

AWSUsed here asGoogle CloudMicrosoft AzureOpen source and others
Amazon CloudFrontEdge PoP, Origin shieldCloud CDN, Media CDNAzure Front DoorCloudflare, Fastly, Akamai
Amazon S3Origin / object storeCloud StorageBlob StorageCloudflare R2, MinIO, Ceph