Ravindra BagaleCourses & study guides मराठी Track your progress

AWS Cloud

What Is Amazon CloudFront and Why Use It? Edge Locations and Caching Explained

In How to Host a Static Website on S3 with CloudFront we used CloudFront once, quickly. Now we learn it properly. This chapter answers why a CDN exists, what edge locations and regional edge caches are, how a cache hit and a miss work, where CloudFront is used, and how price classes change your bill. The lab shows the speed-up with real numbers.

In this chapter

  • Why a server in Mumbai feels slow in London and New York
  • CDN, edge locations (POPs) and regional edge caches
  • Cache hit vs cache miss, step by step
  • Use cases and price classes
  • Measure the difference with curl and X-Cache

1. A story: a Mumbai shop goes global

Priya sells handmade sarees. Her site runs on one EC2 server in Mumbai (ap-south-1). Customers in Pune say it is fast. Customers in London and New York say every page takes 2 to 3 seconds and photos load slowly.

The server is not slow. The distance is. Light in fibre takes time, and each page needs many round trips: DNS, TCP, TLS, HTML, then 30 images. Mumbai to New York is roughly 200 ms per round trip. Thirty round trips add up.

The fix: keep copies of her photos and pages in London and New York. That is a CDN.

2. What is a CDN and what is CloudFront?

You cannot run a server in every city. A CDN company already has servers in hundreds of cities and rents them to you.

A CDN (Content Delivery Network) is a network of cache servers near users. Amazon CloudFront is AWS's CDN. Your real server is the origin (S3, an ALB, EC2, or any HTTP server). CloudFront sits in front of it and gives you a domain like d111abcd.cloudfront.net.

LayerWhat it isCount (AWS, 2026)
Edge location / POPPoint of Presence: the server group a viewer connects to first750+ POPs, 100+ cities
Embedded POPSmall caches inside internet providers' networks1,140+
Regional edge cache (REC)Bigger mid-tier cache between POPs and your origin15
OriginYour real serverYou choose

Compare a cache miss with a cache hit

Both users request the same cacheable image. The second request can avoid another origin download when a usable cached copy is present.

Compare a cache miss with a cache hitViewer: Requests /images/course.jpg; CloudFront cache: Looks up the configured cache key; Origin on a miss: Supplies the object when needed; Viewer response: Receives the image bytesViewerRequests /images/course.jpgCloudFront cacheLooks up the configured cache keyOrigin on a missSupplies the object when neededViewer responseReceives the image bytes
Read all steps
  1. Build the cache lookup

    The path and the cache policy determine the cache key, including any configured query strings, headers or cookies. Two similar-looking URLs are not necessarily the same cache entry.

  2. A usable copy is absent

    If this cache misses, CloudFront may find a fresh copy in another cache tier. It requests the object from the origin when no usable copy is found in the applicable cache tiers. The response headers and cache configuration determine whether and how long the result can be cached.

  3. A later request is a hit

    A later request with the same key can use a fresh cached copy. CloudFront can return it without fetching the object from the origin again. This reduces origin work as well as potential network delay.

  4. An origin update is not an instant cache update

    Replacing the origin image does not immediately replace every cached copy. Use an appropriate freshness policy, versioned file names or an invalidation when needed. Personalised responses require deliberate cache design.

3. Cache hit vs cache miss

Watch two requests for the same image

Put one picture, banner.jpg, at the shop’s origin. Request the exact CloudFront URL once. If a usable cached copy is unavailable, CloudFront fetches the object. Request the same URL again from the same test network while it is fresh. If the same cache location still has the object, it can respond without fetching it again.

Read the response headers instead of judging only by speed. X-Cache can distinguish a hit from a miss and Age, when present, gives the cached response’s age. A change in cache location, requested key or cacheability can produce another miss. Check those details before assuming an edge location is broken.

Knowing the path tells you why the first visitor is slower and why most visitors are fast.

One request through CloudFrontViewer in LondonGET /img/saree1.jpgDNS sends them to thenearest POP (London)POP has it?yes = HIT, reply in ~10 msNo: ask the regionaledge cache (Frankfurt)REC has it?yes = reply, POP stores a copyNo: fetch from originS3 in Mumbai (MISS)Copy stored at REC and POPnext viewers get a HIT
Only the first request per location goes all the way to Mumbai.

