2023-01-10 · 4

AWS CloudFront (CDN)

cloudaws

This post is over 2 years old. The content may be outdated.

What is a CDN?

  • A CDN (Content Delivery Network) is a system that delivers content faster to users who are geographically and physically far away.
  • When a user downloads content (web objects, video, music, images, documents...) from a distant origin server, it takes longer than fetching it from a nearby server.
  • So the content is stored on a cache server located close to the user, and the cache server responds to content requests.

Benefits of a CDN

  • Faster website loading: reduces traffic load and cost on the origin server.
  • Lower bandwidth cost: server costs go down.
  • Reliable content delivery: data is served through direct connections to ISPs, so content arrives fast.
  • Better website security: a large fleet of distributed servers can absorb attack traffic.

How a CDN works

  • The first request fetches content from the origin server, sends it to the client, and stores it on the CDN caching node at the same time.
  • From the second request onward, the cached copy is served until the content expiry set by the CDN provider.
  • Only frequently used pages are cached on CDN nodes, and content is periodically evicted if it is not requested.
  • If content is unavailable or stale, the CDN acts as a proxy to the origin server and stores fresh content so it can answer future requests.

What is CloudFront?

CloudFront is the CDN service provided by AWS. Its purpose is to give users faster delivery through caching. It places edge servers all over the world and serves data from the edge server closest to the user.

CloudFront components

  • Origin Server
    • The server holding the original data.
    • In AWS this means S3 or EC2.
  • Edge Server
    • The servers AWS actually operates across the world.
    • Edge servers provide caching so requested data can be answered quickly.

CloudFront request flow

  • A client sends a request to an edge server.
  • The edge server checks whether the requested data is cached.
  • If a nearby edge server has the cached data, it responds with it directly.
  • If suitable data is not cached, the request is forwarded to the origin server.
  • The origin server returns the data, the edge server creates a cached copy, and the response goes back to the client.

CDN content types in CloudFront

  • Download Distribution: serves typical images or static files downloadable over the HTTP protocol.
  • Streaming Distribution: serves video content using HTTP progressive download or RTSP (Real Time Streaming Protocol).

Edge server cache characteristics

  • By default, once requested, content stays cached on the edge server.
  • The default edge TTL is 24 hours and can be changed (TTL changes take about an hour to propagate to edge servers).
  • Because of this propagation delay, instead of changing the TTL for everything, you can delete individual objects with the invalidation API (removes specific files from cache).
  • The invalidation API allows up to 3 concurrent requests, each covering up to 1,000 objects.
  • Invalidations take about 5–10 minutes to reach edge nodes.

Using CloudFront

This is the CloudFront main screen. Click Create distribution.

Match the settings to the screenshot above. Leave everything else at the defaults.

You can see it was created successfully. Paste that domain straight into your browser.

I connected it to S3, so the S3 website works through it. Now let's test whether CloudFront is actually doing its job. I measure speed with Postman, using both the S3 domain and the CloudFront domain.

This is S3's latency — I measured 436ms down to 200ms.

This is CloudFront. The difference is dramatic: 15ms down to 9ms. CloudFront is clearly working as intended.

References


Original (Korean): tistory — published 2023-01-10, migrated to this blog. This translation was generated with the help of AI.

Comments

Delete this comment?

Related posts

AWS CloudFront (CDN) · 나봄하랑