The response header X-Cache tells you which one happened: Miss from cloudfront, Hit from cloudfront, or RefreshHit from cloudfront (the copy was old, the origin said it is still fine). Header X-Amz-Cf-Pop shows the POP code, like BOM78-P1 for Mumbai or LHR61-P2 for London. Age shows how many seconds the copy has been in cache.

Ravindra Bagale's Tip

Even uncached requests (APIs, logins) get faster: the TLS handshake ends at the nearby POP, and CloudFront keeps warm connections to your origin over the AWS backbone.

4. Where CloudFront is used

Use caseOriginWhy CloudFront
Static website (React, Angular, Hugo)S3HTTPS, private bucket, fast worldwide
Video courses, reelsS3 / MediaPackageBig files cached near viewers; signed URLs for paid content
APIs (Node, Django, Spring, Laravel, .NET, Go)ALB / API GatewayTLS near users, fewer slow handshakes, WAF in front
Software downloads, game patchesS3One upload, millions of downloads, origin not overloaded
DDoS and attack protectionAnyAWS Shield Standard is included free; add AWS WAF rules
WordPress, PHP sitesEC2 / ALBCache /wp-content/*, pass /wp-admin/* through

5. Price classes and cost

Serving data from some places costs AWS more. If your users are only in a few regions, you can skip the expensive edges.

Price classEdges usedPick it when
AllEvery edge locationUsers everywhere, best speed
200North America, Europe, Israel, Middle East, Africa and most of Asia including IndiaIndia + Europe + US audience
100North America, Europe and Israel onlyUsers mainly in US/Europe; lowest price

With Price Class 100, a viewer in Pune is still served, but from a farther edge (for example in Europe), so it is slower. For an Indian audience use 200 or All.

Pay-as-you-go billing is per GB sent to viewers (the rate depends on the region) plus per 10,000 requests. The always-free tier gives 1 TB out and 10 million requests each month. AWS also offers flat-rate monthly plans (starting with a $0 Free plan) that bundle CloudFront, WAF, Route 53 DNS and S3 credits with no overage. Data from AWS origins (S3, ALB, EC2) to CloudFront has no transfer charge.

6. Worked examples

Decide what can be safely reused

The same shop logo can be sent to many visitors. A page showing one signed-in customer’s order history belongs to that customer. Before choosing a cache behavior, list which requests can share a response and which ones must be evaluated for each viewer. Shared caching works best when the response represents a shared object.

Give the site a cache rule for stable assets and a different rule for customer-specific pages. Test two lab accounts and confirm each sees only its own page. A fast response is useful only when it contains the correct data for the viewer.

  1. A 2 MB hero image, 10,000 London visitors a day. Without CDN: 10,000 trips to Mumbai. With CloudFront: 1 trip per POP, the rest are HITs.
  2. An API on an ALB in Mumbai, called from New York. Not cacheable, but TLS ends in New York and the request rides the AWS backbone: often 30 to 50 percent faster first byte.
  3. A 500 MB installer downloaded 100,000 times: origin serves it a few dozen times; CloudFront serves the rest.

7. Lab: CloudFront in front of an S3 image

You need an AWS account and CloudShell (or a laptop with curl). Region ap-south-1.

  1. S3 › Create bucket: name yourname-cf-lab, Region ap-south-1, keep Block all public access on. Why: CloudFront will read it privately.
  2. Upload a photo of about 1 MB: aws s3 cp photo.jpg s3://yourname-cf-lab/img/photo.jpg.
  3. CloudFront › Create distribution. Origin domain: pick yourname-cf-lab.s3.ap-south-1.amazonaws.com.
  4. Origin access: Origin access control settings (recommended) › Create new OAC › defaults › Create.
  5. Viewer protocol policy: Redirect HTTP to HTTPS. Cache policy: CachingOptimized. Why: a good default for static files.
  6. Web Application Firewall: Do not enable security protections (lab only). Price class: Use North America, Europe, Asia, Middle East and Africa (that is 200).
  7. Create distribution. Click Copy policy in the yellow banner, open the bucket › Permissions › Bucket policy › paste › Save.
  8. Wait until Last modified shows a date (not Deploying). Copy the domain, e.g. d111abcd.cloudfront.net.
  9. First request: curl -sI https://d111abcd.cloudfront.net/img/photo.jpg | grep -iE "x-cache|x-amz-cf-pop|age". Expected: X-Cache: Miss from cloudfront.
  10. Run it again. Expected: Hit from cloudfront and an Age header.
  11. Time it: curl -so /dev/null -w "total %{time_total}s\n" https://d111abcd.cloudfront.net/img/photo.jpg. Run 3 times and note the numbers.
  12. Compare with S3 directly using a pre-signed link: U=$(aws s3 presign s3://yourname-cf-lab/img/photo.jpg --region ap-south-1); curl -so /dev/null -w "total %{time_total}s\n" "$U". Why: the bucket is private, so a plain URL would give 403.
  13. Best test: run both commands from CloudShell in a far Region, such as us-east-1 or eu-west-2. Expected: CloudFront HITs are several times faster than S3 Mumbai.

Cleanup

  1. CloudFront › select the distribution › Disable. Wait until it is disabled, then Delete.
  2. aws s3 rb s3://yourname-cf-lab --force. Delete the OAC under CloudFront › Origin access.

Common errors and fixes

These are synthetic practice situations. Diagnose the signal before changing a resource; no infrastructure changes are needed to read this guide.

The second request is still a miss.

Compare the full request, cache behavior, cache key and response headers. Requests may reach different edges, use different key values or target uncacheable content. Two requests are not a guarantee of a hit; a miss by itself is not an outage.

A cached response is assumed to be the newest origin file.

A hit proves that a cached copy answered, not that the origin was just checked. Inspect cache headers, content version and the intended freshness requirement. Publishing a new origin file does not automatically replace every existing edge copy.

A single fast test is treated as a global speed guarantee.

Record where the test ran, the object size, response status and whether the response was a hit. Compare repeated like-for-like requests. A fast error page or a nearby test client does not demonstrate better performance for every viewer.

Practical FAQ: decisions during real work

Does TTL guarantee that an object stays at the edge until that time?

No. CloudFront can evict infrequently requested objects before expiry. TTL controls freshness/revalidation behavior; it is not reserved storage. A later request may need another origin fetch even though the nominal freshness time has not passed.

Is CloudFront a replacement for the origin backup?

No. Cached copies are temporary and can expire or be evicted. Keep the authoritative content and its recovery plan at the origin/storage layer. Do not delete an origin object just because one edge currently serves a hit.

Can every page of an app share one cached response?

No. Public static assets and personalized responses have different requirements. Decide whether the response changes by user or request before enabling shared caching. For an authenticated page, correctness and privacy come before a high hit ratio.

8. Interview questions

Q. What is CloudFront?

AWS's CDN. It caches content at edge locations near viewers and fetches from your origin on a miss.

Q. Edge location vs regional edge cache?

The edge (POP) is where viewers connect. The REC is a larger mid-tier cache between POPs and the origin, so fewer requests reach the origin.

Q. How do you know if a response came from cache?

The X-Cache header: Hit from cloudfront or Miss from cloudfront. Age shows seconds in cache.

Q. Does CloudFront help APIs that cannot be cached?

Yes. TLS ends near the user and traffic uses the AWS backbone with reused origin connections.

Q. Which price class for an India-first audience?

Price Class 200 or All. Price Class 100 has no Indian edges.

Try at home

  1. Repeat the timing test from three Regions and make a small table.
  2. Find the POP code in X-Amz-Cf-Pop and match the first three letters to an airport code.

In short

  1. Distance, not server power, makes far users slow.
  2. CloudFront = CDN: POPs near users, RECs in the middle, your origin behind.
  3. HIT = served from cache; MISS = fetched from origin; X-Cache tells you.
  4. Price class 100/200/All trades cost for reach; India needs 200 or All.

Practise this chapter

Check your understanding

Choose an answer, then compare your reasoning with the explanation.

0 of 3 answered correctly
1. You request the same shop logo twice from the same location. Which response header can tell you whether CloudFront served the object from its cache?

Answer and explanation

B. X-Cache

X-Cache reports whether CloudFront used its cache or had to fetch. Compare the exact URL, location and Age when interpreting the result.

2. Which data can usually be shared under one cache key between every visitor?

Answer and explanation

C. The same public shop logo

Shared caching suits responses that should be the same for all viewers. Treat private or personalized content separately.

3. CloudFront serves its cached version of an image after the origin changed. What can explain it?

Answer and explanation

A. The cached response is still fresh

A CDN can serve a fresh cached copy without fetching the changed origin object for every request.

Practice Tasks

Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.

About this scenario

Seoul Beauty Lab is a cosmetics business in Seoul. Ji-woo Park looks after its IT and cloud work. They count money in KRW and run their AWS resources in the ap-northeast-2 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 10

Easy List distributions

Find the distribution ID and domain name.

List all CloudFront distributions.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy How many addresses in a /16?

Picking a VPC size needs this count.

Seoul Beauty Lab plans the VPC 10.71.0.0/16. How many IP addresses does it hold in total?

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Easy Port for SSH

Security group rules need the right port.

Which TCP port must the security group open for SSH?

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Clear the CloudFront cache

After a deploy, invalidate so visitors get the new files now, not after the TTL.

Invalidate all paths (/*) on distribution E2QWRUHAPOMQZL.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Create a distribution from a config file

The full config (origins, behaviors, certificate) is easiest to keep in JSON.

Create a CloudFront distribution from dist.json.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Invalidate two paths

Invalidating only what changed is cheaper and faster.

On E2QWRUHAPOMQZL, invalidate /index.html and /css/site.css.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Bucket policy for CloudFront OAC

With OAC, only your CloudFront distribution may read the private bucket.

Allow the service cloudfront.amazonaws.com to s3:GetObject on seoulbeauty-site/*, only when aws:SourceArn equals arn:aws:cloudfront::119519391020:distribution/E2QWRUHAPOMQZL.

  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Hard Create an Origin Access Control

OAC lets only CloudFront read your private S3 bucket.

Create an origin access control from oac.json.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Request a free certificate for CloudFront

CloudFront only uses ACM certificates from us-east-1. DNS validation renews automatically.

Request a certificate for seoulbeauty.kr and www.seoulbeauty.kr with DNS validation, in us-east-1.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Make a CloudFront signed URL

Signed URLs let only paying users watch a video, for a limited time.

Sign https://d111.cloudfront.net/course/intro.mp4 with key pair ID K2JCJMDEHXQW5F, private key private_key.pem, expiring 2026-12-31.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Revision, comparison and interview cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

Revision card: What is Amazon CloudFront?. CloudFront is AWS's CDN. It keeps copies of your files near viewers. Your real server is the origin: S3, ALB, EC2 or any HTTP server. 750+ POPs and 15 regional edge caches worldwide. You get a domain like d111abcd.cloudfront.net.
Revision card: What is Amazon CloudFront? · Download PNG
Revision card: Cache hit vs miss. HIT: the edge has the file and answers in milliseconds. MISS: the edge asks the regional edge cache, then the origin. The X-Cache header tells you which happened. Check it with curl.
Revision card: Cache hit vs miss · Download PNG
Revision card: CloudFront price classes. All: every edge, best speed. 200: adds most of Asia (incl. India), Middle East, Africa. 100: only North America, Europe, Israel. Cheapest. Indian users? Pick 200 or All.
Revision card: CloudFront price classes · Download PNG
Comparison: Edge location (POP) vs Regional edge cache. Edge location (POP): Where viewers connect first; hundreds worldwide. Regional edge cache: Bigger mid-tier cache between POPs and the origin.
Comparison: Edge location (POP) vs Regional edge cache · Download PNG
Comparison: Price Class 100 vs Price Class 200. Price Class 100: North America, Europe and Israel only. Price Class 200: Adds most of Asia including India, Middle East and Africa.
Comparison: Price Class 100 vs Price Class 200 · Download PNG
Interview card: Why put CloudFront in front of an app in Mumbai?. Far users get files from a nearby edge, not from Mumbai. Even uncached APIs gain: TLS ends near the user. Plus free Shield Standard and optional WAF.
Interview card: Why put CloudFront in front of an app in Mumbai? · Download PNG

More cards: Ravi Sir's Cards.